Multiple files located inside sub folders have similar names and sequential D.C. _ 18 CA 2616 –00000001.txt. Would like to find and replace the "en dash" char found right before 0000* with a hyphen.
e.g From D.C. _ 18 CA 2616 –00000001.txt to D.C. _ 18 CA 2616 -00000001.txt
Already tried...
@echo off
Setlocal enabledelayedexpansion
Set "Pattern=–"
Set "Replace=-"
For /R %%a in (*–*) Do (
set "File=%%~nxa"
Ren "%%a" "!File:%Pattern%=%Replace%!"
)
Pause&Exit
This is a very tricky task as the en dash character is Unicode code point U+2013. In default Windows command processor, it uses the 437 console code page. The
ûcharacter is character number 150 in code page 437. So it looks like one process is writing a file in code page 1252, while another is reading it using code page 437. - Better explanation about issue hereTo fix this, we can use
chcp 1252at the beginning of the script to change the the 437 console code page to 1252.Since we are dealing with Unicode characters, if you attempt to paste the
ûinto a basic text-file editor, it will change it stright to a en dash. To get around this, I'm using the editor notepad++ and changing the encoding in the editor (Encoding, Character sets, Western European, OEM-US) to allow the editor to paste in theûcharacter. - More explanation about issue here.ChangeEnDashToDash.bat:
The code bellow will change any
–to-simply by using syntax-replacement. To get the.txtfiles we can use aforloop along withdir "*.txt" /b. Then by usingEcho %%A^| find /i "û"it will display only the file names with–. From there we can set a few strings and rename them.If you wish to also search sub-directories change
dir "*.txt" /btodir "*.txt" /b /s. I have left someremstatements to help explain the script process.For help on any of the commands do the following:
set /?for /?if /?ren /?