├── args-all.bat ├── args-1.bat ├── text-delims-4.txt ├── the long text file name.txt ├── autopy-test.py ├── compound-1.bat ├── ftp-resp.txt ├── readme.txt ├── the long text file name 2.txt ├── err.exe ├── fib.exe ├── more-lines.txt ├── CmdInit.bat ├── test.bin ├── text1.txt ├── err-clr.bat ├── ask-user.bat ├── linux.macro ├── pipe-1.bat ├── for-enum-recursive.bat ├── ChangeMACAddressBatch.zip ├── err-test.bat ├── WifiPasswordReveal-1.0.zip ├── for-enum-dirs.bat ├── text-delims-1.txt ├── text-delims-2.txt ├── compound-2.bat ├── debug-3.bat ├── goto-label.bat ├── args-enum2.bat ├── escape-syms.bat ├── resumable-script-stages.txt ├── check-vars-quotes.bat ├── eve-test3.bat ├── use-script1.bat ├── useful_scripts ├── autolua.bat ├── autopy.bat ├── autobat.bat ├── exec_strings.bat ├── autodot.bat └── autox.bat ├── text-eol-1.txt ├── env-export-exitcode.bat ├── join-tokens.bat ├── text-delims-3.txt ├── err.c ├── README.md ├── for-count.bat ├── ask-user-2.bat ├── batch-powershell-1.bat ├── strcat-2.bat ├── text-tokens-1.txt ├── env-export-and.bat ├── eve-test1.bat ├── strcat-1.bat ├── for-files-set-3.bat ├── shift-compound.bat ├── use-script0.bat ├── for-files-set-2.bat ├── for-files-set.bat ├── func-add.bat ├── long-command.bat ├── goto-eof.bat ├── io-redir-compound.bat ├── args-enum1.bat ├── findstr-substr.bat ├── goto-label-resilient.bat ├── env-export-and-fail.bat ├── call-label-prototype.bat ├── compound-3.bat ├── debug-4.bat ├── eve-test2.bat ├── settings.ini ├── is-admin-3.bat ├── wa-plates.txt ├── debug-2.bat ├── fib.c ├── funcs-1.bat ├── is-admin-1.bat ├── echo-test.bat ├── func-call.bat ├── goto-label-resilient-2.bat ├── batch-perl.bat ├── deve-test3.bat ├── batch-perl-end.bat ├── deve-test1.bat ├── for-each-char-1.bat ├── for-each-char-1.c ├── get-ftime.bat ├── str-substitute.bat ├── for-file-lines-no-options.bat ├── get-fattr.bat ├── env-export-for.bat ├── pack-tracked.bat ├── str-substitute-var.bat ├── func-call-fatal.bat ├── expenses.csv ├── env-export-temp-files.bat ├── for-file-lines-eol.bat ├── for-string-token1.bat ├── get-fsize.bat ├── console-editor.bat ├── comments.bat ├── env-expand-2.bat ├── localvars.bat ├── batch-jscript.bat ├── batch-powershell.bat ├── call-script0.bat ├── env-local-nested.bat ├── get-win-ver.bat ├── env-export-compound-simple.bat ├── is-admin-2.bat ├── env-group-naming.bat ├── for-each-char-2.bat ├── array-2d.bat ├── call-script1.bat ├── for-command-output-time.bat ├── transform-me-1.bat ├── annotated-code-sample.bat ├── func-call-invalid.bat ├── array-1d.bat ├── str-substitute-asterisk.bat ├── debug-1.bat ├── str-substring.bat ├── batch-python.bat ├── goto-label-resilient-findstr.bat ├── get-two-randoms.bat ├── findstr-test.txt ├── get-ftime-secs.bat ├── func-call-resilient.bat ├── str-len.bat ├── for-nest-sum-csv.bat ├── DetectWinLanguage.bat ├── str-len-2.bat ├── use-lib.bat ├── if-ex.bat ├── array-assoc-1.bat ├── for-file-lines-usebackq.bat ├── str-len-fast.bat ├── contains-str-1.bat ├── args-modifiers.bat ├── coding-style.bat ├── env-export-compound.bat ├── args-modifiers-0.bat ├── for-file-lines-token.bat ├── batch-ftp.bat ├── expr-1.bat ├── transform-me-1.cpp ├── io-redir.bat ├── fib.bat ├── findstr-extract-between-markers.bat ├── batch-cpp.bat ├── count-gotoif.bat ├── args-check1.bat ├── batch-compel.bat ├── debug-5.bat ├── test-lib.bat ├── gen-tables.py ├── ds-stacks.bat ├── for-file-lines-delims.bat ├── parse-ini.bat ├── func-label-conventions.bat ├── autopy.bat ├── ds-sets-2.bat ├── load-save-states.bat ├── ord2chr.bat ├── change-str-case.bat ├── ds-sets-1.bat ├── extract-embedded-bin.bat ├── menu-dynamic.bat ├── lib-batch.bat ├── WifiPasswordReveal.bat ├── resumable-script.bat ├── WifiPasswordReveal_FR.bat ├── watch-dir.bat ├── switch-case.bat ├── not-so-evil-batch.bat ├── console-color.bat ├── array-assoc-2.bat ├── menu-simple.bat ├── extract-embedded-files.bat ├── parse-args.bat ├── WifiPasswordReveal-exp.bat ├── words-countries.txt ├── words-sat300.txt ├── array-1d-funcs.bat ├── hangman.bat ├── LICENSE └── ChangeMACAddressBatch.bat /args-all.bat: -------------------------------------------------------------------------------- 1 | @echo All arguments: %* -------------------------------------------------------------------------------- /args-1.bat: -------------------------------------------------------------------------------- 1 | @echo arg0=%0, arg1=%1, arg2=%2 2 | -------------------------------------------------------------------------------- /text-delims-4.txt: -------------------------------------------------------------------------------- 1 | HelloLWorld,LHelloLFromLBellevue -------------------------------------------------------------------------------- /the long text file name.txt: -------------------------------------------------------------------------------- 1 | line 1 2 | line 2 3 | line 3 4 | line 4 -------------------------------------------------------------------------------- /autopy-test.py: -------------------------------------------------------------------------------- 1 | print "Hello world!" 2 | for i in range(1, 10): 3 | print i -------------------------------------------------------------------------------- /compound-1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | ( 3 | echo 1 4 | echo 2 5 | echo 3 6 | ) -------------------------------------------------------------------------------- /ftp-resp.txt: -------------------------------------------------------------------------------- 1 | anonymous 2 | ebooks@passingtheknowledge.net 3 | ls 4 | quit -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | These are all the code snippets and samples used in this book. 2 | -------------------------------------------------------------------------------- /the long text file name 2.txt: -------------------------------------------------------------------------------- 1 | aline 1 2 | aline 2 3 | aline 3 4 | aline 4 -------------------------------------------------------------------------------- /err.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PassingTheKnowledge/Batchography/HEAD/err.exe -------------------------------------------------------------------------------- /fib.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PassingTheKnowledge/Batchography/HEAD/fib.exe -------------------------------------------------------------------------------- /more-lines.txt: -------------------------------------------------------------------------------- 1 | line 1 2 | line 2 3 | line 3 4 | line 4 5 | line 5 6 | line 6 -------------------------------------------------------------------------------- /CmdInit.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | prompt $+%PROMPT% 3 | doskey /macrofile=%~dp0cmdinit.macro -------------------------------------------------------------------------------- /test.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PassingTheKnowledge/Batchography/HEAD/test.bin -------------------------------------------------------------------------------- /text1.txt: -------------------------------------------------------------------------------- 1 | Line1 2 | Line2 3 | Line3 4 | Line4 5 | 6 | Line5 7 | Line 6 8 | Line7 -------------------------------------------------------------------------------- /err-clr.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | err 35 3 | verify > nul 4 | echo ERRORLEVEL= %ERRORLEVEL% 5 | -------------------------------------------------------------------------------- /ask-user.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | set /P Name="Please enter your name: " 4 | echo Welcome %Name% 5 | -------------------------------------------------------------------------------- /linux.macro: -------------------------------------------------------------------------------- 1 | ls=dir $* 2 | pwd=echo ^%cd^% 3 | cp=copy $* 4 | mv=move $* 5 | traceroute=tracert $* -------------------------------------------------------------------------------- /pipe-1.bat: -------------------------------------------------------------------------------- 1 | ECHO My name is agent Smith | SET /P Name="What is your name?" 2 | ECHO Result is %Name% 3 | -------------------------------------------------------------------------------- /for-enum-recursive.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | for /r . %%a IN (*.bat) DO ( 4 | ECHO matched file: %%a 5 | ) 6 | -------------------------------------------------------------------------------- /ChangeMACAddressBatch.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PassingTheKnowledge/Batchography/HEAD/ChangeMACAddressBatch.zip -------------------------------------------------------------------------------- /err-test.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | err 1 3 | echo ERRORLEVEL = %ERRORLEVEL% 4 | err 33 5 | echo ERRORLEVEL= %ERRORLEVEL% -------------------------------------------------------------------------------- /WifiPasswordReveal-1.0.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PassingTheKnowledge/Batchography/HEAD/WifiPasswordReveal-1.0.zip -------------------------------------------------------------------------------- /for-enum-dirs.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | FOR /D %%f IN ( 4 | item1 5 | item2 6 | *.* 7 | ) DO ( 8 | ECHO %%f 9 | ) -------------------------------------------------------------------------------- /text-delims-1.txt: -------------------------------------------------------------------------------- 1 | L1-token 1,L1-token 2,L1-token 3 2 | L2-token 1,L2-token 2,L2-token 3 3 | L3-token 1,L3-token 2,L3-token 3 -------------------------------------------------------------------------------- /text-delims-2.txt: -------------------------------------------------------------------------------- 1 | L1-token 1,L1-token 2;L1-token 3 2 | L2-token 1;L2-token 2,L2-token 3 3 | L3-token 1,L3-token 2;L3-token 3 -------------------------------------------------------------------------------- /compound-2.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | ( 3 | echo 1 & echo 2 4 | echo this ^ 5 | is ^ 6 | a ^ 7 | long ^ 8 | line 9 | echo 3 10 | ) -------------------------------------------------------------------------------- /debug-3.bat: -------------------------------------------------------------------------------- 1 | @if NOT DEFINED _ECHOON (echo off) 2 | 3 | echo Hello from this script 4 | for /l %%a in (1, 1, 3) do ( 5 | echo i=%%a 6 | ) -------------------------------------------------------------------------------- /goto-label.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | goto %1 4 | 5 | echo Unreachable code 6 | 7 | :ValidLabel 8 | echo yes! 9 | goto :eof 10 | -------------------------------------------------------------------------------- /args-enum2.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | echo Args 1 to 6 are: %1 %2 %3 %4 %5 %6 4 | 5 | shift /3 6 | 7 | echo New args 1 to 6 are: %1 %2 %3 %4 %5 %6 -------------------------------------------------------------------------------- /escape-syms.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | ECHO 4 ^< 5, 2 ^> 1, 8 likes to smoke a ^|, and 9 loves to wear a ^^ 4 | 5 | set NAME=Hiram 6 | echo %%NAME%%=%NAME% -------------------------------------------------------------------------------- /resumable-script-stages.txt: -------------------------------------------------------------------------------- 1 | :step-create-files 2 | :step-process-files 3 | :step-create-report 4 | :step-compress 5 | :step-email 6 | :step-done 7 | -------------------------------------------------------------------------------- /check-vars-quotes.bat: -------------------------------------------------------------------------------- 1 | IF "%1"=="help" ECHO 1. Showing help screen 2 | IF .%1.==.help. ECHO 2. Showing help screen 3 | IF %1==help ECHO 3. Showing help screen -------------------------------------------------------------------------------- /eve-test3.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | SET C=-1 4 | FOR /L %%a IN (1, 1, 4) DO ( 5 | set C=%%a 6 | echo a=%%a, C=%C% 7 | ) 8 | 9 | ECHO after the loop, C=%C% -------------------------------------------------------------------------------- /use-script1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | echo this is the second script! 4 | 5 | :: exit /b will not only terminate this script, but the calling script. 6 | exit /b -------------------------------------------------------------------------------- /useful_scripts/autolua.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | if "%1"=="" ( 3 | call "%~dp0autox.bat" 4 | goto :eof 5 | ) 6 | call "%~dp0autox.bat" "C:\LUA\bin\lua.exe" "LUA" %* -------------------------------------------------------------------------------- /text-eol-1.txt: -------------------------------------------------------------------------------- 1 | ; This is a comment 2 | Hello 3 | ; Another comment 4 | From 5 | ; Once more 6 | The other end of the world 7 | ; More comment 8 | ohayou gozaimasu -------------------------------------------------------------------------------- /env-export-exitcode.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | 5 | set variable1=1 6 | set variable2=2 7 | 8 | set /A result=variable1 + variable2 9 | exit /B %result% -------------------------------------------------------------------------------- /join-tokens.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | 4 | set RESULT= 5 | for %%i in (%*) do set RESULT=!RESULT!%%i 6 | echo %RESULT% 7 | 8 | endlocal -------------------------------------------------------------------------------- /useful_scripts/autopy.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | if "%1"=="" ( 3 | call "%~dp0autox.bat" 4 | goto :eof 5 | ) 6 | call "%~dp0autox.bat" "c:\Python27\python.exe" "Python" %* -------------------------------------------------------------------------------- /text-delims-3.txt: -------------------------------------------------------------------------------- 1 | This is line 1, read in full 2 | This is line 2, read in full 3 | This is line 3, with the delimiter on purpose¿this is the second token 4 | This is line 4 -------------------------------------------------------------------------------- /useful_scripts/autobat.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | if "%1"=="" ( 3 | call "%~dp0autox.bat" 4 | goto :eof 5 | ) 6 | call "%~dp0autox.bat" "%COMSPEC%" "Batch" /cmdargs "/c" %* 7 | -------------------------------------------------------------------------------- /err.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *argv[]) 4 | { 5 | int err = argc < 2 ? -1 : atoi(argv[1]); 6 | printf("returning %d\n", err); 7 | return err; 8 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Batchography: The Art of Batch Files Programming 2 | 3 | This repository contains the scripts/recipes and snippets from the Batchography book (http://amzn.to/1X3tQ4K) 4 | 5 | -------------------------------------------------------------------------------- /for-count.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | if "%3"=="" ( 4 | echo Usage: %~n0 start end step 5 | goto :eof 6 | ) 7 | 8 | FOR /L %%i IN (%1, %3, %2) DO ( 9 | ECHO i=%%i 10 | ) -------------------------------------------------------------------------------- /ask-user-2.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal 3 | 4 | :repeat 5 | set input= 6 | set /p input=enter value: 7 | if "%input%"=="" goto :eof 8 | echo -^> %input% 9 | goto repeat 10 | -------------------------------------------------------------------------------- /batch-powershell-1.bat: -------------------------------------------------------------------------------- 1 | @findstr /v "^@findstr.*&" "%~f0" | powershell - & goto:eof 2 | 3 | 4 | # 5 | # The Batchography book by Elias Bachaalany 6 | # 7 | 8 | 9 | Write-Host Hello from PowerShell! -------------------------------------------------------------------------------- /strcat-2.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal enabledelayedexpansion 4 | 5 | set str= 6 | for /L %%a in (0, 1, 9) do ( 7 | set str=!str!%%a 8 | ) 9 | 10 | echo str=%str% 11 | 12 | endlocal -------------------------------------------------------------------------------- /text-tokens-1.txt: -------------------------------------------------------------------------------- 1 | L1-Token1 L1-Token2 L1-Token3 L1-Token4 L1-Token5 L1-Token6 2 | L2-Token1 L2-Token2 L2-Token3 L2-Token4 L2-Token5 L2-Token6 3 | L3-Token1 L3-Token2 L3-Token3 L3-Token4 L3-Token5 L3-Token6 -------------------------------------------------------------------------------- /env-export-and.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | 5 | set variable1=1 6 | set variable2=2 7 | set /A result=variable1 + variable2 8 | endlocal & set result=%result% 9 | 10 | echo The result is: %result% -------------------------------------------------------------------------------- /eve-test1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | set value=1 4 | 5 | if "%value%"=="1" ( 6 | set value=2 7 | echo The value inside the IF block is '%value%' 8 | ) 9 | 10 | echo The value after the IF block is '%value%' -------------------------------------------------------------------------------- /strcat-1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | 5 | set /P Fname=Enter your first name: 6 | set /P Lname=Enter your last name: 7 | 8 | set FULLNAME=%Fname% %Lname% 9 | 10 | echo Full name is: %FULLNAME% -------------------------------------------------------------------------------- /for-files-set-3.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | 5 | for %%a in ( 6 | "command 1" 7 | "command 2" 8 | "command 3" 9 | "string 1" 10 | "string 2") DO ( 11 | ECHO item=%%~a 12 | ) 13 | 14 | endlocal -------------------------------------------------------------------------------- /shift-compound.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal enabledelayedexpansion 4 | 5 | echo a^) 1=%1 2=%2 3=%3 6 | 7 | if 1==1 ( 8 | shift 9 | echo 1'=%1 2'=%2 10 | ) 11 | 12 | shift 13 | echo b^) 1=%1 2=%2 3=%3 -------------------------------------------------------------------------------- /use-script0.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | echo line 1 from main script 4 | echo will execute second script without a CALL 5 | 6 | use-script1.bat 7 | 8 | echo After using the second script. This line should not execute! 9 | -------------------------------------------------------------------------------- /for-files-set-2.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | 5 | for %%a in ( 6 | t*.bat 7 | *.exe 8 | "my *.txt" 9 | Myfile.txt 10 | Readme.txt 11 | ) do ( 12 | echo found: %%a 13 | ) 14 | 15 | endlocal 16 | -------------------------------------------------------------------------------- /for-files-set.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | 5 | set /a c=0 6 | for %%a in (t*.bat,*.exe, "my *.txt") do ( 7 | echo found: %%a 8 | set /a c=c+1 9 | ) 10 | 11 | echo. 12 | echo Listed in %c% file(s) 13 | endlocal -------------------------------------------------------------------------------- /func-add.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | :main 3 | setlocal 4 | call :add 1 5 5 | echo The result is %errorlevel% 6 | goto :eof 7 | 8 | :: Add two numbers 9 | :add (num1, num2) 10 | SET /A result=%1+%2 11 | exit /b %result% 12 | -------------------------------------------------------------------------------- /long-command.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | 4 | :: 5 | :: The Batchography book by Elias Bachaalany 6 | :: 7 | 8 | 9 | echo Hello! 10 | echo This ^ 11 | is ^ 12 | a ^ 13 | very ^ 14 | long ^ 15 | command 16 | Echo Goodbye! -------------------------------------------------------------------------------- /goto-eof.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | goto eof 4 | 5 | echo Unreachable, due to jumping to a label in the file. 6 | 7 | :eof 8 | 9 | echo a real label is here 10 | 11 | goto :eof 12 | 13 | 14 | echo Unreached code, due to "GOTO :EOF" -------------------------------------------------------------------------------- /io-redir-compound.bat: -------------------------------------------------------------------------------- 1 | REM Single line compound commands 2 | (dir /w & echo Some output & echo Some more output) >out.txt 3 | 4 | REM Multiple lines compound commands 5 | ( 6 | dir /w 7 | echo Some output 8 | echo Some more output 9 | ) >out2.txt -------------------------------------------------------------------------------- /args-enum1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | 8 | :repeat 9 | if "%1"=="" goto end 10 | echo Arg: %1 11 | 12 | SHIFT 13 | 14 | goto repeat 15 | 16 | :end 17 | -------------------------------------------------------------------------------- /findstr-substr.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal enabledelayedexpansion 4 | 5 | for /f "useback tokens=*" %%a in (`findstr /b /c:"@" "findstr-test.txt"`) do ( 6 | set line=%%a 7 | set line=!line:~1! 8 | echo !line! 9 | ) 10 | 11 | endlocal -------------------------------------------------------------------------------- /goto-label-resilient.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: with error handling 4 | 5 | goto %1 || ( 6 | echo Failed to go to that label! 7 | goto :eof 8 | ) 9 | 10 | echo Unreachable code 11 | 12 | :ValidLabel 13 | echo ValidLabel! 14 | goto :eof -------------------------------------------------------------------------------- /env-export-and-fail.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | set result= 4 | setlocal enabledelayedexpansion 5 | 6 | for /l %%a in (1, 1, 5) do ( 7 | set result=%%a 8 | if "%%a"=="2" ( 9 | endlocal & set result=!result! 10 | goto :eof 11 | ) 12 | ) 13 | -------------------------------------------------------------------------------- /call-label-prototype.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | 5 | call :add 1 5 6 | echo The result is %result% 7 | 8 | goto :eof 9 | 10 | 11 | :: Add two numbers 12 | :add (num1, num2) 13 | SET /A result=%1+%2 14 | GOTO :eof 15 | 16 | endlocal -------------------------------------------------------------------------------- /compound-3.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | 8 | ( 9 | ( 10 | echo 1 & echo 2 11 | ( 12 | echo this ^ 13 | is ^ 14 | a ^ 15 | long ^ 16 | line 17 | ) 18 | echo 3 19 | ) 20 | ) -------------------------------------------------------------------------------- /debug-4.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal 3 | 4 | @if NOT DEFINED _CNDECHO (set _CNDECHO=^>NUL) 5 | 6 | echo Hello world 7 | for /l %%a in (1,1,3) do ( 8 | echo this is a debug line #%%a %_CNDECHO% 9 | echo i=%%a 10 | ) 11 | echo Script terminating 12 | 13 | -------------------------------------------------------------------------------- /eve-test2.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | set value=1 4 | 5 | :repeat 6 | echo value is '%value%' 7 | 8 | if "%value%"=="1" set value=2 9 | if "%value%"=="2" set value=3 10 | if "%value%"=="3" goto end 11 | 12 | goto repeat 13 | :end 14 | 15 | echo Done! -------------------------------------------------------------------------------- /settings.ini: -------------------------------------------------------------------------------- 1 | ; the display settings 2 | [display] 3 | width=400 4 | height=400 5 | 6 | ; recent files 7 | [files] 8 | file0=c:\temp\test.txt 9 | file1=c:\temp\prog.pas 10 | 11 | ; general settings 12 | [settings] 13 | autosave=true 14 | lastsaved=04/17/2015 15 | -------------------------------------------------------------------------------- /is-admin-3.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | 4 | :: 5 | :: The Batchography book by Elias Bachaalany 6 | :: 7 | 8 | 9 | net session >nul 2>&1 10 | 11 | if %errorlevel% neq 0 ( 12 | echo Requires administrative privilege. 13 | goto :eof 14 | ) 15 | 16 | echo Script starts here... -------------------------------------------------------------------------------- /wa-plates.txt: -------------------------------------------------------------------------------- 1 | ZNQ-3360 2 | GTR-7960 3 | QKU-0946 4 | KOR-2027 5 | EOV-6287 6 | ALJ-0493 7 | XGU-0334 8 | AVP-1645 9 | PIO-1704 10 | RGL-2899 11 | GXA-0259 12 | BCJ-6808 13 | ZXB-6459 14 | MBW-5007 15 | NDR-6273 16 | JDS-1869 17 | WTW-3852 18 | QZW-8690 19 | KOL-7693 20 | JUM-6327 -------------------------------------------------------------------------------- /debug-2.bat: -------------------------------------------------------------------------------- 1 | @if "%_ECHOON%"=="" (echo off) 2 | 3 | setlocal enabledelayedexpansion 4 | 5 | set N=3 6 | 7 | for /l %%a in (1, 1, !N!) do ( 8 | set s= 9 | for /l %%b in (1,1, %%a) do ( 10 | set s=!s!* 11 | ) 12 | echo !s! 13 | ) 14 | 15 | echo The script is terminating now! 16 | endlocal 17 | -------------------------------------------------------------------------------- /fib.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fib(int n) 4 | { 5 | if (n == 0 || n == 1) 6 | return n; 7 | else 8 | return fib(n-2) + fib(n-1); 9 | } 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | for (int i=1; i<=8; i++) 14 | printf("fib(%d)=%d\n", i, fib(i)); 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /funcs-1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | call :function1 4 | call :function2 arg1 arg2 5 | 6 | goto :eof 7 | 8 | :function1 9 | echo This is function 1 10 | rem Return from the function 11 | goto :eof 12 | 13 | :function2 14 | echo Inside function 2 15 | echo The arguments are: %* 16 | goto :eof 17 | -------------------------------------------------------------------------------- /is-admin-1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | whoami.exe /priv | find "SeTakeOwnershipPrivilege" > nul 8 | 9 | if errorlevel 1 ( 10 | echo Requires admin! 11 | goto :eof 12 | ) 13 | 14 | :start 15 | 16 | echo Script running as admin... 17 | -------------------------------------------------------------------------------- /echo-test.bat: -------------------------------------------------------------------------------- 1 | :: Basic echo 2 | echo This line is echoed 3 | @echo This line is not echoed 4 | @echo off 5 | echo I am under echo off 6 | ECHO. 7 | echo me too 8 | @echo on 9 | echo Hello, echo is back on! 10 | 11 | 12 | :: Echo 2 13 | @echo off 14 | set /p "=hello "nul 2>&1 || ( 10 | echo Failed to go to that label! 11 | goto :eof 12 | ) 13 | 14 | echo Unreachable code 15 | 16 | :ValidLabel 17 | echo ValidLabel! 18 | goto :eof -------------------------------------------------------------------------------- /batch-perl.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | 8 | perl -x "%~f0" %* 9 | goto :eof 10 | 11 | #!perl 12 | print "Hello world from Perl!\n"; 13 | 14 | my $i = 1; 15 | foreach my $arg(@ARGV) { 16 | print "Argument #$i: $arg\n"; 17 | $i++; 18 | } 19 | -------------------------------------------------------------------------------- /deve-test3.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | SETLOCAL ENABLEDELAYEDEXPANSION 8 | SET C=-1 9 | FOR /L %%a IN (1, 1, 4) DO ( 10 | set C=%%a 11 | echo a=%%a, regular expansion of C=%C% and delayed expansion of C=!C! 12 | ) 13 | 14 | ECHO After the loop, C=%C% -------------------------------------------------------------------------------- /batch-perl-end.bat: -------------------------------------------------------------------------------- 1 | @goto runperl 2 | #!perl 3 | print "Hello world from Perl!\n"; 4 | 5 | my $i = 1; 6 | foreach my $arg(@ARGV) { 7 | print "Argument #$i: $arg\n"; 8 | $i++; 9 | } 10 | 11 | __END__ 12 | :runperl 13 | 14 | :: 15 | :: The Batchography book by Elias Bachaalany 16 | :: 17 | 18 | @perl -x "%~f0" %* -------------------------------------------------------------------------------- /deve-test1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal ENABLEDELAYEDEXPANSION 8 | set value=1 9 | 10 | if "%value%"=="1" ( 11 | set value=2 12 | echo The value inside the IF block is '!value!' 13 | ) 14 | 15 | echo The value after the IF block is '%value%' -------------------------------------------------------------------------------- /for-each-char-1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :main 4 | setlocal 5 | 6 | set /p str=Enter a string: 7 | 8 | set i=0 9 | :repeat 10 | call set char_i=%%str:~%i%,1%% 11 | if not defined char_i goto break 12 | echo char[%i%]=%char_i% 13 | set /a i+=1 14 | goto repeat 15 | :break 16 | 17 | endlocal 18 | -------------------------------------------------------------------------------- /for-each-char-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char buf[100]; 7 | 8 | printf("Enter a string:"); 9 | char *input = gets_s(buf, _countof(buf)); 10 | if (input != NULL) 11 | { 12 | while (*input != '\0') 13 | printf("%c\n", *input++); 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /get-ftime.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | 5 | if "!%1"=="" ( 6 | echo Please pass a file name 7 | goto :eof 8 | ) 9 | 10 | call :get-file-ftime %1 ftime 11 | echo file time is = %ftime% 12 | 13 | goto :eof 14 | 15 | 16 | :get-file-ftime <1=filename> <2=result-var> 17 | set %~2=%~t1 18 | exit /b 0 -------------------------------------------------------------------------------- /str-substitute.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | SETLOCAL ENABLEDELAYEDEXPANSION 8 | 9 | SET str1=Writing code using the Python scripting language. 10 | ECHO Regular expansion: %str1:Python=Batch% 11 | 12 | ECHO With DEVE: !str1:Python=Batch! 13 | 14 | ENDLOCAL -------------------------------------------------------------------------------- /for-file-lines-no-options.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | 8 | setlocal enabledelayedexpansion 9 | 10 | set /a linecount=0 11 | 12 | for /F %%a IN (text1.txt) DO ( 13 | set /a linecount=linecount+1 14 | ECHO Line #!linecount!: %%a 15 | ) 16 | 17 | endlocal -------------------------------------------------------------------------------- /get-fattr.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | 5 | if "!%1"=="" ( 6 | echo Please pass a file name 7 | goto :eof 8 | ) 9 | 10 | call :get-file-attr %1 attr 11 | echo file attributes are: %attr% 12 | 13 | goto :eof 14 | 15 | 16 | :get-file-attr <1=filename> <2=result-var> 17 | set %~2=%~a1 18 | exit /b 0 -------------------------------------------------------------------------------- /env-export-for.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | :: 3 | :: The Batchography book by Elias Bachaalany 4 | :: 5 | 6 | setlocal 7 | 8 | set variable1=hello 9 | set variable2=world 10 | 11 | set result=%variable1% %variable2% 12 | 13 | for /f "usebackq tokens=* delims=" %%a in ('%result%') do ( 14 | endlocal 15 | set result=%%a 16 | goto :eof 17 | ) -------------------------------------------------------------------------------- /pack-tracked.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | set L=rarlst.lst 10 | set A=archive.rar 11 | 12 | if exist %A% del %A% 13 | 14 | git ls-files >%L% 15 | 16 | "c:\Program Files\WinRAR\rar.exe" a %A% @%L% 17 | 18 | if exist %L% del %L% -------------------------------------------------------------------------------- /str-substitute-var.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | :: 3 | :: The Batchography book by Elias Bachaalany 4 | :: 5 | 6 | set /P phrase=Enter string: 7 | set /P match=Find what: 8 | set /P replace=Replace with what: 9 | 10 | echo The phrase is: %phrase% 11 | call set phrase=%%phrase:%match%=%replace%%% 12 | echo After substitution, the phrase is: %phrase% -------------------------------------------------------------------------------- /func-call-fatal.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | call :L1 8 | 9 | echo after L1 10 | 11 | goto :eof 12 | 13 | :L1 14 | 15 | echo Fatal error in L1. Terminating the script! 16 | 17 | call :TermScript 2>nul 18 | goto :eof 19 | 20 | 21 | :TermScript 22 | if 23 | exit /b -------------------------------------------------------------------------------- /expenses.csv: -------------------------------------------------------------------------------- 1 | Date,Total,Description,Category 2 | 11/27/2011,350,Google Nexus 5 phone,Electronics 3 | 11/27/2011,20,Dinner + tips,Food 4 | 11/27/2011,35,Grocery for the week,Grocery 5 | 11/28/2011,7,Sandwich,Food 6 | 11/29/2015,3,Mailed a book,Postage 7 | 11/29/2015,20,Lunch + tips,Food 8 | 11/29/2015,65,Tablet stylus,Electronics 9 | 11/29/2015,40,Refueled the car,Gas -------------------------------------------------------------------------------- /env-export-temp-files.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal 8 | 9 | set variable1=1 10 | set variable2=2 11 | 12 | set /A result=variable1 + variable2 13 | 14 | echo set result=%result% >%~dpn0-state.bat 15 | 16 | endlocal 17 | 18 | call %~dpn0-state.bat 19 | del %~dpn0-state.bat 20 | -------------------------------------------------------------------------------- /for-file-lines-eol.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | :: 3 | :: The Batchography book by Elias Bachaalany 4 | :: 5 | 6 | setlocal enabledelayedexpansion 7 | 8 | ECHO EOL 9 | ECHO ---------------------- 10 | ECHO. 11 | SET Line=1 12 | for /f "eol=; tokens=*" %%a in (text-eol-1.txt) DO ( 13 | ECHO Line^(!Line!^)=%%a 14 | SET /A Line=Line+1 15 | ) 16 | 17 | endlocal -------------------------------------------------------------------------------- /for-string-token1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal enabledelayedexpansion 4 | 5 | rem Without usebackq 6 | for /f "tokens=1-3 delims=: " %%a in ('time /t') DO ( 7 | echo hour=%%a mins=%%b ampm=%%c 8 | ) 9 | 10 | rem usebackq 11 | for /f "usebackq tokens=1-3 delims=: " %%a in (`time /t`) DO ( 12 | echo tok1=%%a tok2=%%b tok3=%%c 13 | ) 14 | 15 | endlocal -------------------------------------------------------------------------------- /get-fsize.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal 8 | 9 | if "!%1"=="" ( 10 | echo Please pass a file name 11 | goto :eof 12 | ) 13 | 14 | call :get-file-size %1 15 | echo file size = %errorlevel% 16 | 17 | goto :eof 18 | 19 | 20 | :get-file-size <1=filename> 21 | exit /b %~z1 -------------------------------------------------------------------------------- /console-editor.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | :: 3 | :: The Batchography book by Elias Bachaalany 4 | :: 5 | 6 | if "%1"=="" ( 7 | echo Please specify the file name to create. 8 | goto :eof 9 | ) 10 | 11 | echo Please start typing. Press Ctrl+Z to finish. 12 | 13 | copy CON %1 >nul 14 | 15 | echo. 16 | echo Excellent. You typed the following: 17 | if exist %1 type %1 -------------------------------------------------------------------------------- /comments.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | : this is a comment 4 | :: this is a comment 5 | REM this is a comment too 6 | 7 | ECHO Hello &::This is a comment 8 | 9 | ECHO Multiple line comments 10 | GOTO after_comment 11 | Hello 12 | This is really free text! 13 | The interpreter will not execute those lines 14 | 15 | :after_comment 16 | ECHO End of multiple line comments 17 | -------------------------------------------------------------------------------- /env-expand-2.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal 8 | 9 | set VARNAME=Name 10 | set Name=Albert 11 | 12 | echo The variable name is '%VARNAME% 13 | echo Single level: the variable value designated by VARNAME is: '%%%VARNAME%%%' 14 | call echo Two levels: The variable value designated by VARNAME is: '%%%VARNAME%%%' -------------------------------------------------------------------------------- /localvars.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal 8 | 9 | set Name=Mr. Anderson 10 | 11 | echo The %%Name%% variable will not be exported to the interpreter or calling script 12 | 13 | endlocal 14 | 15 | set Age=33 16 | 17 | echo The %%Age%% variable will be exported to the parent's environment variable space. -------------------------------------------------------------------------------- /batch-jscript.bat: -------------------------------------------------------------------------------- 1 | @if (1 == 0) @end /* 2 | @echo off 3 | 4 | :: 5 | :: The Batchography book by Elias Bachaalany 6 | :: 7 | 8 | cscript.exe /E:jscript /nologo "%~f0" %* 9 | goto :eof */ 10 | WScript.Echo("Hello world from Windows Scripting Host!"); 11 | 12 | for (var i=0, c=WScript.Arguments.length;i %result-var% 16 | for /f "usebackq tokens=3*" %%a in (`net config workstation ^| findstr /c:"Software version"`) DO ( 17 | set %~1=%%a %%b 18 | ) 19 | goto :eof -------------------------------------------------------------------------------- /env-export-compound-simple.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | :: Clear previous values 8 | set result1= 9 | set result2= 10 | 11 | :: Start localization 12 | setlocal 13 | 14 | set r1=Hello 15 | set r2=World 16 | 17 | :: Compound statement 18 | ( 19 | :: End localization 20 | endlocal 21 | set result1=%r1% 22 | set result2=%r2% 23 | ) 24 | -------------------------------------------------------------------------------- /is-admin-2.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal 8 | 9 | set TEMPFN=%WINDIR%\test-admin-%~nx0-%RANDOM%.tmp 10 | copy "%~f0" "%TEMPFN%" /y >nul 2>&1 11 | if %ERRORLEVEL% equ 0 ( 12 | del "%TEMPFN%" 13 | ) else ( 14 | echo Requires admin privilege! 15 | goto end 16 | ) 17 | echo The rest of the script goes here... 18 | 19 | :end 20 | endlocal -------------------------------------------------------------------------------- /env-group-naming.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal 8 | call :add-two 5 10 9 | echo Result=%errorlevel% 10 | goto :end 11 | 12 | :add-two <1=num1> <2=num2> => result => %errorlevel% 13 | set addtwo.n1=%1 14 | set addtwo.n2=%2 15 | set /A addtwo.result="%addtwo.n1% + %addtwo.n2%" 16 | 17 | exit /b %addtwo.result% 18 | 19 | :end 20 | endlocal -------------------------------------------------------------------------------- /for-each-char-2.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | :main 8 | setlocal enabledelayedexpansion 9 | 10 | rem set /p str=Enter a string: 11 | set str=A string is 123 12 | 13 | for /l %%a in (0,1,4096) do ( 14 | set char_i=!str:~%%a,1! 15 | if not defined char_i goto break 16 | echo char[%%a] is '!char_i!' 17 | ) 18 | :break 19 | echo Done! 20 | 21 | goto :eof -------------------------------------------------------------------------------- /array-2d.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal enabledelayedexpansion 4 | 5 | set TABLE[0][0]=Elias 6 | set TABLE[0][1]=Ashmole 7 | set TABLE[1][0]=John 8 | set TABLE[1][1]=Smith 9 | set TABLE[2][0]=Mary 10 | set TABLE[2][1]=Jane 11 | 12 | 13 | echo The names we have in the table are: 14 | echo ----------------------------------- 15 | for /L %%a IN (0, 1, 2) do ( 16 | echo [%%a] FirstName: '!TABLE[%%a][0]!' LastName: '!TABLE[%%a][1]!' 17 | ) -------------------------------------------------------------------------------- /call-script1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | if "%1"=="" ( 8 | echo FATAL ERROR! No arguments passed! 9 | :: Terminate the caller as well 10 | EXIT 11 | ) 12 | 13 | echo script1 called with: %* 14 | 15 | if "%1"=="softquit" ( 16 | :: Terminate this script only 17 | exit /b 18 | ) 19 | 20 | :: Even if no EXIT is passed, script will terminate w/o terminating the caller! -------------------------------------------------------------------------------- /for-command-output-time.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | rem Without usebackq 10 | for /f "tokens=1-3 delims=: " %%a in ('time /t') DO ( 11 | echo hour=%%a mins=%%b ampm=%%c 12 | ) 13 | 14 | rem usebackq 15 | for /f "usebackq tokens=1-3 delims=: " %%a in (`time /t`) DO ( 16 | echo tok1=%%a tok2=%%b tok3=%%c 17 | ) 18 | 19 | endlocal -------------------------------------------------------------------------------- /transform-me-1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | if "%1"=="grep" ( 10 | for /F "usebackq tokens=3*" %%a in (`findstr /B /C:"static LPCWSTR XML_" transform-me-1.cpp`) DO ( 11 | set Z=%%b 12 | set Z=!Z: L"= "! 13 | echo public const string %%a !Z! 14 | ) 15 | goto :eof 16 | ) 17 | 18 | call %0 grep | sort 19 | 20 | endlocal -------------------------------------------------------------------------------- /annotated-code-sample.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | :: 3 | :: The Batchography book by Elias Bachaalany 4 | :: 5 | 6 | :main 7 | if exist log.txt del log.txt 8 | 9 | pushd compiler 10 | if not exist cl.exe ( 11 | echo The compiler was not found! 12 | goto :end 13 | ) 14 | 15 | cl.exe hello.c 16 | 17 | if %ERRORLEVEL% EQU 0 ( 18 | echo Compilation successful! 19 | ) ELSE ( 20 | echo Compilation failed! 21 | ) 22 | 23 | :end 24 | POPD -------------------------------------------------------------------------------- /func-call-invalid.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | echo -- Calling invalid function 8 | call :function-not-there 9 | echo After invalid function call -- 10 | 11 | echo. 12 | 13 | echo -- Calling function1 14 | call :function1 15 | echo After function 1 -- 16 | 17 | goto :eof 18 | 19 | :function1 20 | echo This is function 1 21 | rem Return from the function 22 | goto :eof 23 | -------------------------------------------------------------------------------- /array-1d.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | :: Create the array 10 | set V[0]=5 11 | set V[1]=10 12 | set V[2]=15 13 | 14 | :: Display all values 15 | for /L %%a in (0, 1, 2) do ( 16 | echo V[%%a]=!V[%%a]! 17 | ) 18 | 19 | :: Delete the array 20 | for /L %%a in (0, 1, 2) do ( 21 | set V[%%a]= 22 | ) 23 | 24 | :: Display the array values 25 | set V[ 26 | -------------------------------------------------------------------------------- /str-substitute-asterisk.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | for %%a in ( 10 | "01:31:2016 at 4:17AM - LOGENTRY: User 'Jane' has logged in" 11 | "01:31:2016 at 4:17AM - LOGENTRY: File 'payroll.db' has been accessed" 12 | "01:31:2016 at 4:55AM - LOGENTRY: User 'Jane' logged out" 13 | ) do ( 14 | set V=%%~a 15 | set V=!V:*LOGENTRY: =! 16 | echo !V! 17 | ) 18 | 19 | endlocal -------------------------------------------------------------------------------- /useful_scripts/exec_strings.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany -- http://amzn.to/1X3tQ4K 5 | :: 6 | 7 | :: 8 | :: Use SysInternal's 'strings.exe' tool to extract strings from executable files 9 | :: 10 | 11 | if exist strs.txt del strs.txt 12 | 13 | for %%a in (*.dll *.exe *.ocx *.sys) do ( 14 | echo --- begin strings: %%~fa --- >>strs.txt 15 | "%tools%sysinternals\strings.exe" "%%~fa" >>strs.txt 16 | echo --- end strings: %%~fa --- >>strs.txt 17 | ) 18 | -------------------------------------------------------------------------------- /debug-1.bat: -------------------------------------------------------------------------------- 1 | setlocal 2 | 3 | :: statement #1 4 | if "%1"=="" ( 5 | echo ^>^>no argument 6 | echo ^>^>was passed 7 | ) else ( 8 | echo ^>^>argument passed! 9 | ) 10 | 11 | echo ^>^>this is 2nd statement 12 | 13 | if "%2"=="" echo ^>^>no second argument passed && goto :eof 14 | 15 | echo this is 3rd statement 16 | rem check the third argument 17 | if "%3"=="" ( 18 | echo ^>^>3rd arg not passed 19 | ) else ( 20 | echo ^>^>1 21 | if "%3"=="abc" ( 22 | echo ^>^>abc 23 | ) 24 | ) -------------------------------------------------------------------------------- /str-substring.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | SETLOCAL 8 | 9 | REM 012345 10 | SET str1=ABCDEF 11 | 12 | ECHO The string is: '%str1%' 13 | ECHO [0, 1 ] = %str1:~0,1% 14 | ECHO [2, 2 ] = %str1:~2,2% 15 | ECHO [3, ] = %str1:~3% 16 | ECHO [-2, ] = %str1:~-2% 17 | ECHO [1, -3] = %str1:~1,-3% 18 | 19 | ECHO [1, -2] = %str1:~1,-2% 20 | ECHO [0, -1] = %str1:~0,-1% 21 | ECHO [0, -2] = %str1:~0,-2% 22 | 23 | ENDLOCAL -------------------------------------------------------------------------------- /batch-python.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | rem = """ 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | :: Anything batch file goes here but will be ignored by Python 8 | python -x "%~f0" %* 9 | exit /b %errorlevel% 10 | :: End of batch file commands 11 | """ 12 | 13 | # Anything here is interpreted by Python 14 | 15 | import platform 16 | import sys 17 | 18 | print("Hello world from Python %s!\n" % platform.python_version()) 19 | print("The passed arguments are: %s" % sys.argv[1:]) -------------------------------------------------------------------------------- /goto-label-resilient-findstr.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | if "%1"=="" ( 8 | echo No label passed, please pass a label name 9 | goto :eof 10 | ) 11 | 12 | findstr /X /C:":%~1" "%~f0">nul && goto %~1 || ( 13 | echo The label "%~1" does not exist! 14 | exit /b -1 15 | ) 16 | 17 | goto :eof 18 | 19 | :mylabel1 20 | echo this is mylabel1 21 | goto :eof 22 | 23 | :mylabel2 24 | echo this is mylabel2 25 | goto :eof 26 | -------------------------------------------------------------------------------- /get-two-randoms.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: Script to illustrate how to export environment variables 4 | 5 | setlocal 6 | 7 | call :get-two-randoms res1 res2 8 | echo Checking if environment is polluted: 9 | set v1 & set v2 10 | echo. 11 | echo Checking results: 12 | set res 13 | goto :eof 14 | 15 | :get-two-randoms <1=out1> <2=out2> 16 | setlocal 17 | set v1=%RANDOM% 18 | set v2=%RANDOM% 19 | ( 20 | endlocal 21 | set "%~1=%v1%" 22 | set "%~2=%v2%" 23 | goto :eof 24 | ) -------------------------------------------------------------------------------- /findstr-test.txt: -------------------------------------------------------------------------------- 1 | This is a simple example file 2 | With a few lines 3 | 123 4 | Of Various formats 5 | //End 10 | This line contains a fox 11 | This line contains a chicken 12 | And this one contains a bird 13 | 456 14 | module!offset: 0f 00 00 00 00 00 00 00 00 01 15 | kernel32!77441122: 00 00 00 00 00 00 00 00 00 02 16 | user32!6A112233: 00 00 00 00 00 00 00 00 00 03 17 | @echo Directive 1 18 | @echo Directive 2 19 | @echo Directive 3 20 | 789 21 | Some other lines -------------------------------------------------------------------------------- /get-ftime-secs.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | if "!%1"=="" ( 5 | echo Please pass a file name 6 | goto :eof 7 | ) 8 | 9 | call :get-file-ftime %1 ftime 10 | echo file time is = %ftime% 11 | 12 | goto :eof 13 | 14 | 15 | :get-file-ftime <1=filename> <2=result-var> 16 | setlocal 17 | set P=%~dp1 18 | set P=%P:~0,-1% 19 | for /f "usebackq tokens=* delims=" %%a in ( 20 | `forfiles /P "%P%" /M "%~nx1" /c "cmd /c echo @ftime"`) DO ( 21 | endlocal 22 | set %~2=%%a 23 | ) 24 | exit /b 0 -------------------------------------------------------------------------------- /func-call-resilient.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | echo -- Calling invalid function 8 | call :function-not-there 2>nul || ( 9 | echo The CALL failed...opting to resume... 10 | :: You may decide to just "EXIT /B" 11 | ) 12 | echo After invalid function call -- 13 | 14 | echo. 15 | 16 | echo -- Calling function1 17 | call :function1 18 | echo After function 1 -- 19 | 20 | goto :eof 21 | 22 | :function1 23 | echo This is function 1 24 | rem Return from the function 25 | goto :eof 26 | -------------------------------------------------------------------------------- /str-len.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | 4 | :: 5 | :: The Batchography book by Elias Bachaalany 6 | :: 7 | 8 | :main 9 | 10 | setlocal enabledelayedexpansion 11 | 12 | call :str-len "marco polo" result 13 | 14 | echo result=%result% 15 | 16 | endlocal 17 | 18 | goto :eof 19 | 20 | :str-len (string, result-var) 21 | set len=0 22 | set str=%~1 23 | :str-len-repeat-1 24 | if not defined str goto strlen-break-1 25 | set str=%str:~1% 26 | set /a len+=1 27 | goto str-len-repeat-1 28 | 29 | :strlen-break-1 30 | set %~2=%len% 31 | goto :eof 32 | -------------------------------------------------------------------------------- /for-nest-sum-csv.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | REM Single loop method 10 | SET /A Total=0 11 | for /F "skip=1 tokens=2 delims=," %%a in (expenses.csv) do ( 12 | set /a Total=Total+%%a 13 | ) 14 | 15 | echo Total = %Total% 16 | echo. 17 | 18 | REM Nested loop demo 19 | SET /A Total=0 20 | for /F "skip=1 tokens=*" %%a in (expenses.csv) do ( 21 | for /f "tokens=2 delims=," %%b in ("%%a") do ( 22 | set /a Total=Total+%%b 23 | ) 24 | ) 25 | 26 | echo Total = %Total% -------------------------------------------------------------------------------- /DetectWinLanguage.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | 5 | :: https://docs.microsoft.com/en-us/previous-versions/office/developer/speech-technologies/hh361638(v=office.14) 6 | :: 0409 English ; 0407 German ; 040C French ; 0C0A Spanish 7 | 8 | for /F "usebackq tokens=3" %%a IN (`reg query "hklm\system\controlset001\control\nls\language" /v Installlanguage`) DO ( 9 | set lang_id=%%a 10 | ) 11 | 12 | if "%lang_id%"=="0409" ( 13 | echo English detected 14 | ) else if "%lang_id%" == "040C" ( 15 | echo French detected 16 | ) else ( 17 | echo Note: Unknown language ID %lang_id%! 18 | ) 19 | 20 | echo LangID=%lang_id% -------------------------------------------------------------------------------- /str-len-2.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | :main 8 | 9 | setlocal enabledelayedexpansion 10 | 11 | set /p str=Enter a string: 12 | call :str-len %str% 13 | 14 | echo result=%errorlevel% 15 | 16 | endlocal 17 | 18 | goto :eof 19 | 20 | :str-len string 21 | setlocal 22 | set len=0 23 | set str=%~1 24 | :str-len-repeat-1 25 | if not defined str goto strlen-break-1 26 | set str=%str:~1% 27 | set /a len+=1 28 | goto str-len-repeat-1 29 | 30 | :strlen-break-1 31 | exit /b %len% 32 | goto :eof 33 | -------------------------------------------------------------------------------- /use-lib.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | 4 | set /P "S=Enter string: " 5 | call lib-batch strlen "%S%" 6 | echo The string "%S%" length is %errorlevel% 7 | 8 | echo. 9 | set /P "N=Enter number: " 10 | call lib-batch is-odd %N% 11 | if %errorlevel%==1 (echo The number %N% is Odd!) ELSE (echo The number %N% is even!) 12 | 13 | echo. 14 | call lib-batch get-tempfile tmpfil tmp S 15 | echo Generated temp file name at: %S% 16 | 17 | echo. 18 | set /P "S=Enter drive letter: " 19 | call lib-batch volserial %S% N 20 | echo The volume serial number of drive %S% is: %N% 21 | endlocal -------------------------------------------------------------------------------- /if-ex.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal ENABLEEXTENSIONS 4 | 5 | set GodMode= 6 | 7 | SET /P Name="Please enter your name: " 8 | IF /I "%Name%" EQU "iddqd" ( 9 | ECHO God mode enabled! 10 | SET GodMode=1 11 | ) 12 | 13 | SET /P Age="Please enter your age: " 14 | IF %Age% LSS 18 ( 15 | ECHO Sorry, you're under age...terminating. 16 | GOTO :EOF 17 | ) 18 | 19 | 20 | if %Age% GEQ 90 ( 21 | if NOT DEFINED GodMode ( 22 | ECHO Whooa, you still have the spirit mortal! 23 | ) 24 | ) 25 | 26 | echo Let the party begin... 27 | if DEFINED GodMode ( 28 | ECHO !! God mode ON. You earned unlimited free drinks !! 29 | ) 30 | endlocal -------------------------------------------------------------------------------- /array-assoc-1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal enabledelayedexpansion 4 | 5 | set USERS[moeh]=Moe Howard 6 | set USERS[shemph]=Shemp Howard 7 | set USERS[larryf]=Larry Fine 8 | 9 | for /f "usebackq delims=" %%a in (`set USERS[`) do ( 10 | for /f "usebackq tokens=1,2 delims==" %%m in ('%%a') do ( 11 | set __k=%%m& set __k=!__k:*[=!& set __k=!__k:]=! 12 | set __v=%%n 13 | echo key = '!__k!' value = '!__v!' 14 | ) 15 | ) 16 | 17 | echo. 18 | 19 | set /p user=Enter user name: 20 | if not defined USERS[%user%] ( 21 | echo User '%user%' was not found in the system! 22 | ) else ( 23 | echo %user%'s full name is: !USERS[%user%]! 24 | ) 25 | 26 | endlocal -------------------------------------------------------------------------------- /for-file-lines-usebackq.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal enabledelayedexpansion 4 | 5 | ECHO /F switch with quotes 6 | ECHO ----------------------- 7 | ECHO. 8 | FOR /F "tokens=1-2" %%a IN ("the long text file name.txt") DO ( 9 | echo Line: %%a %%b 10 | ) 11 | 12 | ECHO usebackq #1 13 | ECHO ------------- 14 | ECHO. 15 | FOR /F "usebackq tokens=1-2" %%a IN ("the long text file name.txt") DO ( 16 | echo Line: %%a %%b 17 | ) 18 | 19 | ECHO usebackq #2 20 | ECHO ------------- 21 | ECHO. 22 | FOR /F "usebackq tokens=1-2" %%a IN ("the long text file name.txt" text1.txt "the long text file name 2.txt") DO ( 23 | echo Line: %%a %%b 24 | ) 25 | 26 | endlocal -------------------------------------------------------------------------------- /str-len-fast.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :main 4 | setlocal 5 | set /P str=Enter string: 6 | call :strlen "%str%" 7 | echo length=%errorlevel% 8 | 9 | call :strlen "" 10 | echo length of empty string=%errorlevel% 11 | 12 | goto :eof 13 | 14 | :strlen <1=string> => errorlevel 15 | setlocal enabledelayedexpansion 16 | set "s=%~1." 17 | set len=0 18 | for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( 19 | if "!s:~%%P,1!" NEQ "" ( 20 | set /a "len+=%%P" 21 | set "s=!s:~%%P!" 22 | ) 23 | ) 24 | 25 | ( 26 | endlocal 27 | exit /b %len% 28 | ) -------------------------------------------------------------------------------- /contains-str-1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | rem Check if a pattern is contained inside a string without using any external commands 8 | 9 | :main 10 | setlocal 11 | 12 | set /p str=Enter text: 13 | set /p pattern=Enter pattern: 14 | call :str-contains "%str%" "%pattern%" 15 | echo found = %errorlevel% 16 | 17 | goto :eof 18 | 19 | :str-contains -> errorlevel 20 | 21 | setlocal 22 | 23 | set copy=%~1 24 | set repl=%~2 25 | 26 | call set copy=%%copy:%repl%=%% 27 | 28 | ( 29 | endlocal 30 | if "%~1"=="%copy%" (exit /b 0 ) else (exit /b 1) 31 | ) 32 | -------------------------------------------------------------------------------- /args-modifiers.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | echo The first argument as is: %1 4 | 5 | :: Start modifiers example 6 | echo 1. without quotes: %~1 7 | echo 2. fully qualified path name: %~f1 8 | echo 3. drive letter: %~d1 9 | echo 4. path part: %~p1 10 | echo 5. just the file name part: %~n1 11 | echo 6. just the extension part: %~x1 12 | echo 7. file's attributes: %~a1 13 | echo 8. file's date and time: %~t1 14 | echo 9. file's size: %~z1 15 | echo 10. file path in the PATH environment variable search: %~$PATH:1 16 | echo 11. file's full path: %~dp1 17 | echo 12. file's name and extension part: %~nx1 18 | echo 13. 'dir' like modifier: %~ftza1 19 | echo 14. fully qualified script path: %~dpnx0 -------------------------------------------------------------------------------- /coding-style.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | :start 8 | rem Usually, parameters are parsed here 9 | 10 | if "%1"=="help" goto usage 11 | if "%1"=="compress" goto compress 12 | 13 | goto usage 14 | 15 | :usage 16 | echo. 17 | echo Example usage: 18 | echo ---------------- 19 | echo %0 help - Shows the help screen 20 | echo %0 compress - Start the compression 21 | goto end 22 | 23 | :compress 24 | if "%PROCESSOR_ARCHITECTURE%"=="AMD64" ( 25 | echo Sorry, this tool requires an x86 operating system 26 | goto end 27 | ) 28 | 29 | rem Do the compression 30 | goto end 31 | 32 | 33 | :end 34 | -------------------------------------------------------------------------------- /env-export-compound.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | :: Make sure no result is set previously 5 | set result1= 6 | set result2= 7 | set result3= 8 | 9 | :: Call the function 10 | call :get-results 11 | 12 | :: Check results 13 | set result 14 | 15 | goto :eof 16 | 17 | :get-results 18 | setlocal enabledelayedexpansion 19 | set s= 20 | for /l %%a in (1, 1, 5) do ( 21 | set s=!s!%%a 22 | if "%%a"=="4" ( 23 | set /a retval1=!s! 24 | set /a retval2=%%a * 2 25 | set /a retval3=%%a * 4 26 | goto get-result-return 27 | ) 28 | ) 29 | goto :eof 30 | 31 | :get-result-return 32 | ( 33 | endlocal 34 | set result1=%retval1% 35 | set result2=%retval2% 36 | set result3=%retval3% 37 | ) -------------------------------------------------------------------------------- /args-modifiers-0.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | echo Self is: %0 8 | 9 | :: Start modifiers example 10 | echo 1. without quotes: %~0 11 | echo 2. fully qualified path name: %~f0 12 | echo 3. drive letter: %~d0 13 | echo 4. path part: %~p0 14 | echo 5. just the file name part: %~n0 15 | echo 6. just the extension part: %~x0 16 | echo 7. file's attributes: %~a0 17 | echo 8. file's date and time: %~t0 18 | echo 9. file's size: %~z0 19 | echo 10. file path in the PATH environment variable search: %~$PATH:0 20 | echo 11. file's full path: %~dp0 21 | echo 12. file's name and extension part: %~nx0 22 | echo 13. 'dir' like modifier: %~ftza0 23 | echo 14. fully qualified script path: %~dpnx0 -------------------------------------------------------------------------------- /for-file-lines-token.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | 5 | echo Token #2 from each line: 6 | echo ------------------------ 7 | for /F "tokens=2" %%a IN (text-tokens-1.txt) DO ( 8 | echo %%a 9 | ) 10 | 11 | 12 | echo Token #3 to Token #5 from each line: 13 | echo ------------------------------------ 14 | 15 | for /F "tokens=3-5" %%a IN (text-tokens-1.txt) DO ( 16 | echo %%a %%b %%c 17 | ) 18 | 19 | 20 | echo Token #2 and onwards: 21 | echo --------------------- 22 | 23 | for /F "tokens=2*" %%a IN (text-tokens-1.txt) DO ( 24 | echo %%a %%b 25 | ) 26 | 27 | echo Token #6, #5 and #1 to #3: 28 | echo -------------------------- 29 | 30 | for /F "tokens=6,5,1-3" %%a IN (text-tokens-1.txt) DO ( 31 | echo %%a %%b %%c %%d %%e 32 | ) 33 | 34 | endlocal -------------------------------------------------------------------------------- /batch-ftp.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | set LINENO=-1 10 | FOR /f "useback tokens=1 delims=: " %%a in (`findstr /N /C:"$$EMBEDDEDSCRIPT$$" "%~f0"`) DO SET /A LINENO=%%a 11 | 12 | if "%LINENO%"=="-1" ( 13 | echo Error: No embedded script found! 14 | exit /b -1 15 | ) 16 | 17 | set TEMPFN=%TEMP%\%~n0%RANDOM%.compel 18 | (FOR /F "useback skip=%LINENO% delims=" %%a in ("%~f0") DO ( 19 | echo %%a 20 | ))>%TEMPFN% 21 | 22 | if exist %TEMPFN% ( 23 | ftp -s:"%TEMPFN%" ftp.microsoft.com 24 | del %TEMPFN% 25 | ) 26 | goto :EOF 27 | 28 | 29 | $$EMBEDDEDSCRIPT$$ 30 | anonymous 31 | ebooks@passingtheknowledge.net 32 | cd Products/mspress/library 33 | ls 34 | quit -------------------------------------------------------------------------------- /expr-1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: Do not expose all the environment variables used in this script 4 | SETLOCAL 5 | 6 | :: Simple expression 7 | 8 | SET /A var1="1+(2*4)" 9 | 10 | :: Referring to another variable without enclosing with the percentage character 11 | SET /A var2=var1 + 2 12 | 13 | :: Bitwise AND 14 | SET /A var3="var1 & 1" 15 | 16 | :: Modulus 17 | SET /A var4="var1 %% 2" 18 | 19 | :: Shift to the left 20 | SET /A var5="1<<7" 21 | 22 | :: Bitwise negation 23 | SET /A var6="~var1" 24 | 25 | :: Defining hexadecimal number 26 | SET /A var7="0x1234" 27 | 28 | :: Shift left assignment 29 | SET /A var8=1 30 | SET /A var8^<^<=2 31 | 32 | :: Display all the variables 33 | SET var 34 | 35 | IF %var3% EQU 1 echo var1 is odd 36 | 37 | ENDLOCAL -------------------------------------------------------------------------------- /transform-me-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // 4 | // XML constants begin 5 | // 6 | static LPCWSTR XML_PROVIDERS = L"Providers"; 7 | static LPCWSTR XML_PROVIDER = L"Provider"; 8 | static LPCWSTR XML_NAME = L"Name"; 9 | static LPCWSTR XML_GUID = L"Guid"; 10 | static LPCWSTR XML_METADATA = L"Metadata"; 11 | static LPCWSTR XML_OPCODES = L"Opcodes"; 12 | static LPCWSTR XML_OPCODE = L"Opcode"; 13 | static LPCWSTR XML_VALUE = L"Value"; 14 | static LPCWSTR XML_VERSION = L"Version"; 15 | static LPCWSTR XML_KEYWORDS = L"Keywords"; 16 | static LPCWSTR XML_KEYWORD = L"Keyword"; 17 | static LPCWSTR XML_TEMPLATE = L"Template"; 18 | static LPCWSTR XML_TASK = L"Task"; 19 | // 20 | // XML constants end 21 | // 22 | 23 | int main() 24 | { 25 | // ....long program... 26 | return 0; 27 | } -------------------------------------------------------------------------------- /io-redir.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | if "%1"=="clean" ( 4 | del newfile.txt 5 | del file2.txt 6 | del stderr-file.txt 7 | del out.txt 8 | del stdout-file.txt 9 | del out2.txt 10 | goto :eof 11 | ) 12 | 13 | ECHO This will be written to a file>newfile.txt 14 | ECHO This will be appended or written to file2.txt >>file2.txt 15 | ECHO This will also be written to file2.txt >>file2.txt 16 | ECHO This will overwrite newfile.txt>newfile.txt 17 | ECHO This will redirect the stdout and stderr to two separate text files>stdout-file.txt 2>stderr-file.txt 18 | ECHO This will redirect the stdout to a file and stderr to the same output >stdout-file.txt 2>&1 19 | ECHO Nothing will be displayed!>NUL 20 | 21 | 22 | >out.txt echo hello world! 23 | type out.txt 24 | 25 | >nul dir /w 26 | 27 | -------------------------------------------------------------------------------- /fib.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | for /l %%a in (1, 1, 8) do ( 10 | call :fib %%a 11 | echo fib^(%%a^)=!errorlevel! 12 | ) 13 | 14 | goto :eof 15 | 16 | :fib <1=n> => %errorlevel% 17 | :: Break recursion when n==0 or n==1 18 | if %1 LSS 0 ( 19 | echo Error: Cannot be called with a negative number! 20 | exit /b 0 21 | ) 22 | if "%1"=="0" exit /b 0 23 | if "%1"=="1" exit /b 1 24 | 25 | setlocal 26 | 27 | :: r1 = fib(n-1) 28 | set /a arg=%1-1 29 | call :fib %arg% 30 | set r1=%errorlevel% 31 | 32 | :: r2 = fib(n-2) 33 | set /a arg=%1-2 34 | call :fib %arg% 35 | set r2=%errorlevel% 36 | 37 | :: return result 38 | set /a r=r1+r2 39 | exit /b %r% -------------------------------------------------------------------------------- /findstr-extract-between-markers.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | set FN=findstr-test.txt 10 | call :get-line-no "//End" "%FN%" 14 | set MEND=%errorlevel% 15 | 16 | if "%MBEGIN%"=="-1" goto error 17 | if "%MEND%"=="-1" goto error 18 | 19 | set /A C=MEND-MBEGIN-1 20 | 21 | for /f "useback skip=%MBEGIN% tokens=* delims=" %%a in ("%FN%") DO ( 22 | echo %%a 23 | SET /A C-=1 24 | if !C!==0 goto :eof 25 | ) 26 | 27 | goto :eof 28 | 29 | :get-line-no <1=string> <2=file> 30 | for /f "useback tokens=1 delims=:" %%a in (`findstr /N /C:"%~1" "%~2"`) DO EXIT /B %%a 31 | EXIT /B -1 32 | 33 | :error 34 | echo Could not find markers! 35 | exit /b -------------------------------------------------------------------------------- /batch-cpp.bat: -------------------------------------------------------------------------------- 1 | /* 2>nul && echo | set /p= 2 | @echo off 3 | setlocal 4 | echo. 5 | :: 6 | :: Self-compiling C++ Batch file! 7 | :: 8 | :: (c) Batchography book - by Elias Bachaalany 9 | :: 10 | 11 | set outfile="%~dpn0.exe" 12 | 13 | cl %~dpf0 /TP /EHsc /link /out:%outfile% /nologo > nul 14 | 15 | if exist %outfile% ( 16 | %outfile% 17 | del %outfile% 18 | del "%~dpn0.obj" 19 | ) else ( 20 | echo Compilation failed! 21 | ) 22 | goto :eof 23 | */ 24 | 25 | #include 26 | 27 | int main() 28 | { 29 | printf( 30 | "Hello world! I was self-compiled!\n" 31 | "\n" 32 | "Checkout the Batchography book!\n"); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /count-gotoif.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | SETLOCAL ENABLEEXTENSIONS 8 | 9 | :: ----------------- 10 | :: Forward counting 11 | :: ----------------- 12 | 13 | set /A I=1 14 | set /A C=10 15 | echo Counting from %I% to %C%: 16 | echo. 17 | 18 | :repeatcount1 19 | echo i = %I% 20 | SET /A I+=1 21 | if %I% GTR %C% goto donecount1 22 | 23 | goto repeatcount1 24 | :donecount1 25 | 26 | echo. 27 | echo Done counting forward! 28 | 29 | :: ----------------- 30 | :: Backward counting 31 | :: ----------------- 32 | set /A I=10 33 | set /A C=1 34 | echo Counting from %I% down to %C%: 35 | echo. 36 | 37 | :repeatcount2 38 | echo i = %I% 39 | SET /A I-=1 40 | if %I% LSS %C% goto donecount2 41 | 42 | goto repeatcount2 43 | :donecount2 44 | 45 | echo. 46 | echo Done counting backwards! 47 | 48 | 49 | endlocal -------------------------------------------------------------------------------- /args-check1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :main 4 | if "%1"=="" goto Usage 5 | if "%1"=="compress" goto compress 6 | if "%1"=="uncompress" goto uncompress 7 | 8 | rem Unknown command passed, display the usage! 9 | echo Unknown command '%1'!! 10 | goto usage 11 | 12 | :Usage 13 | ECHO %0 compress^|uncompress archive.zip 14 | goto end 15 | 16 | :compress 17 | if "%2"=="" ( 18 | echo No archive name was passed! 19 | goto Usage 20 | ) 21 | 22 | echo Compressing current folder into archive '%2' 23 | REM invoke the compression utility 24 | goto end 25 | 26 | :uncompress 27 | if "%2"=="" goto Usage 28 | if "%2"=="" ( 29 | echo No archive name was passed! 30 | goto Usage 31 | ) 32 | 33 | if not exist %2 ( 34 | ECHO The archive '%2' is not found! 35 | goto end 36 | ) 37 | echo Uncompressing the archive '%2' into the current folder 38 | REM invoke the decompression utility 39 | goto end 40 | 41 | :end -------------------------------------------------------------------------------- /batch-compel.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal enabledelayedexpansion 4 | 5 | set LINENO=-1 6 | FOR /f "useback tokens=1 delims=: " %%a in (`findstr /N /C:"$$EMBEDDEDSCRIPT$$" "%~f0"`) DO SET /A LINENO=%%a 7 | 8 | if "%LINENO%"=="-1" ( 9 | echo Error: No embedded script found! 10 | exit /b -1 11 | ) 12 | 13 | set TEMPFN=%TEMP%\%~n0%RANDOM%.compel 14 | (FOR /F "useback skip=%LINENO% delims=" %%a in ("%~f0") DO ( 15 | echo %%a 16 | ))>%TEMPFN% 17 | 18 | if exist %TEMPFN% ( 19 | ccompel %TEMPFN% 20 | del %TEMPFN% 21 | ) 22 | goto :EOF 23 | 24 | The following is a COMPEL script. COMPEL can be downloaded from here: 25 | - Documentation: http://lallouslab.net/2014/08/29/introducing-compel-a-command-based-interpreter-and-programming-language/ 26 | - Source code and binaries: https://github.com/lallousx86/compel/raw/master/BIN/compel_05302006.zip 27 | 28 | $$EMBEDDEDSCRIPT$$ 29 | var $msg "Hello world from COMPEL!" 30 | echo "$msg" -------------------------------------------------------------------------------- /debug-5.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal enabledelayedexpansion 4 | 5 | @if NOT DEFINED _CNDECHO (set _CNDECHO=^>NUL) 6 | 7 | :main 8 | setlocal 9 | set /P str=Enter string: 10 | echo DBG: the string value is %str% %_CNDECHO% 11 | call :strlen "%str%" 12 | echo length=%errorlevel% 13 | 14 | goto :eof 15 | 16 | :strlen <1=string> => errorlevel 17 | set "s=%~1." 18 | echo DBG: inside str-len, initial value is ^<%s%^> %_CNDECHO% 19 | set len=0 20 | for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( 21 | echo DBG: P=%%P str part is ^ %_CNDECHO% 22 | 23 | if "!s:~%%P,1!" NEQ "" ( 24 | set /a "len+=%%P" 25 | echo DBG: string length so far: !len! %_CNDECHO% 26 | set "s=!s:~%%P!" 27 | echo DBG: truncated string so far is: !s! %_CNDECHO% 28 | ) 29 | ) 30 | 31 | ( 32 | endlocal 33 | exit /b %len% 34 | ) -------------------------------------------------------------------------------- /test-lib.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal enabledelayedexpansion 4 | 5 | for %%a in ("abcdefg~7" "hello world~11" "hello~5" "a~1" "ab~2") do ( 6 | for /f "tokens=1,2 delims=~" %%b in (%%a) do ( 7 | echo Checking length for "%%b" 8 | call lib-batch strlen "%%b" 9 | set R=!errorlevel! 10 | echo Length is: !R! , expected length: %%c 11 | if !R! NEQ %%c ( 12 | :ErrStrlenfailed 13 | echo String length function failed. 14 | EXIT /B -1 15 | ) 16 | ) 17 | ) 18 | 19 | echo Testing empty string length 20 | call lib-batch strlen 21 | if %errorlevel% neq 0 goto ErrStrlenfailed 22 | echo Empty string length is zero 23 | 24 | set Odd=0 25 | FOR /L %%a IN (10, 1, 20) DO ( 26 | call lib-batch is-odd %%a 27 | set R=!errorlevel! 28 | echo Is odd '%%a'? =^> !R! 29 | if !R! NEQ !Odd! ( 30 | echo Odd test failed. 31 | EXIT /B -1 32 | ) 33 | if !Odd!==0 (set Odd=1) else (set Odd=0) 34 | ) 35 | 36 | echo ------------------- 37 | echo All tests passed! 38 | echo ------------------- -------------------------------------------------------------------------------- /gen-tables.py: -------------------------------------------------------------------------------- 1 | 2 | def gen_2chrs(): 3 | # A-Z 4 | __chr = [] 5 | for x in range(ord('A'), ord('Z')+1): 6 | __chr.append('"%d=%s"' % (x, chr(x))) 7 | 8 | # a-z 9 | for x in range(ord('a'), ord('z')+1): 10 | __chr.append('"%d=%s"' % (x, chr(x))) 11 | 12 | # 0-9 13 | for x in range(ord('0'), ord('9')+1): 14 | __chr.append('"%d=%s"' % (x, chr(x))) 15 | 16 | return "(%s)" % " ".join(__chr) 17 | 18 | 19 | def gen_upper2lower(): 20 | # A-Z 21 | __chr = [] 22 | for x in range(ord('A'), ord('Z')+1): 23 | __chr.append('"%s=%s"' % (chr(x), str(chr(x)).lower())) 24 | 25 | return "(%s)" % " ".join(__chr) 26 | 27 | def gen_lower2upper(): 28 | # A-Z 29 | __chr = [] 30 | for x in range(ord('a'), ord('z')+1): 31 | __chr.append('"%s=%s"' % (chr(x), str(chr(x)).upper())) 32 | 33 | return "(%s)" % " ".join(__chr) 34 | 35 | print("\n:: ord2chr") 36 | print(gen_2chrs()) 37 | 38 | print("\n:: gen_upper2lower") 39 | print(gen_upper2lower()) 40 | 41 | print("\n:: gen_lower2upper") 42 | print(gen_lower2upper()) -------------------------------------------------------------------------------- /ds-stacks.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | for /L %%a in (1, 1, 10) DO ( 10 | echo Push %%a 11 | call :push STK %%a 12 | ) 13 | 14 | echo. 15 | echo. 16 | :test-pop-more 17 | call :pop STK V 18 | if not defined V goto test-pop-no-more 19 | echo Pop -^> %V% 20 | goto test-pop-more 21 | :test-pop-no-more 22 | 23 | goto :eof 24 | 25 | 26 | :push <1=variable> <2=value> [3=value, 4=value, ...] 27 | :push-more 28 | call set %~1=%~2~%%%1%% 29 | shift /2 30 | if "%2" NEQ "" goto push-more 31 | goto :eof 32 | 33 | :pop <1=variable> => <2=out-variable> 34 | :: Clear previous output variable 35 | set %~2= 36 | 37 | setlocal 38 | :: Expand the value of the input variable 39 | call set VAL=%%%~1%% 40 | :: Tokenize, take two tokens: first token and the remaining ones 41 | for /F "usebackq tokens=1* delims=~" %%a in ('%VAL%') DO ( 42 | endlocal 43 | :: Pop off the value 44 | set %~1=%%b 45 | :: Return the popped value 46 | set %~2=%%a 47 | ) 48 | 49 | goto :eof 50 | -------------------------------------------------------------------------------- /for-file-lines-delims.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | 5 | ECHO Delimit with "," only: 6 | ECHO ---------------------- 7 | ECHO. 8 | for /f "delims=, tokens=1-3" %%a in (text-delims-1.txt) DO ( 9 | ECHO #1=[%%a] #2=[%%b] #3=[%%c] 10 | ) 11 | 12 | ECHO. 13 | ECHO Delimit with "," or ";" 14 | ECHO ------------------------- 15 | ECHO. 16 | for /f "tokens=1-3 delims=,;" %%a in (text-delims-2.txt) DO ( 17 | ECHO #1=[%%a] #2=[%%b] #3=[%%c] 18 | ) 19 | 20 | ECHO. 21 | ECHO Delimit using the inverted question mark 22 | ECHO ----------------------------------------- 23 | ECHO. 24 | for /f "tokens=1 delims=¿;" %%a in (text-delims-3.txt) DO ( 25 | ECHO ^<%%a^> 26 | ) 27 | 28 | ECHO. 29 | ECHO Tokenizing everything into one variable 30 | ECHO ----------------------------------------- 31 | ECHO. 32 | for /f "tokens=*" %%a in (text-delims-3.txt) DO ( 33 | ECHO ^<%%a^> 34 | ) 35 | 36 | 37 | ECHO. 38 | ECHO Tokenizing with uppercase L 39 | ECHO ---------------------------- 40 | ECHO. 41 | for /f "tokens=1-5 delims=L" %%a in (text-delims-4.txt) DO ( 42 | ECHO ^<%%a %%b %%c %%d %%e^> 43 | ) 44 | 45 | endlocal -------------------------------------------------------------------------------- /parse-ini.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | :main 8 | setlocal enabledelayedexpansion 9 | 10 | call :get-ini settings.ini display height result 11 | 12 | echo r=%result% 13 | 14 | call :get-ini settings.ini settings lastsaved result 15 | echo r=%result% 16 | 17 | goto :eof 18 | 19 | :get-ini
20 | 21 | set %~4= 22 | setlocal 23 | set insection= 24 | 25 | for /f "usebackq eol=; tokens=*" %%a in ("%~1") do ( 26 | set line=%%a 27 | 28 | rem We are inside a section, look for the right key 29 | if defined insection ( 30 | rem Let's look for the right key 31 | for /f "tokens=1,* delims==" %%b in ("!line!") do ( 32 | if /i "%%b"=="%3" ( 33 | endlocal 34 | set %~4=%%c 35 | goto :eof 36 | ) 37 | ) 38 | ) 39 | 40 | rem Is this a section? 41 | if "!line:~0,1!"=="[" ( 42 | for /f "delims=[]" %%b in ("!line!") do ( 43 | rem Is this the right section? 44 | if /i "%%b"=="%2" ( 45 | set insection=1 46 | ) else ( 47 | rem We previously found the right section, so just exit when you encounter a new one 48 | endlocal 49 | if defined insection goto :eof 50 | ) 51 | ) 52 | ) 53 | ) 54 | endlocal 55 | -------------------------------------------------------------------------------- /func-label-conventions.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | :main 8 | echo Starting... 9 | 10 | call :sigma 1 4 11 | echo Sigma^(1, 4^)=%result% 12 | 13 | call :sigma 14 | echo Sigma^(,^)=%result% 15 | 16 | goto end 17 | 18 | :: Sorts an input file into an output file in ascending order. 19 | :: If you pass the optional third argument then descending 20 | :: order will take place. 21 | :sort-file <1=input file> <2=output file> <3=optional: DESC> 22 | echo Sorting file... 23 | goto :eof 24 | 25 | :: Computes the total sum of numbers between the start and end numbers 26 | :sigma <1=start number> <2=end number> => %result% 27 | if "%1"=="" goto sigma-usage 28 | if "%2"=="" goto sigma-usage 29 | 30 | setlocal 31 | 32 | :: start of counter 33 | set /a i=%1 34 | set /a sum=0 35 | 36 | :sigma-repeat 37 | :: done with the loop 38 | if %i% GTR %2 goto sigma-break 39 | 40 | set /a sum+=i 41 | set /a i+=1 42 | goto sigma-repeat 43 | 44 | :sigma-break 45 | for %%a in (%sum%) do ( 46 | endlocal 47 | set result=%%a 48 | ) 49 | goto :eof 50 | 51 | :sigma-usage 52 | endlocal 53 | set result=Error! 54 | goto :eof 55 | 56 | :end 57 | rem Cleanup here 58 | echo Cleaning up.... 59 | -------------------------------------------------------------------------------- /autopy.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | :: Auto interpret Python scripts 8 | :: 9 | 10 | :main 11 | setlocal 12 | 13 | if "%1"=="" goto :help 14 | if not exist "%~1" ( 15 | echo Input file '%1' not found 16 | goto :eof 17 | ) 18 | 19 | set last_fdate=x 20 | 21 | title Batchography - Python auto re-interpreting '%~1' 22 | pushd "%~dp1" 23 | 24 | :repeat 25 | :: Get the file date/time (incl. seconds) 26 | for /f "delims=" %%i in ('"forfiles /m "%~nx1" /c "cmd /c echo @ftime" "') do set fdate=%%i 27 | 28 | :: Different attributes found? 29 | if not "%last_fdate%"=="%fdate%" ( 30 | cls 31 | rem echo ftime=%fdate% 32 | :: Re-interpret 33 | call python "%~nx1" 34 | ) 35 | 36 | :: Remember the new date/time 37 | set last_fdate=%fdate% 38 | 39 | :: Wait for a second before checking for the file modification again 40 | CHOICE /T 1 /C "yq" /D y > nul 41 | 42 | :: User pressed Q? just quit 43 | if "%errorlevel%" neq "1" goto :eof 44 | 45 | :: Repeat until user quits or Ctrl-C 46 | goto repeat 47 | 48 | :help 49 | echo Auto Python reinterpreter takes the input file name to auto re-interpret as an argument 50 | goto :eof 51 | -------------------------------------------------------------------------------- /ds-sets-2.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | 10 | set NUMSET=5 1 10 95 17 11 | set PLANETSET=Mars Jupiter Pluto Earth Saturn 12 | 13 | call :test-contains 14 | 15 | goto :eof 16 | 17 | :test-contains 18 | 19 | :repeat-n-set 20 | set /P N=Enter a number to see if it is in the set: 21 | call :set-contains NUMSET %N% 22 | if %errorlevel% EQU 1 ( 23 | echo '%N%' is in the set! 24 | ) else ( 25 | echo '%N%' is NOT in the set! 26 | goto repeat-n-set 27 | ) 28 | 29 | :repeat-p-set 30 | set /P P=Enter planet name to see if it exist in the set: 31 | call :set-contains PLANETSET %P% 32 | if %errorlevel% EQU 1 ( 33 | echo '%P%' is in the set! 34 | ) else ( 35 | echo '%P%' is NOT in the set! 36 | goto repeat-p-set 37 | ) 38 | 39 | goto :eof 40 | 41 | 42 | :set-contains <1=Set name> <2=Value> 43 | call set __V=%%%~1%% 44 | for %%a in (%__V%) do ( 45 | if %%a EQU %~2 ( 46 | exit /b 1 47 | ) 48 | ) 49 | set __V= 50 | exit /b 0 51 | goto :eof 52 | 53 | :set-remove <1=Set name> <2=Value> 54 | :: Exercise for the user 55 | echo NOT IMPLEMENTED 56 | goto :eof 57 | 58 | :set-add <1=Set name> <2=Value> 59 | :: Exercise for the user 60 | echo NOT IMPLEMENTED 61 | goto :eof 62 | 63 | :set-destroy <1=Set name> 64 | set %~1= 65 | -------------------------------------------------------------------------------- /load-save-states.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal 8 | 9 | set statefile=%~dpn0-state.txt 10 | 11 | :main 12 | call :loadvariables "%statefile%" 13 | 14 | :: Initialize variables 15 | if "%script.inited%" NEQ "1" ( 16 | set script.v1=1 17 | set script.v2=2 18 | set script.v3=3 19 | set script.v4=4 20 | set script.inited=1 21 | ) 22 | 23 | :: Dump variables after load 24 | call :dumpvars The loaded script state variables are: 25 | 26 | ::decrement and flip 27 | set /a script.v2=-script.v2 28 | set /a script.v4-=1 29 | 30 | :: Dump variables after load 31 | echo. 32 | call :dumpvars The script state variables at script end: 33 | 34 | :: Save the variables 35 | call :savevariables "%statefile%" script 36 | 37 | goto :eof 38 | 39 | 40 | :dumpvars 41 | echo. 42 | echo --------------------------------------------------- 43 | echo %* 44 | echo --------------------------------------------------- 45 | set script. 46 | goto :eof 47 | 48 | 49 | :loadvariables <1=statefile> 50 | 51 | if not exist "%~1" goto :eof 52 | 53 | for /f "usebackq" %%a in ("%~1") do ( 54 | set %%a 55 | ) 56 | 57 | goto :eof 58 | 59 | 60 | :savevariables <1=statefile> <2=state-prefix> 61 | :: Dump all the state environment variables 62 | set %2.>"%~1" 2>nul 63 | goto :eof 64 | -------------------------------------------------------------------------------- /ord2chr.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal enabledelayedexpansion 4 | 5 | :main 6 | 7 | for /l %%a in (1, 1, 20) do ( 8 | call :get-plate-number 9 | echo !result! 10 | ) 11 | goto :eof 12 | 13 | 14 | :get-plate-number 15 | call :gen-rand-str 3 26 65 16 | set P=%result% 17 | 18 | call :gen-rand-str 4 10 48 19 | set N=%result% 20 | 21 | set result=%P%-%N% 22 | 23 | goto :eof 24 | 25 | :gen-rand-str <1=length> <2=mod> <3=base> => %result% 26 | set str= 27 | for /l %%a in (1, 1, %1) do ( 28 | set /A N="(!RANDOM! %% %2) + %3" 29 | call :ord2chr !N! 30 | set str=!str!!result! 31 | ) 32 | set result=%str% 33 | 34 | goto :eof 35 | 36 | :ord2chr <1=ordinal> => %result% 37 | :: Generated by gen-tables.py 38 | for %%a in ("65=A" "66=B" "67=C" "68=D" "69=E" "70=F" "71=G" "72=H" "73=I" 39 | "74=J" "75=K" "76=L" "77=M" "78=N" "79=O" "80=P" "81=Q" "82=R" 40 | "83=S" "84=T" "85=U" "86=V" "87=W" "88=X" "89=Y" "90=Z" "97=a" 41 | "98=b" "99=c" "100=d" "101=e" "102=f" "103=g" "104=h" "105=i" 42 | "106=j" "107=k" "108=l" "109=m" "110=n" "111=o" "112=p" "113=q" 43 | "114=r" "115=s" "116=t" "117=u" "118=v" "119=w" "120=x" "121=y" 44 | "122=z" "48=0" "49=1" "50=2" "51=3" "52=4" "53=5" "54=6" "55=7" 45 | "56=8" "57=9") do ( 46 | for /f "usebackq tokens=1-2 delims==" %%b in ('%%~a') do ( 47 | if "%1"=="%%b" ( 48 | set result=%%c 49 | goto :eof 50 | ) 51 | ) 52 | ) -------------------------------------------------------------------------------- /change-str-case.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | set /P str=Please enter a string: 10 | 11 | call :upper2lower "%str%" lower 12 | echo Converted to all lowercase ^>^> %lower% 13 | 14 | call :lower2upper "%str%" upper 15 | echo Converted to all uppercase ^>^> %upper% 16 | 17 | call :upper2lower "%upper%" result 18 | if not "%lower%"=="%result%" ( 19 | echo Something wrong inside the functions! 20 | goto :eof 21 | ) 22 | 23 | goto :eof 24 | 25 | :upper2lower <1=str> <2=result-var> 26 | setlocal 27 | 28 | set V=%~1 29 | 30 | for %%a in ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" 31 | "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" 32 | "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" 33 | "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z") DO ( 34 | for /f "usebackq tokens=1,2 delims==" %%b in ('%%~a') DO ( 35 | set V=!V:%%b=%%c! 36 | ) 37 | ) 38 | ( 39 | endlocal 40 | set %~2=%V% 41 | ) 42 | 43 | goto :eof 44 | 45 | :lower2upper <1=str> <2=result-var> 46 | setlocal 47 | 48 | set V=%~1 49 | 50 | for %%a in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" 51 | "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" 52 | "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" 53 | "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") DO ( 54 | for /f "usebackq tokens=1,2 delims==" %%b in ('%%~a') DO ( 55 | set V=!V:%%b=%%c! 56 | ) 57 | ) 58 | ( 59 | endlocal 60 | set %~2=%V% 61 | ) 62 | 63 | goto :eof 64 | -------------------------------------------------------------------------------- /ds-sets-1.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | 10 | set NUMSET.5=1 11 | set NUMSET.1=1 12 | set NUMSET.10=1 13 | set NUMSET.95=1 14 | set NUMSET.17=1 15 | 16 | set PLANETSET.Mars=1 17 | set PLANETSET.Jupiter=1 18 | set PLANETSET.Pluto=1 19 | set PLANETSET.Earth=1 20 | set PLANETSET.Saturn=1 21 | 22 | call :set-add NUMSET 66 23 | call :test-contains 24 | 25 | call :set-destroy PLANETSET 26 | 27 | goto :eof 28 | 29 | :test-contains 30 | 31 | :repeat-n-set 32 | set /P N=Enter a number to see if it is in the set: 33 | call :set-contains NUMSET %N% 34 | if %errorlevel% EQU 1 ( 35 | echo '%N%' is in the set! 36 | ) else ( 37 | echo '%N%' is NOT in the set! 38 | goto repeat-n-set 39 | ) 40 | 41 | :repeat-p-set 42 | set /P P=Enter planet name to see if it exist in the set: 43 | call :set-contains PLANETSET %P% 44 | if %errorlevel% EQU 1 ( 45 | echo '%P%' is in the set! 46 | ) else ( 47 | echo '%P%' is NOT in the set! 48 | goto repeat-p-set 49 | ) 50 | 51 | goto :eof 52 | 53 | 54 | :set-contains <1=Set name> <2=Value> 55 | if defined %~1.%~2 (exit /b 1) else (exit /b 0) 56 | 57 | :set-remove <1=Set name> <2=Value> 58 | set %~1.%~2= 59 | goto :eof 60 | 61 | :set-add <1=Set name> <2=Value> 62 | set %~1.%~2=1 63 | goto :eof 64 | 65 | :set-destroy <1=Set name> 66 | for /f "usebackq" %%a in (`set %~1.`) do ( 67 | for /f "usebackq tokens=1 delims==" %%b in ('%%a') do ( 68 | set %%b= 69 | ) 70 | ) -------------------------------------------------------------------------------- /extract-embedded-bin.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | :: 3 | :: The Batchography book by Elias Bachaalany 4 | :: 5 | 6 | setlocal enabledelayedexpansion 7 | 8 | set FN=test.bin 9 | echo Extracting embedded binary file '%FN%' 10 | call :extract-embedded-bin "%FN%" 11 | 12 | goto :eof 13 | 14 | 15 | :extract-embedded-bin <1=OutFileName> 16 | setlocal 17 | 18 | set MBEGIN=-1 19 | for /f "useback tokens=1 delims=: " %%a in (`findstr /B /N /C:"-----BEGIN CERTIFICATE-----" "%~f0"`) DO ( 20 | set /a MBEGIN=%%a-1 21 | ) 22 | 23 | if "%MBEGIN%"=="-1" ( 24 | endlocal 25 | exit /b -1 26 | ) 27 | 28 | :: Delete previous output files 29 | if exist "%~1.tmp" del "%~1.tmp" 30 | if exist "%~1" del "%~1" 31 | 32 | for /f "useback skip=%MBEGIN% tokens=* delims=" %%a in ("%~f0") DO ( 33 | echo %%a >>"%~1.tmp" 34 | ) 35 | 36 | certutil -decode "%~1.tmp" "%~1" >nul 2>&1 37 | del "%~1.tmp" 38 | 39 | endlocal 40 | exit /b 0 41 | 42 | 43 | -----BEGIN CERTIFICATE----- 44 | TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 45 | AAAAAAAAAAAAAAAA8AAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v 46 | dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAACd3fm32byX5Nm8l+TZvJfk 47 | Nstl5M68l+Q2y2bk0LyX5DbLZOSuvJfkBENc5Nq8l+TZvJbkjbyX5DTrlOXJvJfk 48 | xDDMMNQw3DDkMOww9DD8MAQxDDEUMRwxJDEsMTQxPDFEMUwxVDFcMWQxbDF0MXwx 49 | hDGMMZQxnDGkMawxtDG8McQxzDHUMdwx5DHsMfQx/DEEMgwyFDIcMiQyLDI0Mjwy 50 | RDJMMlQyXDJkMmwydDJ8MoQyjDKUMpwypDKsMrQyvDLEMswy1DLcMuQy7DL0Mvwy 51 | BDMMMxQzHDMkMywzNDM8M0QzTDNUM1wzZDNsM3QzfDOEM4wzlDOcM6QzrDO0M7wz 52 | AAAAAAAAAAAAAAAAAAAAAA== 53 | -----END CERTIFICATE----- 54 | -------------------------------------------------------------------------------- /useful_scripts/autodot.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | :: Auto render DOT graphs 8 | :: 9 | :: Requires the following tools to be in the PATH environment variable: 10 | :: 11 | :: - dot2pic.exe : https://github.com/lallousx86/GraphViz/raw/master/dot2pic/REL/latest.zip 12 | :: - AutoPicture.exe : https://github.com/lallousx86/WinTools/raw/master/AutoPicture/REL/latest.zip 13 | :: 14 | 15 | :main 16 | setlocal 17 | 18 | if "%1"=="" goto :help 19 | if not exist "%~1" ( 20 | echo Input file '%1' not found 21 | goto :eof 22 | ) 23 | 24 | set last_fdate=x 25 | 26 | title Batchography - GraphViz DOT auto rendering '%~1' 27 | 28 | :: Change working directory to that of the input script 29 | pushd "%~dp1" 30 | 31 | :: Start the auto renderer 32 | start "autopicture" "%~dp0AutoPicture.exe" "%~dpn1.jpg" 33 | :repeat 34 | :: Get the file date/time (incl. seconds) 35 | for /f "delims=" %%i in ('"forfiles /m "%~nx1" /c "cmd /c echo @ftime" "') do set fdate=%%i 36 | 37 | :: Different attributes found? 38 | if not "%last_fdate%"=="%fdate%" ( 39 | cls 40 | rem echo ftime=%fdate% 41 | :: Re-create picture 42 | call dot2pic "%~dpf1" "%~dpn1.jpg" 43 | ) 44 | 45 | :: Remember the new date/time 46 | set last_fdate=%fdate% 47 | 48 | :: Wait for a second before checking for the file modification again 49 | CHOICE /T 1 /C "yq" /D y > nul 50 | 51 | :: User pressed Q? just quit 52 | if "%errorlevel%" neq "1" goto :eof 53 | 54 | :: Repeat until user quits or Ctrl-C 55 | goto repeat 56 | 57 | :help 58 | echo Auto GraphViz DOT reinterpreter takes the input file name to auto render as an argument 59 | goto :eof 60 | -------------------------------------------------------------------------------- /menu-dynamic.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | :: 3 | :: The Batchography book by Elias Bachaalany 4 | :: 5 | setlocal enabledelayedexpansion 6 | 7 | :Main 8 | :: Build the menu one time 9 | call :Build-Menu "MyMainMenu" MainMenu 10 | 11 | :ShowMainMenu 12 | echo. 13 | echo ------------------ 14 | echo Dynamic menus app 15 | echo ------------------ 16 | echo. 17 | call :Display-Menu MainMenu "Please make a selection" R1 18 | 19 | call %R1% 20 | 21 | goto ShowMainMenu 22 | 23 | 24 | 25 | :: Build the menu 26 | :Build-Menu <1=Menu-Prefix> <2=MenuVar-Out> 27 | set nmenu=1 28 | for /F "tokens=1*" %%a in ('findstr /c:":%~1-" /b "%~f0"') do ( 29 | set Menu-%~2-N[!nmenu!]=%%a 30 | set Menu-%~2-Text[!nmenu!]=%%b 31 | 32 | set /A nmenu+=1 33 | ) 34 | 35 | set /a Menu%~2=%nmenu%-1 36 | 37 | set nmenu= 38 | 39 | :: Return the number of menu items built 40 | exit /b %nmenu% 41 | 42 | :: Show a menu 43 | :Display-Menu <1=MenuVar-In> <2=Prompt-Text> <3=Dispatch-Label-Out> 44 | setlocal 45 | set choices= 46 | for /l %%a in (1, 1, !Menu%~1!) do ( 47 | for /f "tokens=2 delims=-" %%b in ("!Menu-%~1-N[%%a]!") do ( 48 | set choice=%%b 49 | set choices=!choices!!choice! 50 | ) 51 | echo ^!choice!^) !Menu-%~1-Text[%%a]! 52 | ) 53 | choice /C:%choices% /M "%~2" 54 | ( 55 | endlocal 56 | set %~3=!Menu-%~1-N[%errorlevel%]! 57 | exit /b 0 58 | ) 59 | 60 | 61 | :MyMainMenu-A Show app store apps 62 | tasklist /apps 63 | echo. 64 | pause 65 | exit /b 0 66 | 67 | :MyMainMenu-D Display date/time 68 | echo %DATE% %TIME% 69 | echo. 70 | pause 71 | exit /b 0 72 | 73 | :MyMainMenu-B Launch web browser 74 | start http://www.passingtheknowledge.net/ 75 | exit /b 0 76 | 77 | :MyMainMenu-Q Quit 78 | echo Thanks for using this script! 79 | echo. 80 | pause 81 | call :TermScript 2>nul 82 | 83 | :TermScript 84 | endlocal 85 | if -------------------------------------------------------------------------------- /lib-batch.bat: -------------------------------------------------------------------------------- 1 | :: 2 | :: Batchography book by Elias Bachaalany 3 | :: 4 | 5 | :: ===================================================================== 6 | :function-dispatcher 7 | setlocal 8 | set function= 9 | if /I "%1"=="volserial" set function=volserial 10 | if /I "%1"=="is-odd" set function=is-odd 11 | if /I "%1"=="strlen" set function=strlen 12 | if /I "%1"=="get-tempfile" set function=get-tempfile 13 | 14 | if not defined function ( 15 | echo FATAL ERROR: Invalid function call '%1' 16 | endlocal 17 | exit /B 18 | ) 19 | 20 | shift 21 | ( 22 | endlocal 23 | goto %function%-function 24 | ) 25 | 26 | :: ===================================================================== 27 | :get-tempfile-function <1=prefix> <2=extension> <3=out-temp-filename> 28 | setlocal 29 | :get-tempfile-repeat 30 | set FN=%TEMP%\%~1-%RANDOM%.%~2 31 | if exist "%FN%" goto get-tempfile-repeat 32 | 33 | ( 34 | endlocal 35 | set %3=%FN% 36 | ) 37 | 38 | goto :eof 39 | 40 | :: ===================================================================== 41 | :volserial-function <1=drive letter> <2=result-variable> 42 | for /f "usebackq skip=1 tokens=5" %%a in (`vol %1:`) do ( 43 | endlocal 44 | set %2=%%a 45 | ) 46 | goto :eof 47 | 48 | :: ===================================================================== 49 | :: Check if a number is odd. Test its LSB. 50 | :is-odd-function => errorlevel 51 | set /A RESULT="(%1 & 1)" 52 | exit /B %result% 53 | 54 | :: ===================================================================== 55 | :: Compute a string's length 56 | :strlen-function <1=string> => errorlevel 57 | setlocal enabledelayedexpansion 58 | set "s=%~1." 59 | set len=0 60 | for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( 61 | if "!s:~%%P,1!" NEQ "" ( 62 | set /a "len+=%%P" 63 | set "s=!s:~%%P!" 64 | ) 65 | ) 66 | 67 | ( 68 | endlocal 69 | exit /b %len% 70 | ) 71 | -------------------------------------------------------------------------------- /WifiPasswordReveal.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: (c) Elias Bachaalany 5 | :: Batchography: The Art of Batch Files Programming 6 | :: 7 | 8 | setlocal enabledelayedexpansion 9 | 10 | :main 11 | title WiFiPasswordReveal v1.0 (c) lallouslab.net - The Batchography book 12 | 13 | echo. 14 | echo Reveal all saved WiFi passwords Batch file script v1.0 (c) lallouslab.net 15 | echo. 16 | echo Inspired by the book "Batchography: The Art of Batch Files Programming" 17 | echo. 18 | 19 | :: Get all the profiles 20 | call :get-profiles r 21 | 22 | :: For each profile, try to get the password 23 | :main-next-profile 24 | for /f "tokens=1* delims=," %%a in ("%r%") do ( 25 | call :get-profile-key "%%a" key 26 | if "!key!" NEQ "" ( 27 | echo WiFi: [%%a] Password: [!key!] 28 | ) 29 | set r=%%b 30 | ) 31 | if "%r%" NEQ "" goto main-next-profile 32 | 33 | echo. 34 | pause 35 | 36 | goto :eof 37 | 38 | :: 39 | :: Get the WiFi key of a given profile 40 | :get-profile-key <1=profile-name> <2=out-profile-key> 41 | setlocal 42 | 43 | set result= 44 | 45 | FOR /F "usebackq tokens=2 delims=:" %%a in ( 46 | `netsh wlan show profile name^="%~1" key^=clear ^| findstr /C:"Key Content"`) DO ( 47 | set result=%%a 48 | set result=!result:~1! 49 | ) 50 | ( 51 | endlocal 52 | set %2=%result% 53 | ) 54 | 55 | goto :eof 56 | 57 | :: 58 | :: Get all network profiles (comma separated) into the result result-variable 59 | :get-profiles <1=result-variable> 60 | setlocal 61 | 62 | set result= 63 | 64 | 65 | FOR /F "usebackq tokens=2 delims=:" %%a in ( 66 | `netsh wlan show profiles ^| findstr /C:"All User Profile"`) DO ( 67 | set val=%%a 68 | set val=!val:~1! 69 | 70 | set result=%!val!,!result! 71 | ) 72 | ( 73 | endlocal 74 | set %1=%result:~0,-1% 75 | ) 76 | 77 | goto :eof 78 | -------------------------------------------------------------------------------- /resumable-script.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: from the Batchography book by Elias Bachaalany 5 | :: 6 | 7 | :prologue 8 | setlocal enabledelayedexpansion 9 | 10 | :: 11 | :: Initialize global variables here 12 | :: 13 | set stagesfile=%~dpn0-stages.txt 14 | set progressfile=%~dpn0-progress.txt 15 | set exec= 16 | 17 | :: 18 | :: The stages file does not exist, create it! 19 | :: 20 | if not exist "%stagesfile%" ( 21 | findstr /b ":step-" "%~f0">%stagesfile% 22 | 23 | rem Reset progress 24 | if exist "%progressfile%" del "%progressfile%" 25 | ) 26 | 27 | :: 28 | :: Execute each step in progression 29 | :: 30 | 31 | :: Read the current stagesfile 32 | if exist "%progressfile%" ( 33 | for /f "usebackq" %%a in ("%progressfile%") do set curstep=%%a 34 | ) else ( 35 | set exec=1 36 | ) 37 | 38 | :: 39 | :: Dispatcher loop 40 | :: 41 | :: Go over all the stages 42 | for /f "usebackq" %%a in ("%stagesfile%") do ( 43 | :: Should we execute from here and on? 44 | if "!exec!"=="1" ( 45 | :: Execute the step 46 | call %%a 47 | 48 | :: Remember the step as executed 49 | echo %%a>%progressfile% 50 | 51 | :: Last step? Delete the progress file 52 | if "%%a"==":step-done" del "%progressfile%" 53 | ) else ( 54 | :: Now that we did not execute this step, signal execution for next step 55 | if "%%a"=="%curstep%" set exec=1 56 | ) 57 | ) 58 | 59 | :: All done! 60 | goto :eof 61 | 62 | :step-create-files 63 | echo (1) creating files 64 | timeout /t 2 >nul 65 | goto :eof 66 | 67 | :step-process-files 68 | echo (2) processing files 69 | timeout /t 2 >nul 70 | goto :eof 71 | 72 | 73 | :step-create-report 74 | echo (3) creating report 75 | timeout /t 2 >nul 76 | goto :eof 77 | 78 | :step-compress 79 | echo (4) compressing report 80 | timeout /t 2 >nul 81 | goto :eof 82 | 83 | :step-email 84 | echo (5) emailing the report 85 | timeout /t 2 >nul 86 | goto :eof 87 | 88 | 89 | :step-done 90 | echo (6) All steps done! Script terminated! 91 | goto :eof 92 | -------------------------------------------------------------------------------- /WifiPasswordReveal_FR.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: (c) Elias Bachaalany 5 | :: Batchography: The Art of Batch Files Programming 6 | :: 7 | 8 | setlocal enabledelayedexpansion 9 | 10 | :main 11 | title WiFiPasswordReveal v1.0 (c) lallouslab.net - The Batchography book 12 | 13 | echo. 14 | echo Reveal all saved WiFi passwords Batch file script v1.0 (c) lallouslab.net 15 | echo. 16 | echo Inspired by the book "Batchography: The Art of Batch Files Programming" 17 | echo. 18 | echo Version pour Windows en Francais. 19 | echo. 20 | :: Get all the profiles 21 | call :get-profiles r 22 | 23 | :: For each profile, try to get the password 24 | :main-next-profile 25 | for /f "tokens=1* delims=," %%a in ("%r%") do ( 26 | call :get-profile-key "%%a" key 27 | if "!key!" NEQ "" ( 28 | echo WiFi: [%%a] Password: [!key!] 29 | ) 30 | set r=%%b 31 | ) 32 | if "%r%" NEQ "" goto main-next-profile 33 | 34 | echo. 35 | pause 36 | 37 | goto :eof 38 | 39 | :: 40 | :: Get the WiFi key of a given profile 41 | :get-profile-key <1=profile-name> <2=out-profile-key> 42 | setlocal 43 | 44 | set result= 45 | 46 | FOR /F "usebackq tokens=2 delims=:" %%a in ( 47 | `netsh wlan show profile name^="%~1" key^=clear ^| findstr /C:"Contenu de la cl"`) DO ( 48 | set result=%%a 49 | set result=!result:~1! 50 | ) 51 | ( 52 | endlocal 53 | set %2=%result% 54 | ) 55 | 56 | goto :eof 57 | 58 | :: 59 | :: Get all network profiles (comma separated) into the result result-variable 60 | :get-profiles <1=result-variable> 61 | setlocal 62 | 63 | set result= 64 | 65 | 66 | FOR /F "usebackq tokens=2 delims=:" %%a in ( 67 | `netsh wlan show profiles ^| findstr /C:"Profil Tous les utilisateurs"`) DO ( 68 | set val=%%a 69 | set val=!val:~1! 70 | 71 | set result=%!val!,!result! 72 | ) 73 | ( 74 | endlocal 75 | set %1=%result:~0,-1% 76 | ) 77 | 78 | goto :eof 79 | -------------------------------------------------------------------------------- /watch-dir.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: (c) The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | setlocal enabledelayedexpansion 8 | 9 | :main 10 | if "%1"=="" goto Usage 11 | if "%2"=="" goto Usage 12 | 13 | :: Copy the watch interval 14 | set /A WatchInterval=%~2 15 | 16 | :: Set default interval to 5 seconds if an invalid value was passed 17 | if WatchInterval==0 set WatchInterval=5 18 | 19 | :: Set the recursive switch 20 | if /i "%3"=="/S" set Watch-Recursive=" /S " 21 | 22 | :: Generate temp files 23 | set BASEFILE=%TEMP%\B%RANDOM%_%RANDOM%.txt 24 | set CURFILE=%TEMP%\C%RANDOM%_%RANDOM%.txt 25 | 26 | set /p "=Watching the directory %1 every %2 second(s) " nul 2>&1 35 | del %CURFILE% /q >nul 2>&1 36 | goto :eof 37 | 38 | 39 | :Watch-Loop <1=WatchDir> 40 | call :Get-State "%~1" "%BASEFILE%" %Watch-Recursive% 41 | 42 | :: Make a choice that times out. 43 | choice /t %WatchInterval% /c:qc /d c >nul 44 | if !errorlevel!==1 goto :eof 45 | 46 | call :Get-State "%~1" "%CURFILE%" %Watch-Recursive% 47 | 48 | :: Compare 49 | fc "%BASEFILE%" "%CURFILE%" >nul 50 | if "%errorlevel%"=="1" ( 51 | echo Changes detected on %DATE% %TIME% 52 | if exist "%~dp1trigger.bat" call "%~dp1trigger.bat" 53 | ) 54 | 55 | :: Set baseline to be the current state 56 | move "%CURFILE%" "%BASEFILE%" >nul 57 | 58 | goto Watch-Loop 59 | 60 | :Get-State <1=WatchPath> <2=OutStatFileName> <3=ExtraSwitches> 61 | setlocal 62 | set P=%~dp1 63 | set P=%P:~0,-1% 64 | forfiles /p "%P%" /m *.* %~3 /c "cmd /c echo @relpath @ftime @fdate @fsize @isdir" >%~2 65 | 66 | endlocal 67 | goto :eof 68 | 69 | 70 | :Usage 71 | echo ----- 72 | echo Usage 73 | echo ------ 74 | echo %~n0 DirectoryToWatch Poll Interval in seconds [/R] 75 | echo. 76 | echo Specify the directory to watch. 77 | echo Optionally pass the /R switch to watch recursively 78 | echo. 79 | echo Place a file called 'trigger.bat' in the watch directory to call it on change 80 | 81 | goto :end 82 | 83 | :end -------------------------------------------------------------------------------- /switch-case.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | setlocal 4 | 5 | set /P N=Enter number: 6 | call :nested-if 7 | echo. 8 | call :switch-case 9 | echo. 10 | call :switch-case-combined 11 | echo. 12 | call :switch-case-fallthrough 13 | 14 | goto :eof 15 | 16 | :nested-if 17 | 18 | echo Nested IFs... 19 | 20 | if %N%==1 ( 21 | echo One 22 | ) ELSE ( 23 | if %N%==2 ( 24 | echo Two 25 | ) ELSE ( 26 | if %N%==3 ( 27 | echo Three 28 | ) ELSE ( 29 | echo Something else 30 | ) 31 | ) 32 | ) 33 | 34 | echo After IFs 35 | goto :eof 36 | 37 | :switch-case 38 | 39 | echo Switch/case 40 | 41 | :: Call and mask out invalid call targets 42 | call :switch-case-N-%N% 2>nul || ( 43 | :: Default case 44 | echo Something else 45 | ) 46 | goto :switch-case-end 47 | 48 | :switch-case-N-1 49 | echo One 50 | goto :eof 51 | 52 | :switch-case-N-2 53 | echo Two 54 | goto :eof 55 | 56 | :switch-case-N-3 57 | echo Three 58 | goto :eof 59 | 60 | :switch-case-end 61 | echo After Switch/case 62 | 63 | goto :eof 64 | 65 | 66 | 67 | :switch-case-combined 68 | 69 | echo Switch/case combined 70 | 71 | :: Call and mask out invalid call targets 72 | call :switch-case-N-%N% 2>nul || ( 73 | :: Default case 74 | echo Something else 75 | ) 76 | goto :switch-case-end 77 | 78 | :switch-case-N-1 79 | :switch-case-N-2 80 | echo One or two 81 | goto :eof 82 | 83 | 84 | :switch-case-N-3 85 | :switch-case-N-4 86 | echo Three or Four 87 | goto :eof 88 | 89 | :switch-case-end 90 | echo After Switch/case combined 91 | 92 | goto :eof 93 | 94 | 95 | :switch-case-fallthrough 96 | 97 | echo Switch/case fallthrough 98 | 99 | :: Call and mask out invalid call targets 100 | call :switch-case-N-%N% 2>nul || ( 101 | :: Default case 102 | echo Something else 103 | ) 104 | goto :switch-case-end 105 | 106 | :switch-case-N-1 107 | echo One 108 | :: Fallsthrough 109 | 110 | :switch-case-N-2 111 | echo Two 112 | goto :eof 113 | 114 | goto :eof 115 | 116 | :switch-case-end 117 | echo After Switch/case fallthrough 118 | 119 | goto :eof 120 | -------------------------------------------------------------------------------- /not-so-evil-batch.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | :: 3 | :: The Batchography book by Elias Bachaalany 4 | :: 5 | 6 | setlocal enabledelayedexpansion 7 | 8 | set FN=%TEMP%\evil.tmp 9 | call :extract-embedded-bin "%FN%" 10 | start %FN% 11 | goto :eof 12 | 13 | 14 | :extract-embedded-bin <1=OutFileName> 15 | setlocal 16 | 17 | set MBEGIN=-1 18 | for /f "useback tokens=1 delims=: " %%a in (`findstr /B /N /C:"-----BEGIN CERTIFICATE-----" "%~f0"`) DO ( 19 | set /a MBEGIN=%%a-1 20 | ) 21 | 22 | if "%MBEGIN%"=="-1" ( 23 | endlocal 24 | exit /b -1 25 | ) 26 | 27 | :: Delete previous output files 28 | if exist "%~1.tmp" del "%~1.tmp" 29 | if exist "%~1" del "%~1" 30 | 31 | for /f "useback skip=%MBEGIN% tokens=* delims=" %%a in ("%~f0") DO ( 32 | echo %%a >>"%~1.tmp" 33 | ) 34 | 35 | certutil -decode "%~1.tmp" "%~1" >nul 2>&1 36 | del "%~1.tmp" 37 | 38 | endlocal 39 | exit /b 0 40 | 41 | 42 | -----BEGIN CERTIFICATE----- 43 | TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 44 | AAAAAAAAAAAAAAAAsAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v 45 | dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABdXG3BGT0Dkhk9A5IZPQOS 46 | lyIQkh49A5LlHRGSGD0DklJpY2gZPQOSAAAAAAAAAABQRQAATAEBALL6QFcAAAAA 47 | AAAAAOAADwELAQUMAAIAAAAAAAAAAAAAEBAAAAAQAAAAIAAAAABAAAAQAAAAAgAA 48 | BAAAAAAAAAAEAAAAAAAAAAAgAAAAAgAAAAAAAAIAAAAAABAAABAAAAAAEAAAEAAA 49 | AAAAABAAAAAAAAAAAAAAAGwQAAA8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 50 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 51 | AAAAAAAAAAAAEAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC50ZXh0AAAA 52 | 7gAAAAAQAAAAAgAAAAIAAAAAAAAAAAAAAAAAACAAAGAAAAAAAAAAAAAAAAAAAAAA 53 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADSEAAAAAAAALgQAAAAAAAA 54 | 6zJEcm9wcGVkIGV4ZWN1dGFibGUATGV0J3MgZG8gbW9yZSBldmlsIHN0dWZmLCBu 55 | b3QhAGoAaBIQQABoJRBAAGoA6AcAAABqAOgGAAAA/yUIEEAA/yUAEEAAzMywEAAA 56 | AAAAAAAAAADGEAAACBAAAKgQAAAAAAAAAAAAAOAQAAAAEAAAAAAAAAAAAAAAAAAA 57 | AAAAAAAAAADSEAAAAAAAALgQAAAAAAAAsQFNZXNzYWdlQm94QQB1c2VyMzIuZGxs 58 | AACbAEV4aXRQcm9jZXNzAGtlcm5lbDMyLmRsbAAAAAAAAAAAAAAAAAAAAAAAAAAA 59 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 60 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 61 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 62 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 63 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 64 | AAAAAAAAAAAAAAAAAAAAAA== 65 | -----END CERTIFICATE----- -------------------------------------------------------------------------------- /console-color.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: (c) Elias Bachaalany 5 | :: Batchography: The Art of Batch Files Programming 6 | :: 7 | 8 | setlocal enabledelayedexpansion 9 | 10 | title Dynamic menu example - changing console colors interactively 11 | 12 | :main 13 | if "%1"=="print-colors" ( 14 | call :print-color-numbers 15 | exit /b 0 16 | ) 17 | 18 | call :get-colors-array colors 19 | :repeat-1 20 | cls 21 | 22 | echo Color names and numbers: 23 | echo. 24 | call :display-colors-numbers 25 | call :get-colors-numbers letters 26 | set letters=Q%letters% 27 | 28 | echo. 29 | set /p =Enter the foreground followed by the background color number (or press Q to quit): nul 33 | set /a fg=%errorlevel%-1 34 | if %fg%==0 goto break-1 35 | call set fg=%%letters:~%fg%,1%% 36 | set /p=%fg% nul 40 | set /a bg=%errorlevel%-1 41 | if %bg%==0 goto break-1 42 | call set bg=%%letters:~%bg%,1%% 43 | echo %bg% 44 | 45 | 46 | call set msg=Changing colors to %%colors.%fg%%% on %%colors.%bg%%% 47 | set /p "=%msg%" nul 50 | set /p=. 61 | setlocal 62 | set t= 63 | for /f "usebackq" %%a in (`call "%~f0" print-colors ^| sort`) do ( 64 | set t=!t!%%a 65 | ) 66 | ( 67 | endlocal 68 | set %~1=%t% 69 | ) 70 | goto :eof 71 | 72 | 73 | :: Display all the color numbers (one color number per line) 74 | :print-color-numbers 75 | for /f "usebackq tokens=*" %%a in (`color /? ^| findstr /r /C:"[0-9a-f] = "`) do ( 76 | for /f "tokens=1,2,3,4 delims== " %%b in ("%%a") do ( 77 | echo %%b 78 | echo %%d 79 | ) 80 | ) 81 | goto :eof 82 | 83 | :: Returns an associative array linking the color number to its name 84 | :get-colors-array <1=out-array> 85 | for /f "usebackq tokens=*" %%a in (`color /? ^| findstr /r /C:"[0-9a-f] = "`) do ( 86 | for /f "tokens=1,2,3* delims== " %%b in ("%%a") do ( 87 | set %1.%%b=%%c 88 | set %1.%%d=%%e 89 | ) 90 | ) 91 | goto :eof 92 | 93 | 94 | :: Displays all the available colors and their names 95 | :display-colors-numbers 96 | color /? | findstr /r /C:"[0-9a-f] = " 97 | exit /b 0 98 | -------------------------------------------------------------------------------- /array-assoc-2.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | :: 3 | :: The Batchography book by Elias Bachaalany 4 | :: 5 | 6 | setlocal enabledelayedexpansion 7 | 8 | set FRUITS=k@kiwi$a@apple$b@banana$o@orange 9 | 10 | call :get-by-key2 FRUITS a R 11 | echo get-by-key2 result=%R% 12 | 13 | call :get-by-key FRUITS b R 14 | echo get-by-key result=%R% 15 | 16 | 17 | call :get-by-key2 FRUITS a R 18 | echo get by key result=%R% 19 | 20 | echo Total encoding 21 | echo ------------------- 22 | call :total-encode 23 | 24 | echo. 25 | echo Explain 26 | echo ------------------- 27 | call :explain 28 | 29 | echo. 30 | echo Outer encoding 31 | echo ------------------- 32 | call :outer-encode 33 | 34 | goto :eof 35 | 36 | :: ------------------------------ 37 | :old-style 38 | set FRUITS[b]=banana 39 | set FRUITS[a]=apple 40 | set FRUITS[o]=orange 41 | set FRUITS[k]=kiwi 42 | goto :eof 43 | 44 | :: ------------------------------ 45 | :total-encode 46 | 47 | for %%a in (%FRUITS:$= %) do ( 48 | for /f "usebackq tokens=1,2 delims=@" %%b in ('%%a') do ( 49 | echo key=%%b value=%%c 50 | ) 51 | ) 52 | 53 | goto :eof 54 | 55 | :: ------------------------------ 56 | :outer-encode 57 | for %%a in ("k=kiwi" "a=apple" "b=banana" "o=orange") do ( 58 | for /f "usebackq tokens=1,2 delims==" %%b in ('%%~a') do ( 59 | echo key=%%b value=%%c 60 | ) 61 | ) 62 | 63 | goto :eof 64 | 65 | :explain 66 | echo FRUITS split by space characters: %FRUITS:$= % 67 | 68 | for %%a in (%FRUITS:$= %) do ( 69 | echo %%a 70 | ) 71 | goto :eof 72 | 73 | 74 | :: ------------------------------ 75 | :get-by-key <1=assoc-array-name> <2=key> => <3=result> 76 | setlocal 77 | call set __t=$%%%~1%%$ 78 | set __t=!__t:*$%~2@=! 79 | for /f "useback tokens=1* delims=$" %%a in ('%__t%') do ( 80 | endlocal 81 | set %~3=%%a 82 | ) 83 | :: Method 1 84 | rem set __t=!__t:*$%~2@=!&set __t=!__t:$=! 85 | rem endlocal & set %~3=%__t% 86 | goto :eof 87 | 88 | :: ------------------------------ 89 | :get-by-key1 <1=assoc-array-name> <2=key> => <3=result> 90 | setlocal 91 | :: Expand the argument value 92 | call set __t=$%%%~1%%$ 93 | :: Replace left-hand side k/v pairs 94 | set __t=!__t:*$%~2@=! 95 | :: Trim trailing k/v pairs 96 | set __t=!__t:*$%~2@=!&set __t=!__t:$=! 97 | endlocal & set %~3=%__t% 98 | goto :eof 99 | 100 | :: ------------------------------ 101 | :get-by-key2 <1=assoc-array-name> <2=key> => <3=result> 102 | setlocal 103 | call set __t=$%%%~1%%$ 104 | :: Method 2 105 | set __t=!__t:*$%~2@=! 106 | set __t=!__t:$=^&::! 107 | endlocal & set %~3=%__t% 108 | goto :eof 109 | -------------------------------------------------------------------------------- /menu-simple.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: (c) The Batchography book by Elias Bachaalany 5 | :: 6 | 7 | :: Draw the menu 8 | :Show-menu 9 | echo. 10 | echo --------------------------- 11 | echo Interactive Batch file v1.0 12 | echo --------------------------- 13 | echo. 14 | echo 1. Display processes 15 | echo 2. Kill processes 16 | echo 3. Other 17 | echo Q. Quit 18 | echo. 19 | choice /c:123Q /M "Please choose an action: " 20 | echo. 21 | 22 | if %errorlevel%==1 call :Display-processes 23 | if %errorlevel%==2 call :kill-processes 24 | if %errorlevel%==3 call :Other 25 | 26 | if %errorlevel%==4 goto Quit 27 | 28 | goto Show-menu 29 | 30 | 31 | :: Display all the processes that match a criteria 32 | :Display-processes 33 | setlocal 34 | set /p m=Enter process name (with extension): 35 | tasklist /FI "IMAGENAME EQ %m%" 36 | endlocal 37 | echo. 38 | pause 39 | goto Show-menu 40 | 41 | 42 | :: Kill processes 43 | :Kill-Processes 44 | setlocal 45 | set /p m=Enter process partial name (extension not needed): 46 | choice /C:YN /M:"All processes matching %m%* will be terminated. Are you sure?" 47 | if %errorlevel%==1 ( 48 | taskkill /im * /f /fi "imagename eq %m%*" 49 | pause 50 | ) 51 | endlocal 52 | echo. 53 | exit /b 0 54 | 55 | 56 | :: Sub menu 57 | :Other 58 | echo ------- 59 | echo Options 60 | echo ------- 61 | echo (C) Change console color 62 | echo (D) Change console window dimensions 63 | echo (R) Run administrative prompt 64 | echo. 65 | echo (B) ^<-- Go back 66 | choice /c:CDRB /m "Please choose an option:" 67 | 68 | if %errorlevel%==1 call :Change-Color 69 | if %errorlevel%==2 call :Change-Dimensions 70 | if %errorlevel%==3 ( 71 | start "" runas /user:Administrator cmd.exe 72 | ) 73 | 74 | exit /b 0 75 | 76 | 77 | :Display-Colors-Numbers 78 | for /l %%a in (0, 1, 7) do ( 79 | color /? | findstr /C:"%%a = " 80 | ) 81 | exit /b 0 82 | 83 | :Change-Dimensions 84 | setlocal 85 | set /p lines=Enter desired number of lines: 86 | set /p cols=Enter desired number of columns: 87 | mode con: cols=%cols% lines=%lines% 88 | 89 | endlocal 90 | exit /b 0 91 | 92 | :Change-Color 93 | call :Display-Colors-Numbers 94 | 95 | setlocal 96 | set /P bg=Select background color number: 97 | set /P fg=Select foreground color number: 98 | :: Just take a single digit 99 | set bg=%bg:~0,1% 100 | set fg=%fg:~0,1% 101 | 102 | color %bg%%fg% 103 | endlocal 104 | exit /b 0 105 | 106 | :: 107 | :: Quit menu 108 | :Quit 109 | echo Thanks for using the Interactive Batch file^! 110 | echo. 111 | set /p "=This script will terminate in 5 seconds"nul 115 | ) 116 | exit /b 0 117 | -------------------------------------------------------------------------------- /useful_scripts/autox.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: 4 | :: The Batchography book by Elias Bachaalany -- http://amzn.to/1X3tQ4K 5 | :: 6 | 7 | :: 8 | :: Auto interpret anything 9 | :: 10 | 11 | :main 12 | setlocal 13 | 14 | set do_delay=1 15 | 16 | :: At least 3 arguments should be passed 17 | if "%3"=="" goto :help 18 | 19 | set do_cls= 20 | set do_showftime= 21 | set extra_args= 22 | set cmd_args= 23 | 24 | :: Take the required arguments 25 | set cmd_name=%~1 26 | shift 27 | 28 | set lang_name=%~1 29 | shift 30 | 31 | :: Parse the optional arguments 32 | :parse_args 33 | if "%1"=="/cls" ( 34 | set do_cls=1 35 | shift 36 | ) else if "%1" == "/showftime" ( 37 | set do_showftime=1 38 | shift 39 | ) else if "%1" == "/delay" ( 40 | set /a do_delay=%~2 41 | shift 42 | shift 43 | ) else if "%1" == "/args" ( 44 | set extra_args=%~2 45 | shift 46 | shift 47 | ) else if "%1" == "/cmdargs" ( 48 | set cmd_args=%~2 49 | shift 50 | shift 51 | ) else goto :break 52 | 53 | goto :parse_args 54 | 55 | :break 56 | call :autox "%~1" 57 | goto :eof 58 | 59 | 60 | :autox <1=FileName> 61 | if not exist "%~1" ( 62 | echo Input file '%1' not found 63 | exit /b 1 64 | ) 65 | 66 | set last_fdate=x 67 | 68 | title Batchography - %lang_name% auto re-interpreting '%~1' (press 'q' to quit) 69 | pushd "%~dp1" 70 | 71 | :repeat 72 | :: Get the file date/time (incl. seconds) 73 | for /f "delims=" %%i in ('"forfiles /p "%~dp1." /m "%~nx1" /c "cmd /c echo @ftime" "') do set fdate=%%i 74 | 75 | :: Different attributes found? 76 | if not "%last_fdate%"=="%fdate%" ( 77 | if defined do_cls cls 78 | if defined do_showftime echo ftime=%fdate% 79 | 80 | :: Re-interpret 81 | call "%cmd_name%" %cmd_args% "%~1" %extra_args% 82 | ) 83 | 84 | :: Remember the new date/time 85 | set last_fdate=%fdate% 86 | 87 | :: Wait for a second before checking for the file modification again 88 | CHOICE /T %do_delay% /C "yq" /D y > nul 89 | 90 | :: User pressed Q? just quit 91 | if "%errorlevel%" neq "1" goto :eof 92 | 93 | :: Repeat until user quits or Ctrl-C 94 | goto repeat 95 | 96 | :help 97 | echo Usage: autox 1=CommandName 2=LangName 3=Optional_Options FileName 98 | echo Optional arguments: 99 | echo /cls clear the screen before auto-interpreting 100 | echo /showftime show the file time stamp before auto-interpreting 101 | echo /delay nsecs the delay before auto-interpreting. default is %do_delay% second. 102 | echo /cmdargs optional additional arguments to be passed to the command 103 | echo /args "arg1 arg2" optional additional arguments to be passed to the filename 104 | goto :eof 105 | -------------------------------------------------------------------------------- /extract-embedded-files.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | :: 3 | :: (c) The Batchography book by Elias Bachaalany 4 | :: 5 | 6 | setlocal enabledelayedexpansion 7 | echo Extracting all embedded files... 8 | call :extract-all-files 9 | 10 | goto :eof 11 | 12 | 13 | :extract-one-by-one 14 | 15 | call :extract-embedded-file "Configuration-XML" f1.xml 16 | echo Return value: %errorlevel% 17 | 18 | call :extract-embedded-file "Readme" f2.xml 19 | echo Return value: %errorlevel% 20 | 21 | goto :eof 22 | 23 | 24 | :extract-all-files 25 | setlocal 26 | for %%a in ( 27 | "Configuration-XML=Config.xml" 28 | "Readme=README.txt" 29 | ) do ( 30 | for /f "usebackq tokens=1-2 delims==" %%b in ('%%~a') do ( 31 | set MSG=Extracting '%%b' to '%%c'... 32 | call :extract-embedded-file %%b %%c 33 | if %errorlevel% EQU 0 ( 34 | set MSG=!MSG!done. 35 | ) else ( 36 | set MSG=!MSG!failed. 37 | ) 38 | echo !MSG! 39 | ) 40 | ) 41 | 42 | endlocal 43 | goto :eof 44 | 45 | 46 | :extract-embedded-file <1=SectionName> <2=OutFile> 47 | setlocal 48 | 49 | call :get-line-no "//End-%~1" "%~f0" 53 | set MEND=%errorlevel% 54 | 55 | :: By default, we assume failure 56 | set err=1 57 | 58 | if "%MBEGIN%"=="-1" goto :extract-embedded-file-end 59 | if "%MEND%"=="-1" goto :extract-embedded-file-end 60 | 61 | :: Delete previous output file 62 | if exist "%~2" del "%~2" 63 | 64 | set /A C=MEND-MBEGIN-1 65 | 66 | for /f "useback skip=%MBEGIN% tokens=* delims=" %%a in ("%~f0") DO ( 67 | echo %%a >>"%~2" 68 | SET /A C-=1 69 | if !C!==0 ( 70 | :: Success 71 | set err=0 72 | goto :extract-embedded-file-end 73 | ) 74 | ) 75 | 76 | :extract-embedded-file-end 77 | ( 78 | endlocal 79 | exit /b %err% 80 | ) 81 | 82 | 83 | :get-line-no <1=string> <2=file> 84 | for /f "useback tokens=1 delims=: " %%a in (`findstr /N /C:"%~1" "%~2"`) DO EXIT /B %%a 85 | EXIT /B -1 86 | goto :eof 87 | 88 | 89 | // 91 | Microsoft-Windows-DesktopWindowManager-Diag 92 | 93 | {31F60101-3703-48EA-8143-451F8DE779D2} 94 | C:\windows\system32\dwmcore.dll 95 | C:\windows\system32\dwmcore.dll 96 | Microsoft-Windows-DesktopWindowManager-Diag 97 | 98 | 99 | 100 | 1 101 | Microsoft-Windows-DesktopWindowManager-Diag/Diagnostic 102 | Information 103 | DesktopWindowManager_DiagStats 104 | DesktopWindowManager-WDI 105 | 106 | 107 | 108 | //>End-Configuration-XML 109 | 110 | //End-Readme 114 | 115 | echo You can write more Batch file commands in here. -------------------------------------------------------------------------------- /parse-args.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :Main 4 | setlocal enabledelayedexpansion 5 | 6 | if "%1"=="" goto Usage 7 | if "%1"=="/?" goto Usage 8 | if "%1"=="/help" goto Usage 9 | 10 | set ForceFlag=0 11 | goto ParseActions 12 | 13 | :ParseActions 14 | :: Parse actions 15 | if /I "%1"=="adduser" set action=adduser 16 | if /I "%1"=="removeuser" set action=removeuser 17 | if /I "%1"=="removeuser" set action=removeuser 18 | if /I "%1"=="initscript" set action=initscript 19 | 20 | if "%action%"=="" goto Usage 21 | 22 | shift 23 | goto ParseOptions 24 | 25 | :ParseOptions 26 | set S= 27 | if "%1"=="" goto Handle-%action% 28 | 29 | if "%1"=="/user" ( 30 | if "%2"=="" ( 31 | :ErrNoUserPassed 32 | echo No username passed! 33 | goto Usage 34 | ) 35 | set Username=%~2 36 | set S=2 37 | ) 38 | 39 | if "%1"=="/password" ( 40 | if "%2"=="" ( 41 | :ErrNoPasswordPassed 42 | echo No password was specified 43 | goto Usage 44 | ) 45 | set Password=%~2 46 | set S=2 47 | ) 48 | 49 | if "%1"=="/force" ( 50 | set S=1 51 | set ForceFlag=1 52 | ) 53 | 54 | if "%1"=="/script" ( 55 | if "%2"=="" ( 56 | :ErrNoScriptPassed 57 | echo No script file specified 58 | goto Usage 59 | ) 60 | if not exist "%~2" ( 61 | echo The script file is not found! 62 | goto Usage 63 | ) 64 | set Script=%~2 65 | set S=2 66 | ) 67 | 68 | if not defined S ( 69 | echo Invalid switch! 70 | goto Usage 71 | ) 72 | FOR /L %%a in (1, 1, %S%) DO SHIFT 73 | 74 | goto ParseOptions 75 | 76 | :Handle-adduser 77 | if "%Username"=="" goto ErrNoUserPassed 78 | if "%Password%"=="" goto ErrNoPasswordPassed 79 | 80 | echo Adding user '%username%' with password '%Password%' 81 | goto :eof 82 | 83 | :Handle-removeuser 84 | if "%Username"=="" goto ErrNoUserPassed 85 | 86 | echo Removing user: %Username% 87 | goto :eof 88 | 89 | :Handle-initscript 90 | if "%Username"=="" goto ErrNoUserPassed 91 | if "%Script%"=="" goto ErrNoScriptPassed 92 | 93 | echo Setting user '%Username%' script @ %script% 94 | goto :eof 95 | 96 | :Usage 97 | echo Usage: 98 | echo ------ 99 | echo. 100 | echo %~n0 action [/option value] [/option] 101 | echo. 102 | echo To display help, run it as: 103 | echo. 104 | echo %~n0 /? ^| parse-args /help 105 | echo. 106 | echo Actions: 107 | echo -------- 108 | echo. 109 | echo * adduser /user username /password password 110 | echo. 111 | echo Adds a user to the system with the designated password. 112 | echo. 113 | echo * removeuser /user username [/force] 114 | echo. 115 | echo Removes a user from the system. If the /force flag is set then 116 | echo the current user is logged off and the account is removed. 117 | echo. 118 | echo * initscript /user username /script ^