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
! 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.
:: 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" +
:: 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
) else (
rem Copy the path (and filename)
rem Check if it has a filename
if not [%%~ni]== (
rem If it has a filename, replace dots with dashes
rem Rename original file
ren "%%i" "!RENFN!%%~xi"
:: Exit spawned shell (no need to use setlocal to wipe out the envvar)
:: Test output:
:: C:\t> dir /b/a
:: super duper. .blah.ttt. omergerd.---.mp4
:: 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"