Script package does not execute if chcp was called

I am trying to delete some files with unicode characters in them with a script package (this is a requirement). So I run cmd and execute:

> chcp 65001 

Efficient codepage setup for UTF-8. And it works:

 D:\temp\1>dir Volume in drive D has no label. Volume Serial Number is 8C33-61BF Directory of D:\temp\1 02.02.2010 09:31 <DIR> . 02.02.2010 09:31 <DIR> .. 02.02.2010 09:32 508 1.txt 02.02.2010 09:28 12 delete.bat 02.02.2010 09:20 95 delete.cmd 02.02.2010 09:13 <DIR> RΓΊn 02.02.2010 09:13 <DIR>  Ρ– 3 File(s) 615 bytes 4 Dir(s) 11 576 438 784 bytes free D:\temp\1>rmdir RΓΊn D:\temp\1>dir Volume in drive D has no label. Volume Serial Number is 8C33-61BF Directory of D:\temp\1 02.02.2010 09:56 <DIR> . 02.02.2010 09:56 <DIR> .. 02.02.2010 09:32 508 1.txt 02.02.2010 09:28 12 delete.bat 02.02.2010 09:20 95 delete.cmd 02.02.2010 09:13 <DIR>  Ρ– 3 File(s) 615 bytes 3 Dir(s) 11 576 438 784 bytes free 

Then I put the same rmdir commands in a batch script and save it in UTF-8 encoding. But when I run, nothing happens, literally nothing: even the echo does not work from the script package in this case. Even saving a script in OEM coding does not help.

It seems that when I change the code page to UTF-8 in the console, the scripts just stop working. Does anyone know how to fix this?

+7
cmd unicode batch-file
source share
3 answers

If you want Unicode to be supported in a batch file, CHCP on a separate line simply interrupts the batch file. I suggest putting CHCP on every line of the batch file that needs unicode, as shown below.

 chcp 65001 > nul && <real command here> 

Example. In my case, I wanted to have a good HALL of my log files during debugging, but the content for Latin-1 characters was messed up. So here is my batch file that wraps the real tail implementation from the Windows resource set.

 @C:\WINDOWS\system32\chcp.com 65001 >nul && tail.exe -f %1 

In addition, for output to the console, you need to install a font of the true type, that is, the Lucidia console.

And apparently, for the output to the file, the command line should run as Unicode, so you can run your batch script as follows

 cmd /u /c <batch file command here> 

Disclaimer: tested on Windows XP sp3 using the Windows Resource Kit.

+8
source share

Unicode support on the console and especially in batch files is pretty poor. Can you β€œflip” a requirement to say PowerShell or Active Scripting (VBScript or JScript)?

In the long run, this will save you a lot of grief (if you need to grow beyond this simple task)

Not to mention the fact that both PowerShell and ActiveScripting use more powerful languages, allowing functions, proper loops, real variables, debuggers, and many goodies for a more serious project.

0
source share

Try pasting an empty line in the first line in the batch file ...

Line 1:

Line 2: CHCP 65001

Line 3: script commmands

Must work!

0
source share

All Articles