Thanks to rojo the batch file below will check for illegal characters, replace them with a dash, search subdirectories and require a location argument, but when it's run it takes you to the directory you specify in the argument. I want to stay in drive/folder the batch file is run from. (ex: rename.bat c:\test, this will take you to c:\test after batch file runs)
@@echo off
IF "%~1"=="" goto Continue
pushd %1
setlocal enabledelayedexpansion
for /r %%I in (*) do (
set "file=%%~nxI"
if "!file:~0,1!"=="~" (
set "file=-!file:~1!"
)
for %%d in (# %%) do (
if not "!file!"=="!file:%%d=!" (
set "file=!file:%%d=-!"
)
)
if not "!file!"=="%%~nxI" (
echo %%~fI -^> !file!
ren "%%~fI" "!file!"
)
)
Exit /B
:Continue
@echo You need drive and directory at end or this batch file
At the end of your FOR /R
loop, add POPD
to "undo" the directory change performed by the respective PUSHD
command.
setlocal enabledelayedexpansion
pushd %1
for /r %%I in (*) do (
...
)
REM Revert back to the original directory.
POPD
ENDLOCAL
Exit /B
:Continue