Windows Batch Cheat Sheet
Windows Batch Scripting Essentials
Prefer REM over :: for comments (:: is an empty label and may cause problems).
^ is the line continuation character.
%* contains all the arguments passed to the script.
%~nx0 is the name of the batch file itself, whereas %0 is the command used to call the batch file.
%~d0 gives the drive of the batch file, e.g. F:\.
%~p0 gives the path of the batch file.
Handling Script Arguments
IF "%~1"=="" checks if the first parameter was passed. The ~ removes any surrounding quotes from the parameter.
Using the SHIFT Command
The SHIFT command changes the position of replaceable parameters. This is useful when you need to process more than 9 arguments.
Example: shift-test.bat 1 2 3 4 5 6 7 8 9 10
set LAST=%9%
shift
set AFTER_LAST=%9% <--- Gets the 10th argument after shifting
echo %LAST%
echo %AFTER_LAST%
Output:
9
10
Counting and Accessing Arguments
Count the number of arguments and print a message if there are too few.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set n=0
for %%i in (%*) do (
set /A n+=1
set "args[!n!]=%%~i"
)
echo # of parameters: %n%
if /I "%n%" LSS "4" goto error
echo The first parameter is: "!args[1]!"
echo The fourth parameter is: "!args[4]!"
if /I "%args[5]%" == "" echo no 5th argument
:error
set USAGE="Usage: %~nx0 [one] [two] [three] [four]"
echo %USAGE%
endlocal
Environment Variables with SETX
SETX is Persistent
The SETX command is used to set environment variables persistently. The /m switch sets the variable system-wide (for all users), affecting HKEY_LOCAL_MACHINE, while omitting it sets it for the current user (HKEY_CURRENT_USER).
setx /m JAVA_HOME=C:\Program Files\Zulu\zulu-8\
For more details, refer to What is the difference between SETX and SET in environment variables in Windows.
Retrieving Network Information
Retrieve IP Address into Variable
This command uses ping to get the local IP address and captures it into a variable.
for /f "delims=[] tokens=2" %%a in ('ping %computername% -4 -n 1 ^| findstr "["') do (set ip=%%a)
See How do I get the IP address into a batch-file variable? for more information.
Portable Development Environment Setup
Ant and Maven Build Shell on Portable Drive
This section demonstrates setting up a portable development environment for Ant and Maven on a portable drive.
Directory structure:
F:\
+--.m2
+--src
| +--project1
| +--project2
| +--project3
+--tools
| +--ant
| +--maven
| +--jdk
\--build.bat
\--mvn_ant_env.bat
Set up the tools environment:
@echo Setting Maven Environment...
set MAVEN_OPTS=-Xms256M -Xmx764M
set JAVA_HOME=%~d0\tools\jdk1.8.0_131
set M2_HOME=%~d0\tools\apache-maven-3.2.3
set ANT_HOME=%~d0\tools\apache-ant-1.9.4
PATH=%PATH%;%JAVA_HOME%\bin;%M2_HOME%\bin;%ANT_HOME%\bin
title Maven and Ant Enabled Shell
cmd
Now build some projects:
set HOME=%~d0
cd src\ism_version_0.2\base
call mvn -s %HOME%\.m2\settings.xml -Dmaven.repo.local=%HOME%\.m2\repository clean install
pushd %~dp0
cd src\daas_ags_conn
call mvn -s %HOME%\.m2\settings.xml -Dmaven.repo.local=%HOME%\.m2\repository clean install
pushd %~dp0
cd src\ags6
call ant all
pushd %~dp0
References and Further Reading
- Which comment style should I use in batch files?
- Batch-Script - Iterate through arguments
- How do you utilize more than 9 arguments when calling a label in a CMD batch-script?
- Arrays: Create list or arrays in Windows Batch and Arrays, linked lists and other data structures in cmd.exe (batch) script
- Get list of passed arguments in Windows batch script (.bat)
- Using parameters in batch files at DOS command line
- Using batch parameters
- Alternate: Using batch parameters
- Does %* in batch file mean all command line arguments?
- What does %~d0 mean
- What does %~dp0 mean
- %~dp0 vs %cd%
- Windows Batch Scripting: Advanced Tricks