Back

Batchfile batch script garbled solution

Sometimes we may encounter garbled characters when running Batchfile

Table of contents

TL;DR / [Geek Summary]:

  • Encoding mismatch is the root of all evil; it’s a battle between ANSI and UTF-8.
  • Brute Force: Simply Save As ANSI using Notepad—quick, dirty, and effective.
  • Elegant Upgrade: Inject the chcp 65001 command at the script’s header to force the code page to UTF-8.

# question

The bat batch processing script is as follows

1
2
3
4
5
6
@echo off

echo hello,world.
echo haha

pause

When CMD executes the script, Chinese garbled characters appear.

# analyze

  1. Garbled characters are all related to character encoding. .

  2. Computers can only process numbers. If text is encountered, it must be converted to a number before processing. This is where character encodings (character sets) come in. . If you use a certain character encoding for encoding, you must use the same character encoding for decoding. If different character encodings are used, garbled characters will appear. .

# solve

# Convert the encoding of script files

By default, in the bat script file, if the Chinese is not ANSI encoding, garbled characters will appear. Therefore, you can use the Notepad that comes with the Windows operating system platform to open the script file, click the menu [File] - [Save As], and select ANSI encoding to save.

After converting the encoding and running it again, the Chinese garbled problem was solved.

# Change code

Declare changes to the previous code in the original bat script file

1
chcp 65001

# other

  1. Common code page mapping
code pagemapped character set
936GB2312
20127US-ASCII
65001UTF-8

转载需要保留原始链接,未经明确许可,禁止商业使用。CC BY-NC-SA 4.0

Last updated on 2026-05-17 16:53 UTC