(view source code of chr.bat as plain text)
@ECHO OFF
:: Check Windows version (NT)
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
SETLOCAL ENABLEDELAYEDEXPANSION
:: Read the number of available columns in the console
SET Columns=
FOR /F "skip=2 tokens=*" %%A IN ('MODE CON ^| FIND ":"') DO (
IF "!Columns!"=="" (
FOR %%B IN (%%A) DO (
SET Columns=%%B
)
)
)
IF %Columns% LSS 81 (
SET NoCRLF=1
)
:: Check if FORFILES.EXE is available
SET UseForfiles=0
FORFILES /? 2>NUL | FIND /I "0xHH" >NUL
IF NOT ERRORLEVEL 1 SET UseForfiles=1
:: Check command line
IF "%~1"=="" GOTO Syntax
:: Check Windows version (XP required if FORFILES not available)
FOR /F "tokens=*" %%A IN ('VER') DO (
FOR %%B IN (%%~A) DO (
FOR /F "delims=]" %%C IN ("%%~B") DO (
SET Ver=%%C
)
)
)
:: Abort if neither FORFILES nor %=ExitCodeAscii% can be used
IF %UseForfiles% NEQ 1 (
IF %Ver% LEQ 5.1 (
GOTO Syntax
)
)
:: Check if numeric and in the proper range, depending on the method used
IF %UseForfiles% NEQ 1 (
FOR %%A IN (%*) DO (
SET /A Dummy=%%~A
IF NOT "%%~A"=="!Dummy!" GOTO Syntax
REM Check if in range 32..126
IF %%~A LSS 32 GOTO Syntax
IF %%~A GTR 126 GOTO Syntax
)
)
IF %UseForfiles% EQU 1 (
FOR %%A IN (%*) DO (
ECHO.%%A| FINDSTR /R /X /C:"0x[0-9A-F][0-9A-F](0x[0-9A-F][0-9A-F])*" >NUL || GOTO Syntax
)
)
:: Save current code page and set code page to 437
FOR /F "tokens=*" %%A IN ('CHCP') DO FOR %%B IN (%%A) DO SET CHCP=%%B
CHCP 437 >NUL
:: Main code without FORFILES
:: Set errorlevel, then translate it to ASCII character and display on screen without CR/LF.
:: For characters 32, 34 and 61 we need to use a dirty trick to circumvent SET /P limitations.
IF %UseForfiles% NEQ 1 (
FOR %%A IN (%*) DO (
CMD /C EXIT /B %%A
IF %%A==32 (SET /P Dummy=^^^^ <NUL)
IF %%A==34 (SET /P Dummy=^^^^"<NUL)
IF %%A==61 (SET /P Dummy=^^^=<NUL)
IF NOT %%A==32 IF NOT %%A==34 IF NOT %%A==61 (SET /P "Dummy=!=ExitCodeAscii!"<NUL)
)
)
:: Main code using FORFILES
IF %UseForfiles% EQU 1 (
FOR %%A IN (%*) DO (
FORFILES /P %~dps0 /M %~nxs0 /C "CMD /C SET /P 0x22=%%~A0x22 0x3C NUL"
)
)
:: Restore code page
CHCP %CHCP% >NUL
:: Done
ENDLOCAL
GOTO:EOF
:Syntax
ECHO Chr.bat, Version 3.00 for Windows NT
ECHO Display ASCII characters with the specified ASCII values
ECHO.
ECHO Usage: CHR number [ number ] [ number ] ...
ECHO or: CHR 0xHH[0xHH][0xHH] ...
ECHO,
ECHO Where: number is a whole number in the range 33..126
ECHO 0xHH is a 2-digit hexadecimal number in the range 0x00..0xFF
ECHO.
ECHO Notes: Characters 32 ( ), 34 (") and 61 (=) are displayed as a caret,
ECHO followed by a backspace and the actual character. On screen the
ECHO carets and backspaces will NOT be visible, but redirected they WILL.
ECHO To specify characters in hexadecimal requires FORFILES.EXE.
ECHO With hexadecimal, each spaces will result in a CR/LF in the output.
ECHO With decimal, no CR/LF will be used in the output.
ECHO.
ECHO Examples:
ECHO CHR 84 104 97 110 107 115 32 34 83 109 97 114 116 71 101 110 105 117 115 34
ECHO will return: Thanks "SmartGenius"
IF NOT "%NoCRLF%"=="1" ECHO CHR 0x540x680x610x6E0x6B0x730x200x520x6F0x620x200x560x610x6E0x200x450x740x740x61
IF "%NoCRLF%"=="1" SET /P "=CHR 0x540x680x610x6E0x6B0x730x200x520x6F0x620x200x560x610x6E0x200x450x740x740x61" < NUL
ECHO will return: Thanks Rob Van Etta
ECHO.
ECHO Based on research by "SmartGenius" (decimal) and Rob Van Etta (hexadecimal)
ECHO Written by Rob van der Woude
IF NOT "%OS%"=="Windows_NT" ECHO http://www.robvanderwoude.com
IF "%OS%"=="Windows_NT" SET /P "=http://www.robvanderwoude.com" < NUL
IF "%OS%"=="Windows_NT" ENDLOCAL
page last modified: 2024-04-16; loaded in 0.0089 seconds