We’d like to remind Forumites to please avoid political debate on the Forum.
This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.
📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
Extended characters in batch files?
esuhl
Posts: 9,409 Forumite
in Techie Stuff
I'm writing a Windows batch script which outputs text to the console and to a log file.
I remember, from the days of DOS, there were characters in the "extended" ASCII range that could be used for drawing boxes, like the ones here:
https://en.wikipedia.org/wiki/Box_Drawing
I copied and pasted the box characters into my batch file and ran it. The log file uses the characters correctly; but the on-screen text has different accented letters appearing instead.
I found this article that explains that the problem is related to using different "code pages", but it doesn't say how to work round the issue.
https://blogs.msdn.microsoft.com/oldnewthing/20050308-00/?p=36253
Essentially, I want my batch file to be able to run on any Windows machine and produce the correct characters each time.
Is that possible?
I remember, from the days of DOS, there were characters in the "extended" ASCII range that could be used for drawing boxes, like the ones here:
https://en.wikipedia.org/wiki/Box_Drawing
I copied and pasted the box characters into my batch file and ran it. The log file uses the characters correctly; but the on-screen text has different accented letters appearing instead.
I found this article that explains that the problem is related to using different "code pages", but it doesn't say how to work round the issue.
https://blogs.msdn.microsoft.com/oldnewthing/20050308-00/?p=36253
Essentially, I want my batch file to be able to run on any Windows machine and produce the correct characters each time.
Is that possible?
0
Comments
-
I gave up on that problem years ago, because there seems to be different usage of character sets/code pages.
Now I even do boxes asecho +---------------+ echo I BATchFileName I echo +---------------+
because I can't guarantee the vertical bar | ...
Later: there's something here, perhaps worth a read?
Even later: when I do CHCP it shows that in Windows 10 Pro (at least!) the Command Prompt window is using Code Page 850 which has a limited range of box-drawing characters. See what you get by playing with this CP.
Don't forget you will need to escape the vertical bar by putting a carat before it, as in
echo ^|0 -
Thanks for the tips. I also asked this question on another thread and got some more information here if anyone is interested:
http://www.dostips.com/forum/viewtopic.php?f=3&t=77010 -
Interesting - but rather complicated for my decrepit brain...!0
-
dos also often used the ansi.sys device driver in config.sys
on top of this all the characters were fixed width courier 100 -
This is not worth the hassle.
You could detect Windows version (ver command) and run a different script for each version, but this really isn't worth the hassle.
You also have the issue that your only going to see this box if there is a delay or a pause command, pause is almost always pointless and "timeout" (delay command) doesn't work in windows versions before Win 7.“I may not agree with you, but I will defend to the death your right to make an a** of yourself.”
<><><><><><><><><<><><><><><><><><><><><><> Don't forget to like and subscribe \/ \/ \/0 -
Timeout?
Real BATch writers use
PING -4 -n 11 127.0.0.1 > nul
(for a 10-second delay)!0 -
Strider590 wrote: »This is not worth the hassle.
Quite possibly! But it's interesting, nonetheless...Strider590 wrote: »You could detect Windows version (ver command) and run a different script for each version, but this really isn't worth the hassle.
But... the Windows version wouldn't by itself indicate how to display a box character -- it would also depend on the code page in use.Strider590 wrote: »You also have the issue that your only going to see this box if there is a delay or a pause command, pause is almost always pointless and "timeout" (delay command) doesn't work in windows versions before Win 7.
I use "timeout" commands, which are definitely not pointless, but I didn't realise it only works in Win7 and later. I guess I'll try the ping workaround above, but...Timeout?
Real BATch writers use
PING -4 -n 11 127.0.0.1 > nul
(for a 10-second delay)!
Why does pinging the loopback interface eleven times cause a ten second delay...?! Does the ping command automatically wait one second before issuing a subsequent ping?
EDIT: I just found this page which explains various ways of getting a batch script to pause execution. It also shows the use of a ping command, but in a somewhat different manner.
http://www.robvanderwoude.com/wait.php
EDIT2: Apparently "timeout" works from Vista onwards. That might be good enough for me, although if I can get the script working with XP too, why not.
https://technet.microsoft.com/en-us/library/cc754891(v=ws.11).aspx0 -
Aha! Did you do physics at skool?! And did you have to time an event by counting, starting at zero, then one, two, etc?Why does pinging the loopback interface eleven times cause a ten second delay...?! Does the ping command automatically wait one second before issuing a subsequent ping?
Think of a set of fence posts, each a metre apart.
For a fence 10 metres long you need 11 fence posts.
For PING, the number of PINGs corresponds to the number of fence posts, since a PING is issued immediately, then 10 more at 1 second intervals.
I believe this is what is called "a feature"!0 -
Code pages were more or less designed out with Unicode (2-bits per character) specifically because it was such a horrible cludge to use code pages for internationalisation in the first place. If you can munge your code to be unicode-friendly it may be another option0
-
Aha! Did you do physics at skool?! And did you have to time an event by counting, starting at zero, then one, two, etc?
Think of a set of fence posts, each a metre apart.
For a fence 10 metres long you need 11 fence posts.
For PING, the number of PINGs corresponds to the number of fence posts, since a PING is issued immediately, then 10 more at 1 second intervals.
I believe this is what is called "a feature"!
Ah, so I was right. How reliable is the 1-second interval? Is it documented as such on all Windows versions, or does it depend on other things?Code pages were more or less designed out with Unicode (2-bits per character) specifically because it was such a horrible cludge to use code pages for internationalisation in the first place. If you can munge your code to be unicode-friendly it may be another option
I've discovered that I can use UTF-8 without a BOM (byte order mark) for the batch file. Echoing UTF-8 characters to a text file works fine. When Notepad opens the generated file, it assumes UTF-8 encoding (because of the presence of the UTF-8 encoded characters).
Unfortunately, the console runs in ANSI mode by default. I presume echoing to file works because the UTF-8 characters are simply treated as individual bytes -- the console doesn't need to recognise them as UTF-8.
However, this means that it's not possible to display UTF-8 characters on-screen by default. And displaying extended ANSI characters depends both upon the code page in use, and the way that the characters are interpreted in the batch file itself.
Also, I don't think I can guarantee that any particular code page will be available on every machine.
I discovered that you can start CMD .EXE with the /U switch to enable Unicode mode, and that Microsoft lists 65001 as the code page for UTF-8, but this doesn't seem to work either. And anyway, I'm not sure it would be easy to change to a new Unicode console from within a batch file -- I don't want users to have to do anything other than double-click the file to run it.
Hmmm...0
This discussion has been closed.
Confirm your email address to Create Threads and Reply
Categories
- All Categories
- 352.4K Banking & Borrowing
- 253.7K Reduce Debt & Boost Income
- 454.4K Spending & Discounts
- 245.4K Work, Benefits & Business
- 601.2K Mortgages, Homes & Bills
- 177.6K Life & Family
- 259.3K Travel & Transport
- 1.5M Hobbies & Leisure
- 16K Discuss & Feedback
- 37.7K Read-Only Boards
