How can I convert a directory of pre-existing text from Unicode to ANSI with a batch file? Is there some tool that I can use to loop through the files and perform the conversion?
Asked
Active
Viewed 1.2k times
4
-
Unicode... do you mean UTF-8? Or something else? – Brad Jan 08 '12 at 21:14
2 Answers
5
See https://superuser.com/questions/27060/batch-convert-files-for-encoding-or-line-ending. Specifically, iconv looks exactly like what you're looking for.
You're looking for the reverse of the example posted there, so you'd want something like this:
$ iconv -f utf-8 -t windows-1252 infile > outfile
5
EDIT - The following will convert UTF-16 with BOM. I don't think it works with any of the other UTF formats. I know it doesn't work for UTF-8. I'm not sure about UTF-32 with BOM
for %%F in (*.txt) do type "%%F" >"%%~nF.converted"
If run from the command line then use single percent %
instead of double percent %%
.
After you verify the converted files are correct, you can
del *.txt
ren *.converted *.txt

dbenham
- 127,446
- 28
- 251
- 390
-
1
-
1@Vadzim - DOS (the OS) is virtually non-existent now, The Windows command line and batch is ***not*** DOS. And there is no such thing as DOS encoding. DOS (and Windows command line) support many different code pages, each with its own encoding. I understand what you meant, but that was the entire point of the question in the first place. – dbenham Jun 04 '17 at 13:48
-
I mean that in case of OS in Russian locale the result would appear in CP866 instead of CP1251 that most would really need. – Vadzim Jun 05 '17 at 05:29