28

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?

1
  • 4
    As the answers are indicating, there /is/ no way to do this in Explorer. You need a script or a batch file renamer.
    – Shinrai
    Oct 8, 2010 at 14:39

7 Answers 7

30

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.

2
  • 1
    Note that the same works with folders, using 'Folder.SubFolders' instead of 'Folder.Files'. Jul 24, 2018 at 12:12
  • working perfectly, just need to take care of the path. +1 thanks
    – Raj
    Oct 20, 2019 at 14:59
13

Edit: In Windows 10 and 11, it's now possible to do this (and much more) directly in Explorer using the PowerRename extension from Microsoft's free PowerToys package. PowerToys has lots of other great extensions, too. Unfortunately, it's not available for Windows 7--but perhaps you've upgraded your OS since 2010? ;-)


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/

1
  • That is really nice, thanks!
    – LionAM
    Jun 1, 2021 at 11:57
7

I recommend using a 3rd party tool like Bulk Rename Utility. It's free and it has an installer less than 1 MB.

6
  • 1
    What if the file name is "this_is_a_test.ext"?
    – seanyboy
    Oct 8, 2010 at 13:45
  • 4
    Bulk Rename Utility is a GREAT applications. Its very versatile and i don't think you can achieve the same with Explorer. Oct 10, 2010 at 0:08
  • The dos command would be a little more complicated than that. I'll mock one up.
    – RJFalconer
    Nov 21, 2010 at 12:55
  • i think the answer re the Bulk Rename Utility app on here is more informative superuser.com/questions/455096/… Feb 16, 2020 at 20:47
  • After so many years it's still an AWESOME application. Wish I would have found it sooner.
    – Bjorn
    Jan 17, 2021 at 0:31
3

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
1
  • 2
    I had to change for %%f in (*_*.txt) do call :ProcessFile %%f to for %%f in (*_*.txt) do call :ProcessFile "%%f" in order for this to work.
    – Ste
    Dec 6, 2020 at 17:30
2

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.

1

Windows-based renaming tool, the most powerful there is, and it's free. http://www3.telus.net/pfrank/

1

This doesn't explicitly answer the question ('.. in Explorer'), but I found the most useful software to do this was Adobe Bridge (Free)

  1. Select all files in Bridge
  2. Right click > Batch Rename

Added bonus: Supports Regex!

Screen Shot

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .