CMD replace

Associate
Joined
29 Feb 2008
Posts
1,173
Location
Nottingham
I have a series of files which all have under scores (_) instead of spaces. Is there a quick cmd line I can use on windows to change all of the _ to spaces?

Cheers
 
I dont know it using the CMD prompt [too lazy to go look it up] but you could use PowerShell. If you have Vista or above it will be already installed. Just run powershell.exe from a Run box. Then enter the following all on one line:

gci "C:\myfolder" -Recurse | foreach { if(Test-Path $_.FullName -PathType Leaf) {Move-Item $_.FullName $_.FullName.replace(" ","_")}}

Change the path to the directory where your files are located. If you do not want it to process sub-directories, remove the "-Recurse" bit.

EDIT: I recommend using this line of code on some test files first - or run it on a copy of the directory you intend to change. This works as advertised on my computer, but I wont be held responsible if something goes wrong and you don't have a backup! :p
 
Thanks for the assist! But...Didn't work... I checked the directory location and it is correct so I'm stuck.
 
Last edited:
Working now. The script was backwards haha, it was trying to replace spaces with _ and not _ with spaces so it couldn't find any! Thanks for the help, all sorted now.
 
oops, my bad! I forgot to double-check which way around the replacement was. See, this is why I insist people test these things before going ahead :D
 
Back
Top Bottom