- Platform: Windows
- Interpreter:
cmd.exe
- File Extension:
.bat
or.cmd
- Syntax: Specific to DOS/Windows command line
Print¶
In Batch
scripting, command echoing
refers to the display of commands in the command prompt window as they are executed.
By default, when you run a
batch
script, each command is displayed (echoed) in the command prompt before it is executed.Echo: The
echo
command is used to display messages or toggle command echoing.@echo off
: Turns off command echoing for the batch file.echo Hello, World!
: Displays the message "Hello, World!".@echo off echo Hello, World!
Variables¶
Variables are used to store data. Use
set
to define variables.set name=John echo Hello, %name%!
Environment Variables¶
Special variables that contain
system
anduser-specific
information.echo %USERPROFILE%
: Displays the path to the user's profile directory.echo %USERPROFILE%
To set an environment variable for the current session, you can use the
set
command in the command prompt:set MY_VARIABLE=MyValue
To set an environment variable permanently, you can use the
setx
command:setx MY_VARIABLE "MyValue"
Conditionals¶
Use
if
statements for conditional execution.if "%name%"=="John"
: Checks if the name variable is "John".if "%name%"=="John" ( echo Welcome, John! ) else ( echo You are not John. )
Loops¶
The for
loop in Batch
scripting is used to iterate over a set of items or to perform a task repeatedly.
Here’s the detailed syntax and usage for different scenarios:
for %%variable in (set) do command [command-parameters]
Example
@echo off for %%i in (apple banana cherry) do ( echo %%i ) :: Output :: apple :: banana :: cherry
- Iterating Over Files in a Directory
- Syntax :
for %%variable in (path\*extension) do command [command-parameters]
- Example
@echo off for %%f in (*.txt) do ( echo Found file: %%f ) :: Output :: Found file: file1.txt :: Found file: file2.txt
- Syntax :
- Iterating with a
Counter
- Syntax:
for /l %%variable in (start,step,end) do command [command-parameters]
- Example
(1,1,5)
: Start at 1, increment by 1, and end at 5./l
: Specifies a numerical range.
@echo off for /l %%i in (1,1,5) do ( echo Count: %%i ) :: output :: Count: 1 :: Count: 2 :: Count: 3 :: Count: 4 :: Count: 5
- Syntax:
Functions¶
In Batch
scripting, functions
are implemented as subroutines
.
- These
subroutines
are sections of the script identified by labels. You can call them using thecall
command.
Defining and Calling a Function¶
Syntax
goto :eof
: Ensures the script returns to the point after thecall
command, instead of continuing into the next function.:label commands goto :eof
Example
@echo off rem Call the function call :myFunction rem Continue with the rest of the script echo Script completed. exit /b :myFunction echo This is a function. goto :eof ::output :: This is a function. :: Script completed.
Passing Parameters to Functions¶
Syntax
call :greet John Doe
: Calls thegreet
function with parametersJohn
andDoe
call :label parameter1 parameter2 :label commands %1 %2 goto :eof
Example
echo Hello, %1 %2!
: Uses%1
and%2
to refer to the first and second parameters.@echo off rem Call the function with parameters call :greet John Doe rem Continue with the rest of the script echo Script completed. exit /b :greet echo Hello, %1 %2! goto :eof :: output :: Hello, John Doe! :: Script completed.
Reading Files¶
type
: echo content of filestype file.txt
Deleting Files¶
del file.txt
: Deletesfile.txt
.del file.txt
Changing Directories¶
cd newfolder
: Changes the current directory tonewfolder
.cd newfolder
Listing Directories:¶
dir
: Lists files and directories in the current directory.
Examples¶
Setting a Variable: Defines a variable
name
and displays a greeting.Creating and Navigating a Directory: Creates
myfolder
and changes to it.File Operations: Creates
myfile.txt
, writes to it, and reads it.Conditional Check: Checks if
myfile.txt
exists.Loop: Iterates through numbers and writes them to the file.
Function Call: Calls a function that displays a message.
Cleanup: Deletes the created directory and its contents.
@echo off REM Set and display a variable set name=John echo Hello, %name%! REM Create a directory and navigate to it mkdir myfolder cd myfolder REM Create a file and write to it echo This is the first line of text > myfile.txt echo This is the second line >> myfile.txt REM Read the file contents type myfile.txt REM Check if a file exists if exist myfile.txt ( echo myfile.txt exists. ) else ( echo myfile.txt does not exist. ) REM Loop through numbers and write to a file for %%i in (1 2 3) do ( echo Number %%i >> myfile.txt ) REM Read the file contents again type myfile.txt REM Call a function call :myFunction REM Clean up cd .. rmdir /s /q myfolder exit /b :myFunction echo This is inside the function. exit /b
Another Example¶
- Copy
arcgispro-py3
env to local and install required libraries
if exist "%DEST_DIR%"
: The variable%DEST_DIR%
is enclosed in quotes to handle directories with spaces correctly.set "CONDA_DEFAULT_ENV=arcgispro-py3-addins"
: The quotes around the entire assignment ensure that if the value contained spaces,- it would still be set correctly. In this case, the value
arcgispro-py3-addins
doesn't contain spaces, but using quotes is a good habit.
@echo off :: Set source and destination directories set SOURCE_DIR=C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3 set DEST_DIR=%USERPROFILE%\AppData\Local\ESRI\conda\envs\arcgispro-py3-addins :: Check if the source directory exists if not exist "%SOURCE_DIR%" ( echo Source directory does not exist: %SOURCE_DIR% exit /b 1 ) :: Copy the source directory to the destination echo Copying from %SOURCE_DIR% to %DEST_DIR%... xcopy "%SOURCE_DIR%" "%DEST_DIR%" /E /I /H /C /Y if errorlevel 1 ( echo Failed to copy the directory. exit /b 1 ) echo Directory copied successfully. rem Check if the constructed path exists if exist "%DEST_DIR%" ( rem Set environment variables to mimic activation set "CONDA_DEFAULT_ENV=arcgispro-py3-addins" set "CONDA_PREFIX=%DEST_DIR%" rem Add the environment's Scripts directory to the PATH set "PATH=%DEST_DIR%;%DEST_DIR%\Scripts;%PATH%" rem Installing required libraries from requirements.txt... pip install -r requirements.txt echo Libraries installed successfully. ) else ( echo Failed to install required libraries. ) pause
- it would still be set correctly. In this case, the value