Search code examples
csvbatch-fileterminalspecial-charactersdos

BATCH : Change special characters from filenames (é è à...)


I'm trying to replace special characters from a string in batch. I've tried this :

set filename=%filename:é=e%
set filename=%filename:è=e%

But it doesn't work!

DOS doesn't understand any of these characters properly for some reason. Here's a screenshot (Note that the OUTPUT IN CSV is NOT part of the code. I just posted what was echo'd into the .csv just for you to see).

(Not Enough Reputation to post screenshots...) (https://i.sstatic.net/JSqBk.png)

So yeah, i have different characters in the program, in the terminal and also in the output file. Now i don't know what to do with special characters with accents. Optimal would be a single line of code that deletes them all from the string, no need to replace.

Something like :

 set filename=%filename:À à Ä ä Â â É é È è Ë ë Ê ê Ì ì Ï ï Î î Ò ò Ö ö Ô ô Ù ù Ü ü Û û=%

That would simply kill them all, and i would input this line before shooting the output into the .csv file. Would solve the problem but i can't get my hands on that function in BATCH (I've seen solutions in .NET or other languages but none in BATCH)


Solution

  • Batch can properly manage any Ascii character, that is, characters with code below or equal 255. This is obvious, because the Batch code itself must also be written in Ascii.

    @echo off
    set remove=áéíóúÜü
    set string=Aaá Eeé Iií Ooó UuÜüú It Works!
    for /F "tokens=1-26 delims=%remove%" %%a in ("%string%") do (
       set newString=%%a%%b%%c%%d%%e%%f%%g%%h%%i%%j%%k%%l%%m%%n%%o%%p%%q%%r%%s%%t%%u%%v%%w%%x%%y%%z
    )
    echo "%newString%"
    

    Output:

    "Aa Ee Ii Oo Uu It Works!"
    

    If the characters you want to remove are not Ascii (codes above 255), then a Batch program can not solve this problem.