Skip to content

Instantly share code, notes, and snippets.

@anandrajneesh
Last active November 1, 2017 18:47
Show Gist options
  • Save anandrajneesh/64000809ae05c21574bc8adadb9dd03b to your computer and use it in GitHub Desktop.
Save anandrajneesh/64000809ae05c21574bc8adadb9dd03b to your computer and use it in GitHub Desktop.
Windows scripting
@ECHO OFF
SETLOCAL
:: script global variables
SET me=%~n0
SET log=%TEMP%\%me%.txt
:: The "main" logic of the script
IF EXIST "%log%" DELETE /Q %log% >NUL
:: do something cool, then log it
CALL :tee "%me%: Hello, world!"
:: force execution to quit at the end of the "main" logic
EXIT /B %ERRORLEVEL%
:: a function to write to a log file and write to stdout
:tee
ECHO %* >> "%log%"
ECHO %*
EXIT /B 0
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
:: This demonstrates some tricks with command line arguments
:: ~ strips off quotes
:: %n0 gives the file(cmd) name, if file name is cmdlinetipstest.cmd then this will give cmdlinetipstest
:: %dp0 gives the path to parent folder
:: %f0 gives the path till cmd
:: %fs0 gives the short path till cmd this is to avoid spaces
:: %nx0 gives the cmd name and extension. like cmdlinetipstest.cmd
SET me=%~n0
SET parent=%~dp0
SET whatnx=%~nx0
SET whatfs=%~fs0
SET whatf=%~f0
ECHO me : %me%
ECHO parent : %parent%
ECHO whatnx : %whatnx%
ECHO whatfs : %whatfs%
ECHO whatf : %whatf%

Looping Through Files

FOR %I IN (%USERPROFILE%*) DO @ECHO %I

Looping Through Directories

FOR /D %I IN (%USERPROFILE%*) DO @ECHO %I

Recursively loop through files in all subfolders of the %TEMP% folder

FOR /R "%TEMP%" %I IN (*) DO @ECHO %I

Recursively loop through all subfolders in the %TEMP% folder

FOR /R "%TEMP%" /D %I IN (*) DO @ECHO %I

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET /A errno=0
SET /A ERROR_HELP_SCREEN=1
SET /A ERROR_SOMECOMMAND_NOT_FOUND=2
SET /A ERROR_OTHERCOMMAND_FAILED=4
SomeCommand.exe
::This is doing bitwise OR operation thats why errors above are powers of 2. Multiple errors can be tracked by this.
IF %ERRORLEVEL% NEQ 0 SET /A errno^|=%ERROR_SOMECOMMAND_NOT_FOUND%
OtherCommand.exe
IF %ERRORLEVEL% NEQ 0 (
SET /A errno^|=%ERROR_OTHERCOMMAND_FAILED%
)
EXIT /B %errno%

Checking that a File or Folder Exists

IF EXIST "temp.txt" ECHO found

Or the converse:

IF NOT EXIST "temp.txt" ECHO not found

Both the true condition and the false condition:

IF EXIST "temp.txt" ( ECHO found ) ELSE ( ECHO not found )

Checking If A Variable Is Not Set

IF "%var%"=="" (SET var=default value)

Or

IF NOT DEFINED var (SET var=default value)

Checking If a Variable Matches a Text String

SET var=Hello, World!

IF "%var%"=="Hello, World!" ( ECHO found )

Or with a case insensitive comparison

IF /I "%var%"=="hello, world!" ( ECHO found )

Artimetic Comparisons

SET /A var=1

IF /I "%var%" EQU "1" ECHO equality with 1

IF /I "%var%" NEQ "0" ECHO inequality with 0

IF /I "%var%" GEQ "1" ECHO greater than or equal to 1

IF /I "%var%" LEQ "1" ECHO less than or equal to 1

Checking a Return Code

IF /I "%ERRORLEVEL%" NEQ "0" ( ECHO execution failed )

for directing stdout to destination like DIR > somefile.txt

for append < to take input

0 - stdin 1 - stdout 2 - stderr

to redirect both stderr and out to same file 2>&1 is the trick To suppress the output use > NUL

Windows scripting gists

TYPE CON > output.txt

Ctrl z for EOF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment