how to change spaces in filenames to underscores?

Yossarian

Lifer
Dec 26, 2000
18,010
1
81
I have a directory full of file names that I need to take the spaces out of and replace with underscores. Is there some kind of batch file thing that can do this?

for example test file.prt needs to become test_file.prt

Anyone want to take a crack at it?
 

Otaking

Diamond Member
Mar 13, 2000
5,219
0
0
Originally posted by: Anubis
why do you want to do that> i absoutly hate when people use underscores in file names

Me too. I used to take them out of filenames instead. :p
 

Yossarian

Lifer
Dec 26, 2000
18,010
1
81
Originally posted by: Anubis
why do you want to do that> i absoutly hate when people use underscores in file names

because the awful software package I have to bring them into was written back in the UNIX days and doesn't even display files with spaces in the file open dialog. and it's supposedly "windows compliant". my ass!
 

skace

Lifer
Jan 23, 2001
14,488
7
81
Right now it only echoes. Put it in a batch file and test it out, if you like the output, delete the echo before the "ren" and run it again. Also, I should add that you run it from inside the directory with all the files. That could be changed by modifying the dir line, but I wanted to leave it generic. I use something similar at home, I throw the batch file in the directory, double click and go.

...where did the insert code button go......

#CODE HERE#
@echo off
for /f "tokens=*" %%i in ('dir /b /a-d') do call :start "%%i" "%%~fi"
goto :eof

:start
set "temp0rs=%~1"
set "temp0rs=%temp0rs: =_%"
echo ren "%~2" "%temp0rs%"
goto :eof
#END CODE#
 

Yossarian

Lifer
Dec 26, 2000
18,010
1
81
Originally posted by: skace
Right now it only echoes. Put it in a batch file and test it out, if you like the output, delete the echo before the "ren" and run it again. Also, I should add that you run it from inside the directory with all the files. That could be changed by modifying the dir line, but I wanted to leave it generic. I use something similar at home, I throw the batch file in the directory, double click and go.

...where did the insert code button go......

#CODE HERE#
@echo off
for /f "tokens=*" %%i in ('dir /b /a-d') do call :start "%%i" "%%~fi"
goto :eof

:start
set "temp0rs=%~1"
set "temp0rs=%temp0rs: =_%"
echo ren "%~2" "%temp0rs%"
goto :eof
#END CODE#

thanks dude! I appreciate it!
 

sygyzy

Lifer
Oct 21, 2000
14,001
4
76
Originally posted by: skace
Right now it only echoes. Put it in a batch file and test it out, if you like the output, delete the echo before the "ren" and run it again. Also, I should add that you run it from inside the directory with all the files. That could be changed by modifying the dir line, but I wanted to leave it generic. I use something similar at home, I throw the batch file in the directory, double click and go.

...where did the insert code button go......

#CODE HERE#
@echo off
for /f "tokens=*" %%i in ('dir /b /a-d') do call :start "%%i" "%%~fi"
goto :eof

:start
set "temp0rs=%~1"
set "temp0rs=%temp0rs: =_%"
echo ren "%~2" "%temp0rs%"
goto :eof
#END CODE#


Props for giving one of the most elegant and usable solutions.
 

jimbob200521

Diamond Member
Apr 15, 2005
4,108
29
91
the only thing i could think of is if all the files will be named the same thing, with the only differance being a number at the end or something. (like if you had a bunch of pics that were of the same thing, but wanted to differentiate them by a number, you would select all the ones you want to rename, then either hit f2 or right click and go to rename, then type in the new name. windows will then rename all the files "new_name_01", "new_name_02", and so on.)
 

BrokenVisage

Lifer
Jan 29, 2005
24,771
14
81
Originally posted by: Anubis
why do you want to do that> i absoutly hate when people use underscores in file names

I thought you knew the whole world is out to piss you off. :p
 

GeekDrew

Diamond Member
Jun 7, 2000
9,099
19
81
Originally posted by: Anubis
why do you want to do that> i absoutly hate when people use underscores in file names

Congratulations. I absolutely hate when people use spaces, rather than underscores. ;)
 

Reel

Diamond Member
Jul 14, 2001
4,484
0
76
Originally posted by: GeekDrew
Originally posted by: Anubis
why do you want to do that> i absoutly hate when people use underscores in file names

Congratulations. I absolutely hate when people use spaces, rather than underscores. ;)

It is such a pain in the ass to escape out the spaces with the backslash. I much prefer to see underscores. It also opens up a can of worms occasionally when it is a space. Spaces are annoying in file names.
 

GeekDrew

Diamond Member
Jun 7, 2000
9,099
19
81
Originally posted by: Reel
Originally posted by: GeekDrew
Originally posted by: Anubis
why do you want to do that> i absoutly hate when people use underscores in file names

Congratulations. I absolutely hate when people use spaces, rather than underscores. ;)

It is such a pain in the ass to escape out the spaces with the backslash. I much prefer to see underscores. It also opens up a can of worms occasionally when it is a space. Spaces are annoying in file names.

No kidding... :p