7

Diagnosing why your batch file prints a garbage character, one character, and no...

 3 years ago
source link: https://devblogs.microsoft.com/oldnewthing/20210726-00/?p=105483
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Diagnosing why your batch file prints a garbage character, one character, and nothing more

Raymond Chen

Raymond

July 26th, 2021

You’ve written a batch file, and you try to execute it, but instead of running, it just prints some weird garbage character, then the first character of the batch file, and then that’s it.

Here’s the batch file:

@echo off
echo Hello, world.

And here’s what happens when you run it:

C:\>■@
'■@' is not recognized as an internal or external command,
operable program or batch file.
C:\>

What’s going on here?

Put on your thinking cap.

The file was saved in UTF-16LE format with a byte order mark. The leading garbage character was the byte order mark being interpreted in the ANSI code page.

But wait, you say. The UTF-16LE byte order mark is two characters long: 0xFF and 0xFE. Why did only one garbage character print?

Because character 0xFF is invisible.

The next Unicode character in the batch file is the at-sign, which in UTF-16LE is encoded as a @ followed by a null byte. The @ is read from the batch file, but the null causes the command processor to think it reached the end of the file.

That means that the batch file is treated as if it consisted of a single line. And that explains the error message.

Save the batch file as ANSI rather than UTF16-LE, and that will fix it.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK