Advanced Batch features via auxiliary .exe programs
EDIT: A new simpler method to get these programs is available. Just download the next "Auxiliary .exe programs.zip" attached file and extract all auxiliary .exe programs and their descriptions from it.
EDIT - 2016/12/04: Includes updated version of GetInput.exe and new SendMessage.exe program.
EDIT - 2016/12/21: Includes GetInput.exe version 1.2 with "mouse hovering support".
This is a list of the programs included in this file grouped by type of operation. Each .exe program have a .txt companion file that contains the program description. In certain .txt files there is a link to particular examples of such a program; you may review and post questions about these programs at the given links.
Code: Select all
String manipulation:
====================
StrLen.exe Get the length of string literals and Batch variables.
Ascii.exe Show Ascii codes of string literals and Batch variables.
Keyboard input:
===============
GetKey.exe Get a key from keyboard and return its value.
ShowKeyCodes.bat Display the codes returned by GetKey for all keys.
FlushInputBuffer.exe Clear the input buffer from any previous keystroke.
GetInput.exe Read input from both keyboard keys and mouse clicks.
Screen output:
==============
CursorPos.exe Get or set cursor position.
CursorSize.exe Get or set cursor size.
Show.exe Show characters from Ascii codes, literals and variables.
ColorShow.exe Show characters in the same way as Show.exe, but in color.
ColorChar.exe Stripped down version of ColorShow.exe with no variables.
ColorBox.exe Show a box in text mode using line-drawing characters.
DefineColorCodes.bat Define values useful to write color attributes.
Window.exe Get/set several values related to console screen window.
Date and Time:
==============
StdTime.exe Get a standard time and convert it to milliseconds.
ElapsedTime.bat Display elapsed times in a standard format.
StdDate.exe Get a standard date and convert it to Julian Day Number.
EasterSunday.bat Calculate the date of Christian Easter Sunday.
File handle management:
=======================
GetFileType.exe Get the type of the file connected to a file handle.
FilePointer.exe Move the file pointer of the redirected disk file.
TruncateFile.exe Truncate the size of the redirected disk file.
ReadFile.exe Read bytes from a redirected input file handle.
Miscellaneous programs:
=======================
printf.exe Show formatted output and evaluate RPN arithmetic expressions.
SendMessage.exe Send a System-Defined Message to a window.
EDIT: Below there is an index of these auxiliary commands and example programs with the number of the particular reply, if any, where each program was posted. Just copy the &p=... part and paste it in the address bar after t=3428.
1- StrLen.exe.hex:INDEX wrote: String manipulation.
StrLen.exe
Get the length of string literals and Batch variables.
Ascii.exe &p=17750#p17750
Show Ascii codes of string literals and Batch variables.
Keyboard input.
GetKey.exe
Get a key from keyboard and return its value in ERRORLEVEL.
SHOWKEYCODES.BAT
Display the codes returned by GetKey for all special keys in the keyboard.
:ReadLine.bat
Read a line from keyboard emulating SET /P command.
FlushInputBuffer.exe
Clear the input buffer of any previous keystroke.
Screen management.
CursorPos.exe.hex &p=17216#p17216
Get or set cursor position.
:GetCoords.bat &p=17216#p17216
Separate the coordinates value given by CursorPos in Column & Row.
Show.exe
Show characters from Ascii codes, string literals and Batch variables.
CursorSize.exe &p=17750#p17750
Get or set cursor size.
TEXTANIMATION.BAT &p=17912#p17912
Basic example of text animation.
SNAKE.BAT &p=17216#p17216
Basic example of an interactive animation video game program.
Window.exe &p=17216#p17216
Get or set some values related to console screen window.
Display colors in the screen.
:DefineColorCodes.bat &p=17750#p17750
Define auxiliary variables for color attributes.
TextColor.exe &p=17750#p17750
Get or set text color.
ColorShow.exe &p=17750#p17750
Show characters in color.
:Select.bat &p=17750#p17750
Activate a selection of options in one line.
Date and Time management.
StdTime.exe &p=17610#p17610
Get standard time fields and convert they to/from number of milliseconds.
:ElapsedTime.bat &p=17610#p17610
Display elapsed times in a readable format.
StdDate.exe &p=17610#p17610
Get standard date fields and convert they to/from Julian Day Number.
:EasterSunday &p=17610#p17610
Calculate the date of Christian Easter Sunday of any given year.
File management.
FileSize.exe Soon
Get file size in maximum columns and number of lines.
HandleType.exe Soon, preliminary version in: t=2800 (change t=3428 part)
Identify a redirected standard handle.
FilePointer.exe Soon, preliminary version in: t=2823 (change t=3428 part)
Get or set file pointer of redirected standard handles.
Code: Select all
4D5A900003[3]04[3]FFFF[2]B8[7]40[35]B0[3]0E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F74
2062652072756E20696E20444F53206D6F64652E0D0D0A24[7]D5FA31DE919B5F8D919B5F8D919B5F8D1F844C8D979B5F8D6D
BB4D8D939B5F8D52696368919B5F8D[8]5045[2]4C010200C0D6D34F[8]E0000F010B01050C0002[3]02[7]10[3]10[3]20[4]40[2]10
[3]02[2]04[7]04[8]30[3]02[6]03[5]10[2]10[4]10[2]10[6]10[11]1020[2]28[84]20[2]10[27]2E74657874[3]C0[4]10[3]02[3]02[14]20[2]602E
7264617461[2]90[4]20[3]02[3]04[14]40[2]40[8]E806[3]50E8A3[3]558BEC83C4F8C745F8[4]E863[3]E882[3]8A064684C0744C3C22
75198A064684C074413C227405FF45F8EBF08A06463C2274F4EBD78BFE4F8A1E80FB20740784DB740346EBF2C606006A018D
45FF5057E84E[3]0145F883F8017E03FF4DF8881EEBA88B45F8C9C3CCCCCCCCCCCCE835[3]8BF08A06463C2275098A06463C22
75F9EB0C8A06463C20740484C075F54EC38A06463C2074F94EC3CCFF2508204000FF2500204000FF25042040[321]5620[2]7020
[2]4820[6]3820[10]8220[3]20[22]5620[2]7020[2]4820[6]9B004578697450726F63657373001301476574456E7669726F6E6D656E
745661726961626C654100E600476574436F6D6D616E644C696E6541006B65726E656C33322E646C6C[370]
EDIT: I slightly modified Show.exe program to also accept an X-letter instead an asterisk in CODExTIMES parameter.StrLen.exe description wrote: Get the length of string literals and Batch variables.
StrLen "literal" | variable ...
At end, the accumulated length of all strings is returned in ERRORLEVEL.
2- Show.exe.hex:
Code: Select all
4D5A900003[3]04[3]FFFF[2]B8[7]40[35]B0[3]0E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F74
2062652072756E20696E20444F53206D6F64652E0D0D0A24[7]5527D9C81146B79B1146B79B1146B79B9F59A49B1946B79BED
66A59B1346B79B526963681146B79B[8]5045[2]4C010200935FFF4F[8]E0000F010B01050C0004[3]02[7]10[3]10[3]20[4]40[2]10
[3]02[2]04[7]04[8]30[3]02[6]03[5]10[2]10[4]10[2]10[6]10[11]1820[2]28[84]20[2]18[27]2E74657874[3]0C03[3]10[3]04[3]02[14]20[2]60
2E7264617461[2]BC[4]20[3]02[3]06[14]40[2]40[8]E806[3]50E8E302[2]558BEC81C4E8DFFFFFFCC785E8DFFFFF[4]6AF5E8D402[2]89
45FC8DBDFCDFFFFF89BDF8DFFFFFE88602[2]E8A502[2]8A064684C00F842A02[2]3C22751D8A064684C00F841B02[2]3C227405
880747EBEC8A06463C2274F4EBCF3C3072443C397740E82A02[2]3C2A74083C5874043C78752A8ADC468A06463C300F82E201
[2]3C390F87DA01[2]E80302[2]0FB6CC8885F7DFFFFF8AC3F3AAEB8C882747EB872BBDF8DFFFFF741E01BDE8DFFFFF6A008D85
ECDFFFFF5057FFB5F8DFFFFFFF75FCE82702[2]8BFE4F33DB889DF6DFFFFF8A063C3A74118885F7DFFFFF3C20746084C0745C
46EBE9889DF6DFFFFFC60600468A06463C2D7509C685F6DFFFFF02EB0B3C2B750AC685F6DFFFFF018A06463C300F826F01[2]3C
390F876701[2]E86A01[2]8885F7DFFFFF3C20740884C00F855001[2]84E40F840901[2]8ADCC60600680020[2]8D85FCDFFFFF50
57E88B01[2]85C00F84EA[3]85DB0F84A0[3]3BC37C2C80BDF6DFFFFF00741DF685F6DFFFFF01750A2BC30185F8DFFFFFEB0A2B
C3D1E80185F8DFFFFF8BC333DBEB702BD88BBDF8DFFFFF03F889BDF0DFFFFF50B0208BCBF3AA80BDF6DFFFFF00744FF685F6
DFFFFF027422019DE8DFFFFF6A008D85ECDFFFFF5053FFB5F0DFFFFFFF75FCE81001[2]33DBEB248BFBD1EF2BDF01BDE8DFFF
FF6A008D85ECDFFFFF5057FFB5F0DFFFFFFF75FCE8E8[3]588BF801BDE8DFFFFF6A008D85ECDFFFFF5057FFB5F8DFFFFFFF75
FCE8C7[3]85DB741E019DE8DFFFFF6A008D85ECDFFFFF5053FFB5F0DFFFFFFF75FCE8A5[3]8A85F7DFFFFF88068DBDFCDFFFFF
89BDF8DFFFFFE9C6FDFFFF2BBDF8DFFFFF741E01BDE8DFFFFF6A008D85ECDFFFFF5057FFB5F8DFFFFFFF75FCE866[3]8B85E8
DFFFFFC9C3240F8AE08A06463C30720C3C3977082C30D50A8AE0EBED4EC3CCCCCCCCCCE841[3]8BF08A06463C2275098A0646
3C2275F9EB0C8A06463C20740484C075F54EC38A06463C2074F94EC3CCFF2510204000FF2500204000FF2504204000FF2508
204000FF250C2040[245]6620[2]8020[2]9020[2]9C20[2]5820[6]4020[10]AE20[3]20[22]6620[2]8020[2]9020[2]9C20[2]5820[6]9B0045
78697450726F63657373001301476574456E7669726F6E6D656E745661726961626C6541006A0147657453746448616E646C
65[2]F702577269746546696C6500E600476574436F6D6D616E644C696E6541006B65726E656C33322E646C6C[326]
Example:Show.exe description wrote: Show characters from Ascii codes, string literals and Batch variables.
Show code[*times] | "literal" | variable[:[±]wide] ...
<code>, <times> and <wide> must be decimal numbers up to 255 each.
Times repeat the previous Ascii code character that number of times.
Wide define a width to show the variable value: justified at left, or justified at rigth if <wide> have minus sign, or centered if <wide> have plus sign.
At end, the number of displayed characters is returned in ERRORLEVEL.
Code: Select all
@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
Code: Select all
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
Code: Select all
:ShowFrame width height
set /A width=%1-2, height=%2-2
Show 218 196*%width% 191 13 10
for /L %%i in (1,1,%height%) do Show 179 32*%width% 179 13 10
Show 192 196*%width% 217 13 10
exit /B
Code: Select all
:ShowFileInFrame filename
rem Load file lines and get max line length
set i=0
set maxLen=0
for /F "delims=" %%a in (%1) do (
set /A i+=1
set "line[!i!]=%%a"
StrLen line[!i!]
if !errorlevel! gtr !maxLen! set maxLen=!errorlevel!
)
rem Show file lines into a frame
Show 218 196*%maxLen% 191 13 10
for /L %%i in (1,1,%i%) do Show 179 line[%%i]:+%maxLen% 179 13 10
Show 192 196*%maxLen% 217 13 10
... previous subroutine produce this output:Datafile.txt wrote: This is a small example file
comprised of four lines of text
with a longest line
of thirty one characters
Code: Select all
┌───────────────────────────────┐
│ This is a small example file │
│comprised of four lines of text│
│ with a longest line │
│ of thirty one characters │
└───────────────────────────────┘
3- GetKey.exe.hex:
Code: Select all
4D5A900003[3]04[3]FFFF[2]B8[7]40[35]B0[3]0E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F74
2062652072756E20696E20444F53206D6F64652E0D0D0A24[7]55B5B8FD11D4D6AE11D4D6AE11D4D6AE9FCBC5AE18D4D6AEED
F4C4AE13D4D6AE5269636811D4D6AE[8]5045[2]4C0102005A66D14F[8]E0000F010B01050C0002[3]02[7]10[3]10[3]20[4]40[2]10
[3]02[2]04[7]04[8]30[3]02[6]03[5]10[2]10[4]10[2]10[6]10[11]1820[2]3C[84]20[2]18[27]2E74657874[3]96[4]10[3]02[3]02[14]20[2]602E
7264617461[2]BA[4]20[3]02[3]04[14]40[2]40[8]E806[3]50E873[3]E840[3]E85F[3]803E00741866813E2F57740766813E2F77740A
FF150C20400085C07419FF151020400085C074073DE0[3]7508FF1510204000F7D8C3CCCCCCCCE82F[3]8BF08A06463C227509
8A06463C2275F9EB0C8A06463C20740484C075F54EC38A06463C2074F94EC3CCFF2504204000FF2500204000FF2510204000FF
250C2040[363]7A20[2]6C20[6]A420[2]9A20[6]5420[10]8C20[3]20[2]6020[10]AE20[2]0C20[22]7A20[2]6C20[6]A420[2]9A20[6]9B004578
697450726F6365737300E600476574436F6D6D616E644C696E6541006B65726E656C33322E646C6C[2]CE005F6765746368[2]11
015F6B62686974[2]6D73766372742E646C6C[328]
The program below (SHOWKEYCODES.BAT) display the codes returned by GetKey for all special keys in the keyboard, including Shift-, Ctrl- and Alt- combinations. You may run this program and copy just the specific codes you need.GetKey.exe description wrote: Get a key from keyboard and return its value in ERRORLEVEL.
GetKey [/N]
Ascii characters are returned as positive values, extended keys as negative values.
If /N switch is given, no wait for a key: immediately return zero if no key was pressed.
Code: Select all
@echo off
setlocal EnableDelayedExpansion
(for /F "delims==" %%a in ('set') do (
echo %%a
)) > vars.txt
call :DefineKeyCodes
set a=a
< vars.txt (
for /F "tokens=1* delims==" %%a in ('set') do (
if "!a!" equ "%%a" (
set /P a=
) else (
echo %%a=%%b
)
))
del vars.txt
goto :EOF
:DefineKeyCodes
rem Definition of key codes via key names
rem Antonio Perez Ayala
rem Require Delayed Expansion. Modify "i" variable.
rem Can not use Setlocal because its purpose is to create global variables
for %%a in ("BackSpace=8" "TabKey=9" "Ctrl_Enter=10" "EnterKey=13" "EscKey=27" "Ctrl_@=-3") do (
set %%a
)
set i=-14
for %%a in (Alt_BackSpace Shift_Tab) do (
set %%a=!i!
set /A i-=1
)
rem Currently: i=-16
for %%a in (Q W E R T Y U I O P LeftBracket RightBracket) do (
set Alt_%%a=!i!
set /A i-=1
)
set i=-30
for %%a in (A S D F G H J K L Semicolon Apostrophe BackQuote) do (
set Alt_%%a=!i!
set /A i-=1
)
set i=-43
for %%a in (BackSlash Z X C V B N M Comma Dot Slash "" GrayStar) do (
set Alt_%%~a=!i!
set /A i-=1
)
set i=-59
for %%a in (F1 F2 F3 F4 F5 F6 F7 F8 F9 F10) do (
set %%a=!i!
set /A i-=1
)
set i=-71
for %%a in (HomeKey UpArrow PageUp Alt_GrayDash LeftArrow KeyPad5 RightArrow
Alt_GrayPlus EndKey DownArrow PageDown InsKey DelKey) do (
set %%a=!i!
set /A i-=1
)
rem Currently: i=-84
for %%a in (Shift Ctrl Alt) do (
for %%b in (F1 F2 F3 F4 F5 F6 F7 F8 F9 F10) DO (
set %%a_%%b=!i!
set /A i-=1
)
)
rem Currently: i=-114
for %%a in (PrtSc LeftArrow RightArrow End PageDown Home) do (
set Ctrl_%%a=!i!
set /A i-=1
)
rem Currently: i=-120
for %%a in (1 2 3 4 5 6 7 8 9 0 Dash Equal) do (
set Alt_%%a=!i!
set /A i-=1
)
rem Currently: i=-132
for %%a in (Ctrl_PageUp F11 F12 Shift_F11 Shift_F12 Ctrl_F11 Ctrl_F12 Alt_F11 Alt_F12) do (
set %%a=!i!
set /A i-=1
)
rem Currently: i=-141
for %%a in (UpArrow GrayDash KeyPad5 GrayPlus DownArrow Ins Del Tab GraySlash GrayStar) do (
set Ctrl_%%a=!i!
set /A i-=1
)
rem Currently: i=-151
for %%a in (Home UpArrow PageUp "" LeftArrow KeyPad5 RightArrow "" End
DownArrow PageDown Ins Del GraySlash) do (
set Alt_%%~a=!i!
set /A i-=1
)
set Alt_=
set i=
exit /B
Code: Select all
@echo off
set Bell=7
set BackSpace=8
set Enter=13
set Space=32
:ReadLine var=["prompt"]
set %1=
if "%~2" neq "" Show %2
:nextKey
GetKey
set key=%errorlevel%
if %key% geq %Space% (
rem Ascii character
Show %key%
for /F "delims=" %%a in ('Show %key%') do set "%1=!%1!%%a"
) else if %key% equ %BackSpace% (
if defined %1 (
Show %BackSpace% %Space% %BackSpace%
set "%1=!%1:~0,-1!"
) else (
Show %Bell%
)
) else if %key% equ %Enter% echo/& exit /B
goto nextKey
Code: Select all
set Digit_0=48
set Digit_9=57
. . .
rem Ascii character: ignore characters that are not digits
if %key% lss %Digit_0% goto nextKey
if %key% gtr %Digit_9% goto nextKey
Show %key%
. . .
Code: Select all
set and=if
set Upcase_A=65
set Lowcase_a=97
set Lowcase_z=122
. . .
rem Ascii character: convert lowcase letters to upcase ones
if %key% geq %Lowcase_a% %and% %key% leq %Lowcase_z% set /A key-=Lowcase_a-Upcase_A
Show !key!
. . .
Code: Select all
. . .
rem Ascii character: get it, but echo an asterisk
Show "*"
. . .
/N switch is useful to control actively interactive programs; for example, a program may repeat a loop until a key is pressed:
Code: Select all
:loop
rem Do a repetitive process here
GetKey /N
if %errorlevel% equ 0 goto loop
Code: Select all
@echo off
set Bell=7
set BackSpace=8
set Enter=13
set Space=32
:ReadLineTime var= seconds ["prompt"]
set %1=
set seconds=%2
Show %3 seconds ": "
set lastTime=%time:~0,-3%
:nextKey
GetKey /N
set key=%errorlevel%
if %key% equ 0 (
if %lastTime% equ %time:~0,-3% (
goto nextKey
) else (
set lastTime=%time:~0,-3%
set /A seconds-=1
if !seconds! gtr 0 (
Show 13 %3 seconds ": " %1 " " %BackSpace%
goto nextKey
) else (
StrLen %1
Show 13 %3 "0: " %Space%*!errorlevel! 13 10
set %1=Input timeout
exit /B 1
)
)
)
if %key% geq %Space% (
rem Ascii Character
Show %key%
for /F "delims=" %%a in ('Show %key%') do set "%1=!%1!%%a"
) else if %key% equ %BackSpace% (
if defined %1 (
Show %BackSpace% %Space% %BackSpace%
set "%1=!%1:~0,-1!"
) else (
Show %Bell%
)
) else if %key% equ %Enter% echo/& exit /B
goto nextKey
4- FlushInputBuffer.exe.hex
Code: Select all
4D5A900003[3]04[3]FFFF[2]B8[7]40[35]B0[3]0E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F74
2062652072756E20696E20444F53206D6F64652E0D0D0A24[7]551E49C1117F2792117F2792117F27929F603492167F2792ED
5F3592137F279252696368117F2792[8]5045[2]4C010200D319C84F[8]E0000F010B01050C0002[3]02[7]10[3]10[3]20[4]40[2]10
[3]02[2]04[7]04[8]30[3]02[6]03[5]10[2]10[4]10[2]10[6]10[11]1420[2]28[84]20[2]14[27]2E74657874[3]66[4]10[3]02[3]02[14]20[2]602E
7264617461[2]A8[4]20[3]02[3]04[14]40[2]40[8]6AF6E853[3]50E847[3]6A00E83A[3]CCCCCCCCCCCCCCCCCCCCCCCCE83B[3]8BF08A
06463C2275098A06463C2275F9EB0C8A06463C20740484C075F54EC38A06463C2074F94EC3CCFF250C204000FF2500204000FF
2504204000FF25082040[411]5E20[2]7820[2]8820[2]5020[6]3C20[10]9A20[3]20[22]5E20[2]7820[2]8820[2]5020[6]9B004578697450
726F6365737300C600466C757368436F6E736F6C65496E707574427566666572006A0147657453746448616E646C65[2]E60047
6574436F6D6D616E644C696E6541006B65726E656C33322E646C6C[346]
FlushInputBuffer.exe description wrote: Clear the input buffer of any previous keystroke.
FlushInputBuffer
Feel free to copy and use these programs in any way you wish. I'd appreciate it if you report any problem you may find. If you use anyone of these programs to write and publish a Batch-file application, then you should not copy these *.exe files directly in your distribution; instead, you should include a link to this site so final users can copy the last version of the auxiliary program besides the help .txt file and examples.
Antonio