I have a program I use to create a batch file. My problem is that the program's output is UTF-8 so as soon as any diacritical marks like é,à,?,? are in my batch file it fails.
It seems I can't figure out a way to convert my output to anything but UTF-8 in the program that creates the batch file.
So I was thinking of creating two bach files. The actual one and another that converts the actual one from UTF-8 to ANSI (Windows Codepage 1252, or maybe cp 850) and then executes it after that. Of course I'd add a chcp xxxx as the first command of the actual batch file.
So my question is is there an alternative to iconv on Windows - or how does one convert a UTF-8 text file to a windows codepage using a second batch file. Is there anything built into Win XP and up that I could use or is there a free and redistributable tool I might use for this?
Note:
chcp 65001
does not work for batch files.
EDIT 1:
on windows XP I created two batch files to test the first answer.
1.bat encoded to UTF-8 without BOM contains:
chcp 1252
cd ü??
2.bat also encoded to UTF-8 without BOM - but without any special characters contains:
chcp 1252
type "1.bat" >"ansi_file.bat"
The resulting ansi_file.bat created when one executes 2.bat will still be utf-8 encoded and not ansi encoded.
EDIT 2:
The mentioned reverse process works.
chcp 1252
echo ü > ansi.txt
cmd /u /c type ansi.txt > unicode.txt
but neither of the following subsequent lines
cmd /a /c type unicode.txt > back2ansi.txt
type unicode.txt > back2ansi_v2.txt
gets me back to ANSI. I tried this both on Win XP and Win 7.
Can anyone help?
NOTE:
I'm aware of how to use the Windows Script Host and VBS. I'd like to avoid depending on the script host though. The VBS method is detailed here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa368046%28v=vs.85%29.aspx
EDIT 3:
The text file created containing a unicode ü above is not utf-8
The Windows unicode file is HEX:
FC 00 20 00 0D 00 0A 00
UTF-8 without BOM would be HEX:
C3 BC 20 0D 0A
The VBS solution linked to only works with the unicode form but fails on the UTF-8 form.
I need to convert UTF-8 to another code page so not even that one seems to work for me...
See Question&Answers more detail:
os