Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

How do I read special characters in a file using a .bat file

I have a .bat file(batch file) and I want it to be able to read a file, but it has lots of special characters the code I currently have istype "C:UsersmehidDesktopCommand prompt gameStuff for coders ;)Map.txt" and the .txt file has ─ │ ┌ ┐ └ ┘ ├ ┤ ┬ ┴ ┼ and those types of characters it outputs this Γ?? Γ?é Γ?? Γ?é Γ?? Γ?? Γ?£ Γ?? Γ?? Γ?┤ Γ?╝

question from:https://stackoverflow.com/questions/65872432/how-do-i-read-special-characters-in-a-file-using-a-bat-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The most likely reason for your issue, is that your Map.txt file was saved with UTF8 encoding, and your batch file has not told cmd.exe to use anything other than the default codepage.

The simplest way to change that, other than changing the encoding of your text file, is to change your codepage to something which can read UTF8 characters, i.e. codepage 65001.

Example:

@Echo Off
SetLocal EnableExtensions

Rem Determining, and saving to a variable, your current codepage.
Set "codepage=" & For /F "Tokens=*" %%G In ('%SystemRoot%System32chcp.com'
) Do For %%H In (%%G) Do If Not %%~nH Equ 65001 (Set "codepage=%%~nH"
    %SystemRoot%System32chcp.com 65001 1> NUL)

Echo typing your text file using codepage 65001 & Echo(
Type "C:UsersmehidDesktopCommand prompt gameStuff for coders ;)Map.txt"
Pause

Rem Changing your codepage back to what it was previously.
If Defined codepage %SystemRoot%System32chcp.com %codepage% 1> NUL

Echo typing your text file using your original codepage, [%codepage%]. & Echo(
Type "C:UsersmehidDesktopCommand prompt gameStuff for coders ;)Map.txt"
Pause

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...