Batch find and replace en dash with hyphen using Windows batch command

1.8k Views Asked by At

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
1

There are 1 best solutions below

2
John Kens On

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 here

To fix this, we can use chcp 1252 at 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:

@echo off
setlocal EnableDelayedExpansion
chcp 1252>NUL

Rem | Get Each File Name
for /f "tokens=*" %%A in ('dir "*.txt" /b') do (

    Rem | Get Each File Name
    for /f "tokens=*" %%B in ('Echo %%A^| find /i "û"') do (

        Rem | Save Original Name
        Set "OriginalName=%%B"

        Rem | Get New Name
        SET "ModifiedName=!OriginalName:û=-%!"

        Ren "!OriginalName!" "!ModifiedName!"

    )
)

goto :EOF

The code bellow will change any to - simply by using syntax-replacement. To get the .txt files we can use a for loop along with dir "*.txt" /b. Then by using Echo %%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" /b to dir "*.txt" /b /s. I have left some rem statements to help explain the script process.


For help on any of the commands do the following:

  • set /?
  • for /?
  • if /?
  • ren /?
  • So on.