Your original question was answered but it appears that this thread has
turned into a general discussion on finding and possibly renaming files
with special characters in the name. Here is a script which seems to
function properly in doing that. It deals only with individual
components of the name; checking first file names, then directory names
if configured to, then descends into subdirectories if configured to do so.
The script begins at the directory specified in the variable "$root"
which can be changed to take a path from the command line. It changes to
this directory and deals only with that level of the path. If configured
to descend it will again read the directory to make sure it gets any
changed names.
This script is also available at <
https://gist.github.com/1497826>.
@Echo OFF
::EnableDelayedExpansion just to show that we can.
SetLocal EnableExtensions EnableDelayedExpansion
For /F "tokens=1 delims==" %%a in ('Set "$" 2^>NUL:') Do (
Set "%%a="
)
:: For demo:
Set "$RENAME=Echo Rename"
:: For real:
Set "$RENAME=Rename"
:: Begin here:
Set "$root=%TEMP%"
::Set "$root=%*"
Set "$level=3"
:: $verbose: If DEFINED prints current directory.
::Set "$verbose=1"
ChDir /D %$root%
if DEFINED $verbose ChDir
Call :renameBadComponents
Goto :EOF
:renameBadComponents directory
SetLocal EnableExtensions DisableDelayedExpansion
:: First files:
If "%$level%" LSS "1" Goto :EOF
For /F "delims=" %%a in ('DIR /b /a-d 2^>NUL:') Do (
Set "$current=%%a"
Call :changeEXCAMP
Call :changePER
Call :checkAndRename
)
:: Second directories:
If "%$level%" LSS "2" Goto :EOF
For /F "delims=" %%a in ('DIR /b /ad 2^>NUL:') Do (
Set "$current=%%a"
Call :changeEXCAMP
Call :changePER
Call :checkAndRename
)
:: Third descend into subdirectories:
If "%$level%" LSS "3" Goto :EOF
For /F "delims=" %%a in ('DIR /b /ad 2^>NUL:') Do (
SetLocal
ChDir %%a
If DEFINED $verbose ChDir
Call :renameBadComponents
EndLocal
)
Goto :EOF
:changeEXCAMP
Set "$changed=%$current:!=-EXC-%"
Set "$changed=%$changed:&=-AMP-%"
Goto :EOF
:changePER
SetLocal EnableExtensions EnableDelayedExpansion
Set "$changed=!$changed:%%=-PER-!"
EndLocal & Set "$changed=%$changed%" & Goto :EOF
:checkAndRename
SetLocal EnableExtensions DisableDelayedExpansion
if "%$current%" NEQ "%$changed%" (
If EXIST "%$changed%" (
Set "$changed=%$changed%~"
Call %0
Goto :EOF
)
Echo Renaming "%$current%" to "%$changed%".
%$RENAME% "%$current%" "%$changed%"
Set "$current=%$changed%"
)
Goto :EOF