How do I find and replace a character in filenames in Windows 7 using Explorer?
I want to replace all the underscore characters (_
) with a space () in a filename.
How can I do this quickly, when I have lots of _
characters to replace?
Solution 1:
vbScript should do it for you. Create a file called "rename_underscores.vbs" containing the following.
Set objFso = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("c:\test\")
For Each File In Folder.Files
sNewFile = File.Name
sNewFile = Replace(sNewFile,"_"," ")
if (sNewFile<>File.Name) then
File.Move(File.ParentFolder+"\"+sNewFile)
end if
Next
Make sure the folder name is correct. (In the example, I've used c:\test) And then double click your file to do the renaming.
Solution 2:
As others have mentioned, there is no way to do exactly what you want without using a script or batch file. For instance, in PowerShell you can do what you want quite easily:
cd "C:\Users\MyName\Documents\MyDirectory"
# by default, -replace is case-insensitive (equivalent to -ireplace)
# for case-sensitive, use -creplace
Dir | Rename-Item –NewName { $_.name –replace "_"," " }
Just be sure to replace "C:\Users\MyName\Documents\MyDirectory" with the path to your directory.
For more detail and other options, including some things you can do using just Explorer, see here: http://www.howtogeek.com/111859/how-to-batch-rename-files-in-windows-4-ways-to-rename-multiple-files/
Solution 3:
I recommend using a 3rd party tool like Bulk Rename Utility. It's free and it has an installer less than 1 MB.
Solution 4:
This is within the realms of a dos bat file. Create a bat file (New->Text file, rename extension to .bat) then copy the blue sections below into it.
I'll split it into sections and explain what it's doing.
Print the current state of the directory for comparison later
dir /b
List the files in the directory, then for all the ones that contain an underscore and end in .txt, call ProcessFile on them.
for %%f in (*_*.txt) do call :ProcessFile "%%f"
goto :finished
The syntax "str=%str:x=y%" replaces instances of x with y. In this case, we're replacing "_" with nothing
:ProcessFile
set str=%1
rename %1 %str:_=%
goto :eof
That's it!
Now it just prints what was changed so we can confirm it's working as expected;
:finished
echo ----
dir /b
Results (Not part of the bat file)
baz_ing.txt
big_rawr.foo
foo_bar.txt
multiple_underscores_test.txt
----
bazing.txt
big_rawr.foo
foobar.txt
multipleunderscorestest.txt
Solution 5:
Here is my batch rename ruby script. I haven't used other batch renaming tools but I think my script is very easy to use and very versatile. Here is the command for your case:
ruby bren.rb *_* "_/ "
I have setup my windows environment in such a way bren.rb can be executed as an executable from any directory.