Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a difference between the encoding of the Windows Command Prompt vs. a batch file?

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!

like image 423
jamesmus Avatar asked Oct 31 '08 09:10

jamesmus


People also ask

What is the difference between CMD file and batch file?

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.

Why does PowerShell encoding differ from VS Code's?

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.

Why does my batch file run under command com?

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.

What is the default encoding for vs code files?

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:


1 Answers

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.

like image 124
Tomalak Avatar answered Oct 05 '22 16:10

Tomalak