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?

  • 2
    You could simply rename them from your other computer over SMB. No need to copy files just for a name change.
    – Joey
    Nov 5, 2009 at 7:23

6 Answers 6


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


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.

  • 2
    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!
    – AdamV
    Nov 5, 2009 at 3:14
  • 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/…) Nov 5, 2009 at 3:55
  • Nice!!! I have a one-liner on my mac that I used, was hoping for one in Windows. NICE JOB! Nov 5, 2009 at 14:07
  • I edited it for a little correction (I had problems with spaces inside the filenames) Mar 13, 2012 at 21:09
  • does this work for folders as well?
    – Sun
    Jul 18, 2013 at 18:42

Batch File to replace one character in a filename with another character

Consider using a free GUI app to hold your hand: http://www.bulkrenameutility.co.uk/Main_Intro.php

If you must this yourself with a batch file, be super careful! Batch scripts don't have an "undo" button. If you execute your bat script which applies to all files recursively under somewhere like C:, you've just renamed every file on your computer and it will immediately stop working and fail to boot. You'll have to do an full OS reinstall. Always have a backup!

First you'll have to decide do you want the batch file to work on a single file? To work on all files in a directory? Or do have it done recursively (all files/folders under a directory). Here are some pointers:

Batch file to replace all underscores _ with the letter M to all files in current directory

Put this in a batch file named change_underscores_in_this_directory.bat

@echo off
setlocal enabledelayedexpansion
for %%a in (*_*) do (
  set file=%%a
  ren "!file!" "!file:_=M!"

Execute it, and all the files in that directory with an underscore will be changed to an 'M'.

Use a Batch File to replace spaces with nothing (removing the spaces):


Use a Batch File to replace spaces with underscores, Recursively:



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.


Found it on stackoverflow:


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))
    WScript.Echo "Folder not found."
  End If
  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, "_")
  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
End Sub
  • 1
    this seems to be removing, rather than replacing, although I am sure it could be amended reasonably easily
    – AdamV
    Nov 5, 2009 at 2:43
  • the post you link to includes instructions for running the script, it would be helpful if you included that info in your answer. Nov 5, 2009 at 3:21

12noon has a FREE utility to do mass file renaming with full regular expression support, which is pretty cool. "Name Twister" info page with links to download

I haven't used this one in anger, but have used other apps of theirs (especially display changer) and been really happy.


You can use the below Power shell script to achieve the desired result

# Set the path to the directory containing the files
$dirPath = "D:\COMLOGS\_traffic-files_\ZETTA-T1 - Copy\"

# Get a list of all .txt files in the directory
$fileList = Get-ChildItem -Path $dirPath -Filter *.rp

# Loop through each file and rename it
foreach ($file in $fileList) {
    $newName = $file.Name.Replace("_", "M")
    Rename-Item -Path $file.FullName -NewName $newName

You must log in to answer this question.

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