I have a Win2k3 server that has a whole bunch of filenames that need renamed. Basically, I just need all - (dashes) replaced with _ (underscores), no matter where they are in the string. 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. Would love to do this in a command shell and not have to download a renamer or any add'l software. Thanks!

link|improve this question

80% accept rate
1  
You could simply rename them from your other computer over SMB. No need to copy files just for a name change. – Joey Nov 5 '09 at 7:23
feedback

3 Answers

up vote 5 down vote accepted

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.

link|improve this answer
1  
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 '09 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/…) – Murdoch Ripper Nov 5 '09 at 3:55
Nice!!! I have a one-liner on my mac that I used, was hoping for one in Windows. NICE JOB! – Matt Rogish Nov 5 '09 at 14:07
I edited it for a little correction (I had problems with spaces inside the filenames) – Daniel Mošmondor Mar 13 at 21:09
feedback

Found it on stackoverflow:

http://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
link|improve this answer
1  
this seems to be removing, rather than replacing, although I am sure it could be amended reasonably easily – AdamV Nov 5 '09 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. – quack quixote Nov 5 '09 at 3:21
feedback

12noon has a FREE utility to do mass file renaming with full regular expression support, which is pretty cool. Rename Reg Ex 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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.