Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: change spaces to _

1 view
Skip to first unread message
Message has been deleted

billious

unread,
Sep 23, 2008, 11:25:47 AM9/23/08
to

"me at" <my.ad...@is.invalid> wrote in message
news:48d904bd$0$33575$742e...@news.sonic.net...
>
> 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
>

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


Timo Salmi

unread,
Sep 23, 2008, 12:01:57 PM9/23/08
to
me at <my.ad...@is.invalid> wrote:
> 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 _?

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

FileGod

unread,
Sep 23, 2008, 12:08:25 PM9/23/08
to
me at <my.ad...@is.invalid> wrote:
>I have yet to find a clean accurate way to deal with0file names with spaces
in them.@nPlease, Has anyone a script I can run the a commandfprompt that
will cycle thru the files in the currentcdirectory and rename files with
spaces to files with _? <WinXP9$Thanks,egVic

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%

http://www.filegod.netfirms.com

Message has been deleted

Timo Salmi

unread,
Sep 23, 2008, 1:23:02 PM9/23/08
to
me at <my.ad...@is.invalid> wrote:

> Timo Salmi <t...@uwasa.fi> wrote:
> | me at <my.ad...@is.invalid> wrote:
> |> 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 _?

> | 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

ten.n...@virgin.net

unread,
Sep 23, 2008, 3:29:57 PM9/23/08
to
On 23 Sep 2008 15:01:17 GMT, me at wrote:

> 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.

001.d...@gmail.com

unread,
Sep 25, 2008, 2:09:19 AM9/25/08
to
@echo off
setlocal
for /f "tokens=*" %%i in ('dir /b /s /a C:\') do call:ren "%%i"
goto:eof

:ren
set "new=%~nx1"
set "new=%new: =_%"
if not "%new%"=="%~nx1" (
if not exist "%~dp1%new%" (
ren %1 "%new%"
) else (
ren %1 "~%new%"
)
)

0 new messages