Difference between revisions of "Batch Scripting"
Ralph.ebnet (talk | contribs) (Created page with "<seo title="Batch Scripting" metadescription="Batch scripting is a useful tool to automate repetitive tasks. But how does it work and what are common commands? Find out in our...") |
Ralph.ebnet (talk | contribs) (→Examples of common batch commands) |
||
Line 38: | Line 38: | ||
Lastly, the <code>for</code> command is another useful and common programming tool, which allows you to loop through files and statements to quickly iterate and execute code across a number of files or variables at once. A <code>for</code> loop contains a condition and code to execute every time that condition is or is not met. A simple <code>for</code> loop that runs through all the files in a user's profile looks like this: | Lastly, the <code>for</code> command is another useful and common programming tool, which allows you to loop through files and statements to quickly iterate and execute code across a number of files or variables at once. A <code>for</code> loop contains a condition and code to execute every time that condition is or is not met. A simple <code>for</code> loop that runs through all the files in a user's profile looks like this: | ||
− | '''For %%i in (%USERPROFILE%\*) do''' Run code here | + | '''For %%i in (%USERPROFILE%\*) do''' Run code here |
This can be used to quickly rename lots of files at once, change file extensions, modify file paths, and much more. It is very useful for automating tedious tasks across very many files. | This can be used to quickly rename lots of files at once, change file extensions, modify file paths, and much more. It is very useful for automating tedious tasks across very many files. |
Revision as of 15:11, 27 July 2020
Contents
Definition
Batch scripting is a useful tool for Windows software developers, that enables command line instructions to be executed like files. Developers can automate plenty of tasks on servers and on their local machines using batch files. It is especially useful for repetitive tasks, such as setting up new projects or automatically running routine tasks.
Batch files only work on Windows machines, although Mac, Linux, and other UNIX-like machines have similar alternatives. They are a relatively old piece of technology, having originally been developed for DOS and OS/2. As such, they are somewhat uncommon, even among developer circles now.
How it works
Batch files are files that usually end in .bat, but can also end in .cmd or .btm. A batch file is essentially a series of commands in a very high-level language that is read and executed by the operating system. A user can run a batch file by opening it just like they would a .exe file, but instead of opening a program, it will read and run a series of commands contained in the batch file.
Batch scripts are very simple, and commands are executed in sequence. Batch scripts essentially enable you to write and run a series of command prompt operations. Although Windows PowerShell is a more modern alternative that provides more options, batch scripts will still run on contemporary Windows 10 systems, so they can still be useful to developers.
Examples of common batch commands
There is a range of possible uses for batch scripts. From quickly changing the names of all the files within a folder to monitoring all computers connected to a network, batch scripts can do almost everything that can be done from the command prompt. Below are some specific examples of uses for batch files, and what batch files commonly look like.
Virtually every batch file will start with @echo off
on line 1. This is because, by default, Windows will echo (print on the screen) the name of the command being run. This is not often wanted behavior, so adding this on line 1 permanently turns off that feature for the rest of the file.
Common operators like if, else, and set
(to declare variables) are available for use. Special variables that include the name, path of the batch file running, and the current operating system version are also available. Batch scripts can also accept arguments when they run, which are accessible within the script using the convention %1, %2, %3 to refer to each of 3 arguments the file is run with.
The call
command is also very useful for more complicated scripts. Call
can either run a subprogram within a batch script, or it can be used to run an external batch script. This allows for better code organization and decoupling, allowing for certain best practice programming techniques to be used even when writing Windows batch files. Large scripts can easily get unwieldy, so being able to split them up into discrete functions or files helps a lot with development and readability for programmers.
Goto
is another common, though less frequently used, command. Goto
lets you jump to a specific place inside a script. This can be used to run a function or command outside of the usual linear execution path. Depending on how the script is written, this can be useful, though its use has fallen out of favor and is generally considered a 'code smell' by modern programming standards.
Return codes are an important part of batch scripts. By convention, when a function is executed on the command line, it should return zero if the execution was successful and a non-zero value otherwise. These return codes are intended to indicate whether the recently executed command was successful or not.
Fortunately, batch automatically contains the environment variable %ERRORLEVEL%
. This will contain the return code of whatever the most recently executed command was. As a return code of zero means a command was successfully executed, a common way of checking for errors is:
IF %ERRORLEVEL% NEQ 0 ( Execution failed code here )
NEQ stands for Not Equal (To), so if the return code was anything other than zero, a programmer can write additional code to handle the error. Unfortunately, return codes are a convention and not an enforced standard, so not all batch scripts may follow this convention.
Lastly, the for
command is another useful and common programming tool, which allows you to loop through files and statements to quickly iterate and execute code across a number of files or variables at once. A for
loop contains a condition and code to execute every time that condition is or is not met. A simple for
loop that runs through all the files in a user's profile looks like this:
For %%i in (%USERPROFILE%\*) do Run code here
This can be used to quickly rename lots of files at once, change file extensions, modify file paths, and much more. It is very useful for automating tedious tasks across very many files.