ANSI / Unicode conflict in batch file

2.5k views Asked by At

I have some batch files that use a text file for language-independancy. Until yesterday all worked fine ... but then I began translating the standard texts to Dutch and German. Both languages use so called diacritical or accented characters like ä, ë, ö. I think Spanish will give the same problems with ñ. I created the text file with Notepad using standard encoding, which is ANSI. Just typing (DOS: TYPE) the file showed the wrong accented characters: e.g. ë showed as Ù. After I edited the text file and saved with Unicode encoding the DOS TYPE showed exactly what I typed in Notepad. At this point I thought my problem was solved ... but my batch code now shows me no text at all! All text is retrieved from the file by a batch file that looks like this (simplified):

@rem Parms %1 text type number             File %%a program name
@rem       %2 program name (double quoted)      %%b  - - filler (tabs)
@rem       %3 text number                       %%c text number
@rem       %4 replacement value - 1             %%d  - - filler (tabs)
@rem       %5 replacement value - 2             %%e text string
set TempText=
set TempType=
setlocal enabledelayedexpansion
@rem Read file until both values are set ...
for /f  "usebackq tokens=1,2,3,4,5 delims=|" %%a in ("%EnvPath%Text.txt") do (
    if /i   %%a==Tools (if /i %%c==%1 (set  TempType=%%e))
    if /i   %%a==%~2   (if /i %%c==%3 (set  TempText=%%e))
    if not  "!TempType!"=="" (if not "!TempText!"=="" (goto :Leave))
)
:Leave
endlocal & set TempText=%TempText%&set TempType=%TempType%

When ECHO is ON it shows that no lines are read from the file or the FOR-loop is never executed.

My question is: how can I make the FOR loop to read the Unicode texts?

2

There are 2 answers

0
noodlesstillalive On

Your problem is that cmd uses code page 850 (in the US it may be 437), type chcp to see. English Windows uses 1252 elsewhere.

GUI programs

ñ 0xf1

Console programs

ñ 0xa4

If you are on 32 bit use edit.exe (a msdos text editor). Else you can use Word and save as MSDos text.

0
Stephan On

three years late, but...

you can convert the file to ANSI "on the fly" with the type command:

...  %%a in ('type "%EnvPath%Text.txt"') do (