Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Like the title says, I need help creating a batch file to replace "." with "-", however I need to perserve the file extensions and it needs to work through the subfolders. I found another post on here that got the effect I wanted but it also swapped the . in my file extensions. I am a completely ignorant regarding shell script so please be detailed in your response. I have no idea what any of the switches mean or even how to specify the path for that matter.

Thanks!

share|improve this question
2  
So you want "File name.ext" to become "File name-ext" (i.e. the file has no extension now)? Or do you have files like "File.name.ext" that should become "File-name.ext"? –  Karan Nov 9 '12 at 22:43
    
There are some good answers here, but please see also techsupportalert.com/best-free-rename-utility.htm Ifor (free) applications which are designed to do this sort of thing –  Mawg Nov 10 '12 at 2:11
    
@Karan I have "File.name.ext" that should become "File-name.ext" –  Brandon Ogle Nov 12 '12 at 16:03

3 Answers 3

I completely agree with the other answer actually. A GUI mass renamer utility would be so much easier to use. However, just for fun I wrote the following batch file that should recursively rename all files in the current dir. and subdirs. and replace . with - (so for example "Long .file. .name.ext" will become "Long -file- -name.ext"):

@echo off
setlocal enabledelayedexpansion
for /r %%f in (*.*) do (
    set fn=%%~nf

    REM Remove the echo from the following line to perform the actual renaming!
    if not [!fn!]==[] if not ["%%~nxf"]==["!fn:.=-!%%~xf"] echo ren "%%~f" "!fn:.=-!%%~xf"
)
pause

Run the batch file once, then if the output seems satisfactory perform the actual renaming by removing the word echo (second instance, not from the first line) and re-running the file.

share|improve this answer
    
That doesn’t work for files with only an extension like .htaccess –  Synetech Nov 10 '12 at 0:09
    
@Synetech: Since a file like that (blank filename, only extension) should be skipped, I just added an extra if condition. Should work now, thanks for the catch! –  Karan Nov 10 '12 at 0:23
    
like the code below this doesn't seem to be working, I am simply copying this into notepad and saving as a batch file, then throwing it in my parent directory. When I look at the command prompt while its running it doesnt seem to be renaming. Also the window closes after execution and I dont get an end output window –  Brandon Ogle Nov 12 '12 at 16:40
    
The window closes after execution because you're directly running the batch file from Explorer and not the Command prompt. If you want you can add an extra line saying pause at the end to keep the window open (actually I just added it above), or run the batch file from the Command prompt after changing to the directory. Also, for your safety I have disabled renaming by default. I mentioned it in the answer above, if the output is satisfactory, remove the echo before the ren and run again to do the actual renaming. –  Karan Nov 12 '12 at 17:16
1  
BTW, Synetech mentioned Bulk Rename Command above. You can also use ReNamer to set up complex renaming rules and/or scripts using a simple GUI, then call the saved presets via the command line. –  Karan Nov 12 '12 at 18:56

I somehow fail to understand why you want to bother with a batch file then. Why don't you go with one of a bazillion GUI rename tools like:

http://www.beroux.com/english/softwares/renameit/

If that particular one does not float your boat, check out this parade:

http://www.techsupportalert.com/best-free-rename-utility.htm

share|improve this answer
    
I have an application that downloads thousands of stock tickers on a daily basis and formats different class shares with a period. However I am using another application to develop a trading strategy, and it is incompatible with the periods in the file names. I can set up my data downloader to run a batch script on completion to fix the file naming error. Therefore taking sometime in the beginning to get a working script is going to save me time from having to use a GUI application on a daily basis to accomplish the task. –  Brandon Ogle Nov 12 '12 at 18:33
    
Ok, at half of your comment i got why you wanted a batch. :-) i guess the ideal way would have been to ask for an automated or schedulable way. you dont need to do it in batch, you could use all kinds of languages or those command line tools Synetec was talking about. But i guess you got more than one viable answer, so all's good. :-) –  TheUser1024 Nov 12 '12 at 20:43

