For example, suppose I have a batch file called 'test.cmd'
and it simply contains:
echo %1
I can call this directly from the command prompt with 'test.cmd some¬arg'
and the result is that the string 'some¬arg'
is printed.
However if I place that same call in a second batch file, called 'tester.cmd'
for the sake of argument, and I call this from the command prompt the result is that the string 'some%arg'
is printed.
What is it that messes up the encoding and how do I get around it? I am sure I've fixed this before, but I can't remember how...
Thanks!
CMD files are newer version came into existence for batch files. The older systems will not know about CMD files and will run them partially which will throw an error for the batch files saved as CMD.
If PowerShell's encoding differs from VS Code's, something can go wrong here. When a script that is open in VS Code references another script that is not open in VS Code, the extension falls back to loading that script's content from the file system.
We discovered that the reason the batch file ran under COMMAND.COM is that it was being started from a .PIF file (also ancient). Since the special memory configuration settings available only through a PIF have become irrelevant, we replaced it with a conventional desktop shortcut. The same batch file, launched from the shortcut, runs in CMD.EXE.
VS Code's default encoding is UTF-8 without BOM. To set VS Code's encoding, go to the VS Code settings (Ctrl +,) and set the "files.encoding" setting:
This is because your batch file is encoded in a different code page than cmd.exe is currently in.
In western default configurations, cmd.exe starts in CP850, but text editors usually work in CP1252 (what is often wrongly referred to as Latin-1 or ISO-8859-1).
The characters "¬" and "¼" share the same character code in these two code pages, "BC".
The solution is simple. Either encode your batch file in code page 850, or switch cmd.exe to code page 1252 by issuing chcp 1252
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With