Remove special characters from multiple files batch script solution works on everything but '~'
Posted on 2010-09-21
We have a client who needs to rename thousands of files in order to upload them to SharePoint and many of those files contain tildes(~).
I have used the script suggested in the related question to replace many different characters in a filename, but it doesn't work on tildes even if it is preceded by a ^ to make it a literal.
Here's the script from the other response. I modified it to allow a second parameter to pass in the desired character to replace. Whether I use a parameter or the literal ^~, it doesn't work:
if "%~1"=="" (
echo # ERROR - supply the directory from which to begin the search for
echo filenames containing the ampersand symbol. Any file
echo encountered with have the ampersand replaced by 'and'.
if not exist "%~1" (
echo # ERROR - directory NOT found
echo = '%~1'
echo + Parsing "%~1", please wait ...
for /f "tokens=*" %%F in ('dir "%~1" /s/b') do (
set newFILEname=!fileNAME:^%2= !
if not "!newFILEname!"=="!fileNAME!" (
for /f "tokens=*" %%N in ("!newFILENAME!") do (
echo + renaming "!fileNAME!" to "!newFILEname!"
ren "!fileNAME!" "%%~nxN"
if not errorlevel 1 (
echo - SUCCESS
) else (
echo # FAILED to rename "!fileNAME!"
echo - COMPLETE