Here’s my final, tested version of a batch file that can do what you want. It works with files with and without filenames or extensions, however files with filenames containing % or ! will cause trouble.

It uses delayed-expansion, so you must run it from a command-prompt with delayed expansion on (simply running setlocal /enabledelayedexpansion won’t cut it because that only toggles it if it’s already enabled; it has no effect if it’s not enabled when the command-prompt is run).

You can turn delayed-expansion on by opening the command-prompt with the /V:ON switch, but you can also do so from an existing command-prompt as in the batch-file below.

@echo off

:: This batch file (prints the command to) rename files so that
:: any dots (.) are replaced with dashes (-)
::
:: Note, files with names containing percents (%) and exclamantions (!)
:: will intefere with command-prompt syntax and are not supported, but
:: can be worked around: http://stackoverflow.com/questions/5226793/

:: If this batch-file has no parameters...
if [%1]==[] (
    :: Open a new command-prompt with delayed-expansion enabled and call self
    cmd /v:on /c "%0" +
    :: Quit
    goto :eof
)

:: Recurse through all files in all subdirectories
for /r %%i in (*) do (

    rem (:: cannot be used for comments in a FOR loop)
    rem Check if it has an extension
    if [%%~xi]==[] (
        rem If it has an extension, preserve it
        set RENFN=%%~nxi
    ) else (
        rem Copy the path (and filename)
        set RENFN=%%~ni
        rem Check if it has a filename
        if not [%%~ni]==[] (
            rem If it has a filename, replace dots with dashes
            set RENFN=!RENFN:.=-!
        )
    )

    rem Rename original file
    ren "%%i" "!RENFN!%%~xi"

)

:: Exit spawned shell (no need to use setlocal to wipe out the envvar)
exit

:: Test output:
::
:: C:\t> dir /b/a
::
:: .txt
:: blah
:: file.blah.txt
:: foo.bar.txt
:: super duper. .blah.ttt. omergerd.---.mp4
:: t.bat
::
:: C:\t> t.bat
::
:: ren "C:\t\.txt" ".txt"
:: ren "C:\t\blah" "blah"
:: ren "C:\t\file.blah.txt" "file-blah.txt"
:: ren "C:\t\foo.bar.txt" "foo-bar.txt"
:: ren "C:\t\super duper. .blah.ttt. omergerd.---.mp4" "super duper- -blah-ttt- omergerd----.mp4"
:: ren "C:\t\t.bat" "t.bat"
share|improve this answer
    
lol at omergerd. But this doesn't seem to be working, I am simply copying this into notepad and saving as a batch file, then throwing it in my parent directory. When I look at command prompt while its running it doesnt seem to be renaming. Also the window closes after execution and I dont get an end output window –  Brandon Ogle Nov 12 '12 at 16:35
    
No, it’s not renaming because of the echo command. I always use echo in front of my commands when writing a batch-file to see the results without actually making changes. That way, I can test and develop the batch-file without harm. Apparently Karan does the same thing. ☺ I’ve removed the echos. –  Synetech Nov 12 '12 at 17:36
    
As a side note, if you are not familiar with batch programming, then why are you using it to rename? Why not just use a GUI program? I recommend WildRename because it is powerful, yet easy to use—and free. –  Synetech Nov 12 '12 at 17:38
    
I have an application that downloads thousands of stock tickers on a daily basis and formats different class shares with a period. However I am using another application to develop a trading strategy, and it is incompatible with the periods in the file names. I can set up my data downloader to run a batch script on completion to fix the file naming error. Therefore taking sometime in the beginning to get a working script is going to save me time from having to use a GUI application on a daily basis to accomplish the task. –  Brandon Ogle Nov 12 '12 at 18:31
1  
Ah, that makes sense. Then yes, scripting an automated solution is good (though I’m sure there are advanced rename utilities that can be run from the command-line like Bulk Rename Command). –  Synetech Nov 12 '12 at 18:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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