This solution developed using XP
It may work for NT4/2K
----- batch begins -------
[1]@echo off
[2]setlocal enabledelayedexpansion
[3]for /f "tokens=*" %%i in ( ' dir /b /a-d ' ) do echo %%i|find " " >nul&if
not errorlevel 1 set ynn=%%i&set ynn=!ynn: =_!&echo ren "%%i" "!ynn!"
------ batch ends --------
Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed
The ECHO keyword needs to be removed to activate the rename/delete It is
there as a safety measure to show what the process WOULD do until
you have verified that it will do what you require
The spaces surrounding the single-quotes are for emphasis only. The SPACES
are not required but the single-quotes ARE required.
%varname% will be evaluated as the value of VARNAME at the time that the
line is PARSED. The ENABLEDELAYEDEXPANSION option to SETLOCAL causes
!varname! to be evaluated as the CURRENT value of VARNAME - that is, as
modified by the operation of the FOR
This will not work for filenames containing poison characters like & or ! or
%, but should be fine for normal characters. YMMV
There may be more concise ways, but this is more tolerant of the
potential poison characters
@echo off & setlocal enableextensions
set targetFolder=C:\_D\BAS
pushd "%targetFolder%"
for /f "tokens=*" %%f in ('dir /b "%targetFolder%"') do (
call :Process "%%~ff"
)
popd
endlocal & goto :EOF
::
:Process
setlocal enableextensions disabledelayedexpansion
set filename="%~nx1"
set filename=%filename: =_%
echo ren %1 %filename%
endlocal & goto :EOF
The output might look something like
ren "C:\_D\BAS\My test file.txt" "My_test_file.txt"
ren "C:\_D\BAS\My test file2.txt" "My_test_file2.txt"
ren "C:\_D\BAS\Test & (5!).txt" "Test_&_(5!).txt"
ren "C:\_D\BAS\TEST.VBS" "TEST.VBS"
ren "C:\_D\BAS\TESTADD.BAT" "TESTADD.BAT"
ren "C:\_D\BAS\TESTFNGT.BAS" "TESTFNGT.BAS"
Remove the echo when you are sure.
> WinXP
Attaboy! My compliments for stating your OS as recommended by the
regulars.
All the best, Timo
--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Hpage: http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm
I just made this one for you:
[1] @echo off
[2] for %%a in ('dir /b * *.*') do call :process "%%a"
[4] set "OldName=%1"
[5] rem The line below does a search for a space in OldName & changes it to
_
[6] set "NewName=%OldName: =_%"
[7] ren %OldName% %NewName%
> | There may be more concise ways, but this is more tolerant of the
> | potential poison characters
> Thank you very much.
Good. You are welcome. The edited solution is now stored as the new item
166} How to rename files with underscores instead of spaces?
http://www.netikka.net/tsneti/info/tscmd166.htm
> I have yet to find a clean accurate way to deal with
> file names with spaces in them.
>
> Please, Has anyone a script I can run the a command
> prompt that will cycle thru the files in the current
> directory and rename files with spaces to files with _?
>
> WinXP
>
> Thanks,
>
> Vic
And just for fun, here's my effort!
::----- START -----
@Echo off&Setlocal enableextensions
For /f "delims=" %%# In ('dir/b/a-d-s^|find " "') Do (
Set "_=%%#"&&Call Ren "%%#" %%_: =_%%)
::------ END ------
Two notes other than the usual addition of two spaces pre-line for
wrapping:
1. The -s can be removed, I'd recommend however not renaming system files.
2. It would be prudent to add a method of ensuring that the file is not run
accidentally on its current directory, the consequences could be
problematic.
:ren
set "new=%~nx1"
set "new=%new: =_%"
if not "%new%"=="%~nx1" (
if not exist "%~dp1%new%" (
ren %1 "%new%"
) else (
ren %1 "~%new%"
)
)