I'm trying to delete some files with unicode characters in them with batch script (it's a requirement). So I run cmd and execute:
> chcp 65001
Effectively setting codepage to 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 batch script and save it in UTF-8 encoding. But when I run nothing happens, literally nothing: not even echo works from batch script in this case. Even saving script in OEM encoding does not help.
So it seems that when I change codepage to UTF-8 in console, scripts just stop working. Does somebody know how to fix that?
Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.
chcp 65001 sets both InputEncoding and OutputEncoding to UTF8. [console]::OutputEncoding is affected as well.
What does 0 |% 0 Do in batch? %0|%0 is a fork bomb. It will spawn another process using a pipe | which runs a copy of the same program asynchronously. This hogs the CPU and memory, slowing down the system to a near-halt (or even crash the system).
When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.
If you want to have unicode supported in batch file, then CHCP on a line by itself just aborts the batch file. What I suggest is putting CHCP on each batch file line that needs unicode as follows
chcp 65001 > nul && <real command here>
Example: In my case I wanted to have a nice TAIL of my log files while debugging, but the content for even Latin-1 characters was being messed up. So here is my batch file which wraps the real tail implementation from Windows Resource Kit.
@C:\WINDOWS\system32\chcp.com 65001 >nul && tail.exe -f %1
In addition, for output to a console, you need to set a true type font, i.e. Lucidia Console.
And apparently for output to a file the command line needs to run as Unicode, so you would kick off your batch script as follows
cmd /u /c <batch file command here>
Disclaimer: Tested on Windows XP sp3 with Windows Resource Kit.
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