| Author |
Message |
|
dbenham
Expert
Joined: 12 Feb 2011 21:02 Posts: 888 Location: United States (east coast)
|
 Generate nearly any character, including TAB, from batch
One of my early posts on DosTips was a :chr routine that could convert a number into the corresponding character. The final code that appears near the end of the thread works really well, and is fast, but nearly all the characters must be embedded in the script. The TAB character in particular can be problematic, especially when trying to post the code on a forum site. There is the undocumented %=ExitCodeAscii% dynamic variable that converts the most recent error code (from EXIT, not EXIT /B), but it is limited to codes between 32 and 126. I had never seen a native method to generate a TAB character that works on all Windows versions - until now I recently ran across this FORFILES documentation gem at SS64.COM: SS64.COM wrote: To include special characters in the command line, use the hex code for the character in 0xHH format (ex. 0x09 is theTAB character, 0x22 is the double quote " character.)
Eureeka  - The following simple printHex routine can generate any byte code value except 0x00 (nul), 0x0A (newline), and 0x0D (carriage return). Code: @echo off setlocal
::Define a Linefeed variable set LF=^
::above 2 blank lines are critical - do not remove.
::Create a TAB variable call :hexprint "0x09" TAB
::Print a string with encoded TABs call :hexprint "A0x09B0x09C"
::Create a string variable with encoded TABs call :hexprint "A0x09B0x09C" var set var
exit /b
:hexPrint string [rtnVar] for /f eol^=^%LF%%LF%^ delims^= %%A in ( 'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(%~1"' ) do if "%~2" neq "" (set %~2=%%A) else echo(%%A exit /b
I'll be playing Native American style flutes at a retreat in the Blue Ridge Mountains of Virginia for the next 4 days  , without computer access  I wanted to post this before I left. Enjoy the code, while I enjoy the crisp fall air. Dave Benham
Last edited by dbenham on 11 Oct 2012 04:30, edited 1 time in total.
|
| 10 Oct 2012 21:46 |
|
 |
|
jeb
Expert
Joined: 30 Aug 2007 08:05 Posts: 567 Location: Germany
|
 Re: Generate nearly any character, including TAB, from batch
Hello Dave, it's awesome, fantastic and so simply  And it was always there, but I never read the help of FORFILES (I had never used it before). I'm suprised that nobody else saw this simple usage before jeb
|
| 11 Oct 2012 00:33 |
|
 |
|
Squashman
Joined: 23 Dec 2011 13:59 Posts: 1000
|
 Re: Generate nearly any character, including TAB, from batch
I think why we all over looked it for so many years is that FORFILES wasn't really native to the OS unless you installed the Server Resource Kits. The extent of my use with FORFILES has always been using the date option. I guess this solves ED's SUB character question. viewtopic.php?f=3&t=2863Pretty output. (SUBS with TABS)
|
| 11 Oct 2012 05:55 |
|
 |
|
dbenham
Expert
Joined: 12 Feb 2011 21:02 Posts: 888 Location: United States (east coast)
|
 Re: Generate nearly any character, including TAB, from batch
FORFILES is native to XP and beyond - Yes? / No?
Dave Benham
|
| 11 Oct 2012 06:17 |
|
 |
|
foxidrive
Joined: 10 Feb 2012 02:20 Posts: 2467
|
 Re: Generate nearly any character, including TAB, from batch
No, it is not native to XP. http://technet.microsoft.com/de-de/libr ... 10%29.aspxWindows Server 2008, Windows Vista and later I think.
|
| 11 Oct 2012 06:26 |
|
 |
|
Squashman
Joined: 23 Dec 2011 13:59 Posts: 1000
|
 Re: Generate nearly any character, including TAB, from batch
Was surprised nobody did this already but here goes. Code: @echo off setlocal
::Define a Linefeed variable set LF=^
::above 2 blank lines are critical - do not remove.
FOR %%G IN (0 1 2 3 4 5 6 7 8 9 A B C D E F) DO ( FOR %%H IN (0 1 2 3 4 5 6 7 8 9 A B C D E F) DO ( IF NOT %%G%%H==00 call :hexprint "0x%%G%%H" ) )
Pause
exit /b
:hexPrint string [rtnVar] for /f eol^=^%LF%%LF%^ delims^= %%A in ( 'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(%~1"' ) do if "%~2" neq "" (set %~2=%%A) else echo(%%A exit /b HEX 07 actually sounded the BELL through my speaker. That was kind of cool. HEX 3C 3E & 7C all gave an error message of "The Syntax of the command is incorrect." Which really doesn't surprise me.
|
| 15 Nov 2012 17:03 |
|
 |
|
foxidrive
Joined: 10 Feb 2012 02:20 Posts: 2467
|
 Re: Generate nearly any character, including TAB, from batch
In Windows 8 it returns all the filenames in the folder, for each call. Changing the forfiles /p syntax to this 'forfiles /p "." /m "%~nx0" /c "cmd /c echo(%~1"' or this fixed it. 'forfiles /m "%~nx0" /c "cmd /c echo(%~1"' This version prints the hex value and the character, and press a space for every one. Code: @echo off setlocal
::Define a Linefeed variable set LF=^
::above 2 blank lines are critical - do not remove.
FOR %%G IN (0 1 2 3 4 5 6 7 8 9 A B C D E F) DO ( FOR %%H IN (0 1 2 3 4 5 6 7 8 9 A B C D E F) DO ( IF NOT %%G%%H==00 call :hexprint "0x%%G%%H" ) )
Pause
exit /b
:hexPrint string [rtnVar] for /f eol^=^%LF%%LF%^ delims^= %%A in ( 'forfiles /p "." /m "%~nx0" /c "cmd /c echo(%~1"' ) do if "%~2" neq "" (set %~2=%%A) else echo %1 "%%A" pause>nul exit /b
|
| 15 Nov 2012 17:59 |
|
 |
|
carlos
Joined: 20 Aug 2010 13:57 Posts: 88
|
 Re: Generate nearly any character, including TAB, from batch
This is a similar implementation: Code: @ECHO OFF :HexPrint SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION FOR /F delims^=^ eol^= %%# in ( 'FORFILES /m "%~nx0" /c "%ComSpec% /cECHO(%~1"' ) Do (Echo(%%#) GOTO :EOF
I look that the x should be in lowe case and the number in a width of two, these mean: that for example 0X1 not works, but 0x01 works.
Last edited by carlos on 15 Nov 2012 21:24, edited 1 time in total.
|
| 15 Nov 2012 20:31 |
|
 |
|
Squashman
Joined: 23 Dec 2011 13:59 Posts: 1000
|
 Re: Generate nearly any character, including TAB, from batch
Well thought it would be neat to create a 16 by 16 table of all the characters. I cheated a bit because the function cannot output HEX 0A 0D 26 3C 3E 5E 7C. It failed miserably. Code: @echo off setlocal
::Define a Linefeed variable set LF=^
::above 2 blank lines are critical - do not remove. echo. 0 1 2 3 4 5 6 7 8 9 A B C D E F >characters.txt FOR %%G IN (0 1 2 3 4 5 6 7 8 9 A B C D E F) DO ( set /p "=%%G " >>"characters.txt" <nul FOR %%H IN (0 1 2 3 4 5 6 7 8 9 A B C D E F) DO ( setlocal EnableDelayedExpansion set special=N IF %%G%%H==00 SET special=Y &&set /p "=00">>"characters.txt" <nul IF %%G%%H==0A SET special=Y &&set /p "=LF">>"characters.txt" <nul IF %%G%%H==0D SET special=Y &&set /p "=CR">>"characters.txt" <nul IF %%G%%H==26 SET special=Y &&set /p "=& ">>"characters.txt" <nul IF %%G%%H==3C SET special=Y &&set /p "=< ">>"characters.txt" <nul IF %%G%%H==3E SET special=Y &&set /p "=> ">>"characters.txt" <nul IF %%G%%H==5E SET special=Y &&set /p "=^ ">>"characters.txt" <nul IF %%G%%H==7C SET special=Y &&set /p "=| ">>"characters.txt" <nul IF !special!==N call :hexprint "0x%%G%%H" endlocal ) echo.>>"characters.txt" )
Pause
exit /b
:hexPrint string [rtnVar] for /f eol^=^%LF%%LF%^ delims^= %%A in ( 'forfiles /p "." /m "%~nx0" /c "cmd /c echo(%~1"' ) do if "%~2" neq "" (set "%~2=%%A") else set /p "=%%A ">>"characters.txt" <nul
exit /b output. Code: 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 00 LFCR 1 2 # $ % & ' ( ) * + , - . / 3 0 1 2 3 4 5 6 7 8 9 : ; < > ? 4 @ A B C D E F G H I J K L M N O 5 P Q R S T U V W X Y Z [ \ ] ^ _ 6 ` a b c d e f g h i j k l m n o 7 p q r s t u v w x y z { | } ~ 8 € ‚ ƒ „ … † ‡ ˆ ‰ Š ‹ Œ Ž 9 ‘ ’ “ ” • – — ˜ ™ š › œ ž Ÿ A ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ® ¯ B ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ C À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï D Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß E à á â ã ä å æ ç è é ê ë ì í î ï F ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ
EDIT: Just realized that you can't paste some of the special characters into the forum. Here is a link to a screenshot of the output. https://docs.google.com/open?id=0B3sIKf ... WpwbzVOYm8
|
| 15 Nov 2012 21:06 |
|
 |
|
Squashman
Joined: 23 Dec 2011 13:59 Posts: 1000
|
 Re: Generate nearly any character, including TAB, from batch
I remember now that things will display differently based on the code page and the font you are using. CMD window is using codepage 437 and a raster font and Notepad was using Courier new and Windows is set for code page 1252. But that doesn't explain why the equals sign doesn't output to my file. Does the set statement not like the equals sign?
|
| 15 Nov 2012 22:01 |
|
 |
|
Aacini
Expert
Joined: 06 Dec 2011 22:15 Posts: 400 Location: México City, México
|
 Re: Generate nearly any character, including TAB, from batch
dbenham wrote: I had never seen a native method to generate a TAB character that works on all Windows versions - until now FORFILES is native to XP and beyond - Yes? / No? No, it's not. However, you must note that FORFILES is an EXTERNAL command provided in FORFILES.EXE file. If the use of an external .exe file is accepted as suitable solution for this problem, then the same could be done with other .exe files, I think... In this post I released my Show.exe auxiliary program that can display any Ascii character given its Ascii code; search for it under 2- Show.exe.hex. This program is just 2048 bytes long, so it run very fast. I don't know the size of FORFILES.EXE file, but I am pretty sure that it is much larger. This is Show.exe example program displayed in that post: Code: @echo off setlocal EnableDelayedExpansion set Num1=1 set Num2=2 Show "Result one: " Num1 9 9 "Result two: " Num2 13 10 "Next line" 13 10 set LF=^
for /F "tokens=1-4 delims= " %%a in ('Show 7 32 8 32 9 32 13 32') do ( set "BEL=%%a" & set "BS=%%b" & set "TAB=%%c" & set "CR=%%d" ) echo BEL: %BEL% echo BS: One%BS%Two echo TAB: One%TAB%Two echo LF: One!LF!Two echo CR: One!CR!Two Show "Result one: " Num1 TAB TAB "Result two: " Num2 CR LF "Next line" CR LF
Output: Code: Result one: 1 Result two: 2 Next line BEL: BS: OnTwo TAB: One Two LF: One Two Two One Result one: 1 Result two: 2 Next line
This is AsciiTable.bat: Code: @echo off setlocal EnableDelayedExpansion set Hexa=0123456789ABCDEF for /L %%c in (0,1,15) do set C%%c=%%c echo 0 1 2 3 4 5 6 7 8 9 A B C D E F for /L %%r in (0,1,15) do ( show "!Hexa:~%%r,1!" 32 !C0! 32 !C1! 32 !C2! 32 !C3! 32 !C4! 32 !C5! 32 !C6! 32 !C7! 32 !C8! 32 !C9! 32 !C10! 32 !C11! 32 !C12! 32 !C13! 32 !C14! 32 !C15! 13 10 for /L %%c in (0,1,15) do set /A C%%c+=16 )
The output of previous program is the same of Squashman's code above, but with all characters. Antonio
|
| 17 Nov 2012 14:09 |
|
 |
|
Sponge Belly
Joined: 01 Oct 2012 13:32 Posts: 40 Location: Ireland
|
 Re: Generate nearly any character, including TAB, from batch
Hello All! Good news for those who don't have forfiles on their systems: MVP Daniel Petri wrote an article on the Windows 2000 Resource Kit that contains download links to many of the tools in said kit, including forfiles. But the link is to an early release that doesn't do the hex escapes. Fortunately, this obscure webpage has a link to a more full-featured version. Hope you find this useful. - SB
Last edited by Sponge Belly on 24 Feb 2013 09:11, edited 1 time in total.
|
| 03 Feb 2013 17:28 |
|
 |
|
Squashman
Joined: 23 Dec 2011 13:59 Posts: 1000
|
 Re: Generate nearly any character, including TAB, from batch
Sponge Belly wrote: Hello All! Good news for those who don't have forfiles on their systems: MVP Daniel Petri wrote an article on the Windows 2000 Resource Kit that contains download links to many of the tools in said kit, including forfiles. But the link is to an early release that doesn't do the hex escapes. Fortunately, this obscure webpage has a link to a more full-featured version. Hope you find this useful.  - SB That is still an older version of Forfiles which uses a dash for the switch syntax. If you want the newer version you can get it from here. http://www.sharedcache.com/cms/tips_and_tricks.aspx
|
| 04 Feb 2013 07:50 |
|
 |
|
Sponge Belly
Joined: 01 Oct 2012 13:32 Posts: 40 Location: Ireland
|
 Re: Generate nearly any character, including TAB, from batch
Thanks Squashman! Good find.
|
| 04 Feb 2013 08:41 |
|
 |
|
dbenham
Expert
Joined: 12 Feb 2011 21:02 Posts: 888 Location: United States (east coast)
|
 Re: Generate nearly any character, including TAB, from batch
I've posted blazing fast macros that convert between numeric ASCII code and character values in either direction at Batch macros to convert between ASCII code and character. The macros work natively on all modern versions of Windows, including XP. Dave Benham
|
| 09 Feb 2013 11:49 |
|
|