Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for chr.bat

(view source code of chr.bat as plain text)

  1. @ECHO OFF
  2. :: Check Windows version (NT)
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. SETLOCAL ENABLEDELAYEDEXPANSION
  6.  
  7. :: Read the number of available columns in the console
  8. SET Columns=
  9. FOR /F "skip=2 tokens=*" %%A IN ('MODE CON ^| FIND ":"') DO (
  10. 	IF "!Columns!"=="" (
  11. 		FOR %%B IN (%%A) DO (
  12. 			SET Columns=%%B
  13. 		)
  14. 	)
  15. )
  16. IF %Columns% LSS 81 (
  17. 	SET NoCRLF=1
  18. )
  19.  
  20. :: Check if FORFILES.EXE is available
  21. SET UseForfiles=0
  22. FORFILES /? 2>NUL | FIND /I "0xHH" >NUL
  23. IF NOT ERRORLEVEL 1 SET UseForfiles=1
  24.  
  25. :: Check command line
  26. IF "%~1"=="" GOTO Syntax
  27.  
  28. :: Check Windows version (XP required if FORFILES not available)
  29. FOR /F "tokens=*" %%A IN ('VER') DO (
  30. 	FOR %%B IN (%%~A) DO (
  31. 		FOR /F "delims=]" %%C IN ("%%~B") DO (
  32. 			SET Ver=%%C
  33. 		)
  34. 	)
  35. )
  36.  
  37. :: Abort if neither FORFILES nor %=ExitCodeAscii% can be used
  38. IF %UseForfiles% NEQ 1 (
  39. 	IF %Ver% LEQ 5.1 (
  40. 		GOTO Syntax
  41. 	)
  42. )
  43.  
  44. :: Check if numeric and in the proper range, depending on the method used
  45. IF %UseForfiles% NEQ 1 (
  46. 	FOR %%A IN (%*) DO (
  47. 		SET /A Dummy=%%~A
  48. 		IF NOT "%%~A"=="!Dummy!" GOTO Syntax
  49. 		REM Check if in range 32..126
  50. 		IF %%~A LSS  32 GOTO Syntax
  51. 		IF %%~A GTR 126 GOTO Syntax
  52. 	)
  53. )
  54. IF %UseForfiles% EQU 1 (
  55. 	FOR %%A IN (%*) DO (
  56. 		ECHO.%%A| FINDSTR /R /X /C:"0x[0-9A-F][0-9A-F](0x[0-9A-F][0-9A-F])*" >NUL || GOTO Syntax
  57. 	)
  58. )
  59.  
  60. :: Save current code page and set code page to 437
  61. FOR /F "tokens=*" %%A IN ('CHCP') DO FOR %%B IN (%%A) DO SET CHCP=%%B
  62. CHCP 437 >NUL
  63.  
  64. :: Main code without FORFILES
  65. :: Set errorlevel, then translate it to ASCII character and display on screen without CR/LF.
  66. :: For characters 32, 34 and 61 we need to use a dirty trick to circumvent SET /P limitations.
  67. IF %UseForfiles% NEQ 1 (
  68. 	FOR %%A IN (%*) DO (
  69. 		CMD /C EXIT /B %%A
  70. 		IF %%A==32 (SET /P Dummy=^^^^ <NUL)
  71. 		IF %%A==34 (SET /P Dummy=^^^^"<NUL)
  72. 		IF %%A==61 (SET /P Dummy=^^^=<NUL)
  73. 		IF NOT %%A==32 IF NOT %%A==34 IF NOT %%A==61 (SET /P "Dummy=!=ExitCodeAscii!"<NUL)
  74. 	)
  75. )
  76.  
  77. :: Main code using FORFILES
  78. IF %UseForfiles% EQU 1 (
  79. 	FOR %%A IN (%*) DO (
  80. 		FORFILES /P %~dps0 /M %~nxs0 /C "CMD /C SET /P 0x22=%%~A0x22 0x3C NUL"
  81. 	)
  82. )
  83.  
  84. :: Restore code page
  85. CHCP %CHCP% >NUL
  86.  
  87. :: Done
  88. ENDLOCAL
  89. GOTO:EOF
  90.  
  91.  
  92. :Syntax
  93. ECHO Chr.bat,  Version 3.00 for Windows NT
  94. ECHO Display ASCII characters with the specified ASCII values
  95. ECHO.
  96. ECHO Usage:    CHR  number  [ number ]  [ number ] ...
  97. ECHO    or:    CHR  0xHH[0xHH][0xHH] ...
  98. ECHO,
  99. ECHO Where:    number   is a whole number in the range 33..126
  100. ECHO           0xHH     is a 2-digit hexadecimal number in the range 0x00..0xFF
  101. ECHO.
  102. ECHO Notes:    Characters 32 ( ), 34 (") and 61 (=) are displayed as a caret,
  103. ECHO           followed by a backspace and the actual character. On screen the
  104. ECHO           carets and backspaces will NOT be visible, but redirected they WILL.
  105. ECHO           To specify characters in hexadecimal requires FORFILES.EXE.
  106. ECHO           With hexadecimal, each spaces will result in a CR/LF in the output.
  107. ECHO           With decimal, no CR/LF will be used in the output.
  108. ECHO.
  109. ECHO Examples:
  110. ECHO CHR 84 104 97 110 107 115 32 34 83 109 97 114 116 71 101 110 105 117 115 34
  111. ECHO will return: Thanks "SmartGenius"
  112. IF NOT "%NoCRLF%"=="1" ECHO CHR 0x540x680x610x6E0x6B0x730x200x520x6F0x620x200x560x610x6E0x200x450x740x740x61
  113. IF     "%NoCRLF%"=="1" SET /P "=CHR 0x540x680x610x6E0x6B0x730x200x520x6F0x620x200x560x610x6E0x200x450x740x740x61" < NUL
  114. ECHO will return: Thanks Rob Van Etta
  115. ECHO.
  116. ECHO Based on research by "SmartGenius" (decimal) and Rob Van Etta (hexadecimal)
  117. ECHO Written by Rob van der Woude
  118. IF NOT "%OS%"=="Windows_NT" ECHO http://www.robvanderwoude.com
  119. IF     "%OS%"=="Windows_NT" SET /P "=http://www.robvanderwoude.com" < NUL
  120.  
  121. IF "%OS%"=="Windows_NT" ENDLOCAL
  122.  

page last modified: 2024-02-26; loaded in 0.0245 seconds