Thursday, January 31, 2019

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


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?


Answer



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.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...