How to replace a single character in Windows filenames using a batch file?

55,925

Solution 1

From the command prompt - assuming that all of your files are in the same directory:

ONE-LINER

for /f "tokens=* delims= " %i in ('dir /b "*.txt"') do Set LIST=%i& set LIST | ren "%~fi" "%LIST:-=_%"

Keep in mind that this is a one shot per command prompt window. That means if you cancel this for any reason, then you'll need to open another command prompt and run again.

Solution 2

Found it on stackoverflow:

https://stackoverflow.com/questions/261515/batch-file-script-to-remove-special-characters-from-filenames-windows

Set fso = CreateObject("Scripting.FileSystemObject")
Set re  = New RegExp

re.Pattern = "[-]" ' put all characters that you want to strip inside the brackets'
re.IgnoreCase = True
re.Global = True

If WScript.Arguments.Unnamed.Count = 1 Then
  If fso.FolderExists(WScript.Arguments.Unnamed(0)) Then
    Recurse fso.GetFolder(WScript.Arguments.Unnamed(0))
  Else
    WScript.Echo "Folder not found."
  End If
Else
  WScript.Echo "Please give folder name as argument 1."
End If


Sub Recurse(f)
  For Each sf In f.SubFolders
    Recurse sf
     WScript.Echo sf.Name, " -> ", re.Replace(sf.Name, "_")
    sf.Name = re.Replace(sf.Name, "_")
  Next
  For Each sf In f.Files
     WScript.Echo sf.Name, " -> ", re.Replace(sf.Name, "_")

     If sf.Name <> re.Replace(sf.Name, "_" ) Then
       sf.Name = re.Replace(sf.Name, "_")
     End If
  Next
End Sub

Solution 3

Another solution would be to use two batch files.

File 1: run_rn.bat:

forfiles /m "*.log" /c " cmd /c rn @file"

File 2: rn.bat:

set LIST1=%1
set LIST2=%LIST1:_=;%
ren %LIST1% %LIST2%

The batch files have to be in the same directory like the files you want to change.

Share:
55,925

Related videos on Youtube

Matt Rogish
Author by

Matt Rogish

I like: Ruby on Rails, Mobile Apps, IT/Strategy, Travel, etc. etc.

Updated on September 17, 2022

Comments

  • Matt Rogish
    Matt Rogish over 1 year

    I have a Windows Server 2003 server that has a whole bunch of filenames that need renaming. Basically, I just need all - (hyphens) replaced with _ (underscores), no matter where they are in the filename. Assume that there are no duplicates.

    I can do this on my Mac with a little script but the files are too large and crazy to transfer to my Mac, rename, then go back to the server. Is it possible to do this in a Windows command prompt without having to download a renamer or any additional software?

  • TheModularMind
    TheModularMind over 14 years
    this seems to be removing, rather than replacing, although I am sure it could be amended reasonably easily
  • TheModularMind
    TheModularMind over 14 years
    I was trying to figure out a for /f method for it and my brain leaked out through my ears well before I got to the solution. Cracking bit of code, well done!
  • quack quixote
    quack quixote over 14 years
    the post you link to includes instructions for running the script, it would be helpful if you included that info in your answer.
  • Murdoch Ripper
    Murdoch Ripper over 14 years
    Thanks Adam. This actually took me about a week to figure out how to do. The only reason I did it was because of Command Line Kung Fu! (blog.commandlinekungfu.com/2009/05/…)
  • Matt Rogish
    Matt Rogish over 14 years
    Nice!!! I have a one-liner on my mac that I used, was hoping for one in Windows. NICE JOB!
  • Daniel Mošmondor
    Daniel Mošmondor about 12 years
    I edited it for a little correction (I had problems with spaces inside the filenames)
  • Sun
    Sun almost 11 years
    does this work for folders as well?
  • Murdoch Ripper
    Murdoch Ripper over 10 years
    Yes, it does. Just change the following code from "('dir /b "*.txt"')" to "('dir /ad')".