UncleBob
UncleBob

Reputation: 1391

Command line argument from batch file containing UTF-8 character causes trouble

I wrote a little utility that takes a path as a command line argument, but I am running into trouble if (and only if) that argument is passed from a batch file and contains UTF-8 characters like Umlauts (which is unfortunately rather common when the system language is german).

I can pass a file name with Umlaut from the projects debug properties, no problem. I can pass a file name with Umlaut from the command line, no problem. I pass a file name with Umlaut from a batch file, mayhem.

Obviously, I have tried saving the batch file UTF-8 encoded... but then the darn thing won't execute any commands whatsoever.

The actual program is written in C#. What options do I have to process these arguments correctly, either in the batch file or code-side?

Upvotes: 3

Views: 3481

Answers (3)

Jeppe Sörensen
Jeppe Sörensen

Reputation: 1

If someone finds this, and is on Windows 10 or newer, here is an update:

chcp 65001 will set cmd to utf-8.

Don't know if this will work in all scenarios, but it helped me.

Upvotes: 0

Kryptonian
Kryptonian

Reputation: 870

you can use PowerShell scripts for UTF 8.The batch processor supports ASCII files only.

Upvotes: 0

C4d
C4d

Reputation: 3272

Use CHCP 1252 as the first line of your batch and it will work with paths.

Edit: Just tested it. It will work. Parameters are working where getting the right output (by echo) wont.

Anyway, if you try to echo out something like "öäü" it will not be displayed correctly.

To fully get around it, get yourself an editor which is able to save your file in ASCII-Encoding (notepad++ cant from my knowledge).

Upvotes: 5

Related Questions