Whatever commands that you are manually performing in windows console window, can be automated using batch file. A Windows batch file is just a text file saved with *.bat extension.
A batch file is not as powerful as a programming language like Python, but in some cases can get the job done. The major advantage of batch file is that, it can be run in any Windows PC without installing any additional programs and it is fairly known among all programmers regardless of their programming language of choice.
This article is my reference guide for syntax whenever I want to do something in batch file.
Basics of Windows Batch File
This covers some of the core basics of batch file.
:: Comments can be added to batch file using '::' at the beginning | |
@REM This is another way to add comment to a batch file | |
:: Printing to Console | |
echo This prints anything to the console window | |
:: Use the "timeout" command to make the console wait for a specified timeout in seconds | |
:: If user presses any key during the timeout, it will immediately proceeds | |
:: If user has not pressed any key and the timeout period is elapsed, it will proceed to the execution of next line | |
timeout 60 | |
:: To pause indefinitely till user presses a key, use the "pause" command | |
pause | |
:: To call another batch file, use the call command | |
:: File/Folder paths in batch file can either be absolute or relative to the current path | |
call SomeFolder\SubFolder\SomeBatchFileName.bat | |
:: Deleting a folder/directory | |
RMDIR /S /Q FolderName | |
:: Deleting a file | |
del /f SomeFolder/SubFolder/RandomFile.txt | |
:: Copying file contents of one folder to another | |
robocopy SourceFolder DestinationFolder |
Loops and Structures in Windows Batch File
This covers creation of arrays and usage of loops in batch file.
:: Turning echo off to disable printing of each line of below file to console | |
@echo off | |
:: Delayed expansion has to be enabled, to dynamically assign values for variables using "!VariableName!" | |
setlocal enabledelayedexpansion | |
:: You can use Windows environmental variables like %LocalAppData% | |
:: If you want to get the path of a folder called "MyRandomFolder" inside an environmental variable like above, use as below. | |
set MyLocalPath=%LocalAppData%/MyRandomFolder | |
echo !MyLocalPath! | |
:: For list of more such variables, refer to the table at https://en.wikipedia.org/wiki/Environment_variable#Windows | |
:: Assigning a value for a variable using "set" command | |
:: Below is a string variable | |
set MyModeFlag=dev | |
:: To define a variable as a number, use "/A" like below | |
set /A MyFavoriteNumber=2 | |
:: Example IF..ELSE Case | |
IF %MyModeFlag%==dev ( | |
echo Dev Mode. Value of MyFlag was !MyFlag! | |
) ELSE ( | |
echo Production Mode. Value of MyFlag was !MyFlag! | |
) | |
:: Defining Arrays in Batch File | |
:: Technically arrays are not supported in batch file | |
:: But we can statically define multiple variable names which will mimic an array, like below | |
set MyPaths[0]=C:\Program Files\Mozilla Firefox\firefox.exe | |
set MyPaths[1]=C:\Program Files (x86)\Notepad++\notepad++.exe | |
set MyPaths[2]=C:\Program Files\Google\Chrome\Application\chrome.exe | |
:: Using two other variables to denote the start index and end index of the array | |
set /A start=0 | |
set /A step=1 | |
set /A end=2 | |
:: Using For loop | |
:: Use /L for Numeric Looping of For loop | |
:: In below example, "%%a" is the iteration variable | |
:: Single command For Loop. Replace echo with your desired command | |
for /L %%a in (%start%,%step%,%end%) do echo !MyPaths[%%a]! | |
:: For multiline commands, use brackets as below | |
for /L %%a in (%start%,%step%,%end%) do ( | |
echo This is iteration %%a | |
echo Current path is !MyPaths[%%a]! | |
) | |
:: For other types of For Loop, refer to https://ss64.com/nt/for.html | |
:: Pause - Wait for any key press | |
pause |
Have you used batch file for any purpose? Have any useful tips/tricks related to batch file, let us know in the comments :-)
No comments:
Feel free to leave a piece of your mind.