Diagnosing why your batch file prints a garbage character, one character, and nothing more
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.