China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-12 20:31
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] How to convert the UTF-8 encoding of a txt text file to Unicode encoding! View 6,908 Replies 17
Original Poster Posted 2007-11-30 10:10 ·  中国 广东 深圳 电信
新手上路
Credits 16
Posts 6
Joined 2007-11-30 09:41
18-year member
UID 104222
Gender Male
Status Offline
How to convert the UTF-8 encoding of a TXT text file to the Unicode encoding!!

Note: There are many files, and they can't be processed one by one. I want to convert the encoding because I need to open the TXT file directly with Excel, but opening a UTF-8 encoded text will become garbled, and it must be converted to a Unicode encoded text file. I can use Notepad to open the text file and then choose the encoding when "Save As", but there are too many files to operate one by one. See which expert can solve this problem!!!
Floor 2 Posted 2007-11-30 11:17 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
```@echo off
for /f "delims=" %%a in ('dir /s /b /a-d D:\test\*.txt') do (
call cmd /u /c type "%%a">"%%~dpna.U"
call echo move /y "%%~dpna.U" "%%a"
)
pause```
第一高手 第二高手

Floor 3 Posted 2007-11-30 11:32 ·  中国 广西 钦州 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
21-year member
UID 44210
Status Offline
It seems that batch processing can't directly handle text encoded in UTF-8.
Floor 4 Posted 2007-11-30 11:34 ·  中国 广东 深圳 电信
新手上路
Credits 16
Posts 6
Joined 2007-11-30 09:41
18-year member
UID 104222
Gender Male
Status Offline
It doesn't work. After running, several .u format files are generated. Opening them with Notepad is all garbled code, which is not the result I want!!! Experts, take a look again!!!
Floor 5 Posted 2007-11-30 11:35 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
Oh, really? I tested with ANSI.
第一高手 第二高手

Floor 6 Posted 2007-11-30 11:44 ·  中国 广东 深圳 电信
新手上路
Credits 16
Posts 6
Joined 2007-11-30 09:41
18-year member
UID 104222
Gender Male
Status Offline
No, the encoding should be selected as utf-8. I need to convert utf-8 to unicode. Just write a few sentences in Notepad, then save as, select the encoding as: utf-f, and save. Then run your program, and you can take a look.

[ Last edited by moonofwell on 2007-11-30 at 11:46 AM ]
Floor 7 Posted 2007-11-30 12:05 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
Since the zh159 batch processing can't directly handle UTF-8, there's no way around it. We have to rely on third-party tools.
第一高手 第二高手

Floor 8 Posted 2007-11-30 12:11 ·  中国 广东 深圳 电信
新手上路
Credits 16
Posts 6
Joined 2007-11-30 09:41
18-year member
UID 104222
Gender Male
Status Offline
I found a third - party tool, ConvertZ ver 7.40. It would be even better if it could be done with DOS batch processing or VBS! (*^_^*)
Floor 9 Posted 2007-11-30 13:21 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
UTF82Unicode.vbs
[code]
Set objArgs = WScript.Arguments
For I = 0 To objArgs.Count - 1
FileUrl = objArgs(I)
Call WriteToFile(FileUrl, ReadFile(FileUrl, "UTF-8"), "Unicode")
Next
Function ReadFile(FileUrl, CharSet)
Dim Str
Set stm = CreateObject("Adodb.Stream")
stm.Type = 2
stm.mode = 3
stm.charset = CharSet
stm.Open
stm.loadfromfile FileUrl
Str = stm.readtext
stm.Close
Set stm = Nothing
ReadFile = Str
End Function

Function WriteToFile (FileUrl, Str, CharSet)
Set stm = CreateObject("Adodb.Stream")
stm.Type = 2
stm.mode = 3
stm.charset = CharSet
stm.Open
stm.WriteText Str
stm.SaveToFile FileUrl, 2
stm.flush
Floor 10 Posted 2007-11-30 13:22 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
UTF82Unicode.vbs

Set objArgs = WScript.Arguments
For I = 0 To objArgs.Count - 1
FileUrl = objArgs(I)
Call WriteToFile(FileUrl, ReadFile(FileUrl, "UTF-8"), "Unicode")
Next
Function ReadFile(FileUrl, CharSet)
Dim Str
Set stm = CreateObject("Adodb.Stream")
stm.Type = 2
stm.mode = 3
stm.charset = CharSet
stm.Open
stm.loadfromfile FileUrl
Str = stm.readtext
stm.Close
Set stm = Nothing
ReadFile = Str
End Function

Function WriteToFile (FileUrl, Str, CharSet)
Set stm = CreateObject("Adodb.Stream")
stm.Type = 2
stm.mode = 3
stm.charset = CharSet
stm.Open
stm.WriteText Str
stm.SaveToFile FileUrl, 2
stm.flush
stm.Close
Set stm = Nothing
End Function


@echo off
for /f "delims=" %%a in ('dir /s /b /a-d D:\test\*.txt') do UTF82Unicode.vbs "%%a"
pause

Direct replacement mode, please test before applying
第一高手 第二高手

Floor 11 Posted 2007-11-30 13:30 ·  中国 广东 深圳 电信
新手上路
Credits 16
Posts 6
Joined 2007-11-30 09:41
18-year member
UID 104222
Gender Male
Status Offline
Okay, thanks buddy, you're really a master!!!
Floor 12 Posted 2007-12-07 17:06 ·  中国 上海 黄浦区 电信
初级用户
Credits 47
Posts 18
Joined 2007-09-08 21:55
18-year member
UID 96891
Gender Male
Status Offline
The LZ is converting UTF-8 encoding to Unicode.

And my question is:
How to convert UTF-8 to ANSI
And how to convert Unicode to ANSI

How to implement it with a batch file?
Please experts answer
Floor 13 Posted 2007-12-07 20:54 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
Command Line GB2312, UTF - 8, Unicode, BIG5... Encoding Conversion Tool VBS Version
http://www.cn-dos.net/forum/viewthread.php?tid=35986&fpage=1&highlight=

Text File Encoding Query Tool QueryCode.vbs
http://www.cn-dos.net/forum/viewthread.php?tid=36012&fpage=1&highlight=
第一高手 第二高手

Floor 14 Posted 2007-12-10 15:43 ·  中国 上海 黄浦区 电信
初级用户
Credits 47
Posts 18
Joined 2007-09-08 21:55
18-year member
UID 96891
Gender Male
Status Offline
Thanks to the upstairs

[ Last edited by gameyixiu on 2007-12-10 at 03:45 PM ]
Floor 15 Posted 2008-05-18 22:10 ·  中国 山东 烟台 联通
新手上路
Credits 2
Posts 1
Joined 2008-05-18 21:44
18-year member
UID 119048
Gender Male
Status Offline
Not bad, learning!
Forum Jump: