The cmd.exe , also known as the command prompt, is one of the oldest software components in Windows. The command tool has been enabling direct changes to Microsoft operating systems for decades . Directly related to the command prompt are the so-called batch files (also bat files), which use system-specific commands to access cmd.exe and process the commands in a stacked manner. We will reveal to you what characterizes these useful scripts, and how you can create, save and run batch files..
A batch file (also a bat file, bat script, or stacked processing file) is a text file that the cmd.exe command tool can run as stacked processing . In this context, the command prompt takes on both the role of the interpreter and the runtime environment. In short, it is a computer program or script whose data or commands are processed in succession using the command prompt.
The term? Stacked processing? comes from the beginnings of data processing, when interactive processing was still impossible. At that time, the data sets to be processed used to be found on punch cards , the storage media of the time, which were processed in stacks (batch), one card after the other. In modern computer operating systems, the term has become known, especially in connection with the publication of MS-DOS (1981) and the batch files used in this context..
With cmd.exe as the interpreter and runtime environment, batch files allow you to use and run conventional CMD commands . Also, when creating batch files you can use comments , markers , variables , conditions and queries , among others. In order for text files to be converted to batch files, you must add the .bat extension on newer Microsoft systems. In Windows NT and OS / 2 the extension .cmd was used .
In 2006 Microsoft published PowerShell , a framework that since 2016 has been developed for all platforms and under the free MIT license and that also allows the programming and execution of stacked processing files. In this context, PowerShell offers an alternative command interpreter and its own scripting language called the PowerShell Scripting Language..
Creating a batch is very useful if you regularly deal with scripts that are repeated over and over again and want to automate their execution. It may be, for example, processes logon or the start of so- called TSR (programs t erminate and s tay r esident ) you want to run continuously in the background. In the next few paragraphs we explain what tools you will need to create batch files and how you can create, save, and run your own stacked processing files.
As we mentioned previously, text documents are a good basis for batch scripts . To write your own batch file, you will only need a conventional text editor. Since you will not need features such as syntactic highlighting, the Microsoft Editor (also known simply as Editor or Notepad) built into Windows by default is sufficient. To open it, you just have to type the term? Notepad? or? Editor? in Windows search and click the corresponding icon in the search results:
If you want to create a batch, you don't have to learn the complex programming language. But it is essential that you have knowledge about conventional system commands and their functionalities in batch files , so you should first familiarize yourself with some commands before starting to create your own script. Among the most important commands that you should know, we want to highlight the following:
You can consult a more detailed list of the most important commands in our extensive article dedicated to? Batch commands ?.
A simple first contact in the art of creating a batch is the elaboration of a simple script that creates several directories in a data medium selected in your system. If, for example, you create a batch file with the following input, after executing it on drive C: this file generates two directories named? Example1 ? Y ? Example2 ?:
MKDIR C:\Ejemplo1 MKDIR C:\Ejemplo2
Just copy both lines to an empty Notepad document, just as the screenshot shows.
In order to save this bat statement or the script, click? File ? and select the menu item? Save as? ? Specify the storage location and type a suitable name for the script, including the .bat extension , in the? Field. File name ?:
Once the bat file is created and saved as such, you have two options to run it: you can open the script through the familiar Windows explorer environment, or you can open the command prompt and start the script through the corresponding command.
In this context, the first option is simpler and more appropriate for beginners, since you only have to access the directory where the batch file is located and open it by double clicking .
If a batch script contains commands that require administrator rights to run, you can only start them as administrator. In this case, you select the stacked render file with the right mouse button and click the option "Run as administrator".
However, if you want to open the batch file through a command, proceed as follows:
Once a batch script has been created, you can adapt it at any time if, for example, you want to add other commands, delete existing commands or modify directories. To do this, simply go to the folder where the command script is located and select it with the right mouse button. Then activate the option? Edit ?.
By following the step-by-step instructions above you can create batch files of all kinds, regardless of how many system commands they include. In this context, the usefulness of a created script is directly proportional to the number of times it can be used. In the following, we will use two examples of durable value batch files to demonstrate the capabilities of stacked processing using the Windows command window.
The following example illustrates the usefulness of batch files, as it represents an easy way to make regular backups of any directory.
XCOPY C:\Directorio de salida C:\Directorio de copia de seguridad /m /e /y
When you create and run a batch file containing the line represented at the top, what you are doing is using the command? XCOPY? to copy the contents of the? output folder? to the? backup folder ?; Of course, you must adapt the directories in which the two folders are located accordingly. In this process, the three parameters have the following function:
The backup program explained above allows you to copy the output files from the output folder to the destination folder. You can also create batch files that cause the original data to be divided into several destination folders, and that, for example, the type of file serves as the selection criteria. For this objective you need to make use of the so-called FOR loop that allows executing a command several times with a variable argument:
cd C:\Directorio de salida FOR %%f IN (*.doc *.txt) DO XCOPY C:\Directorio de salida"%%f" C:\Directorio de copia de seguridad\Textos /m /y FOR %%f IN (*.jpg *.png *.bmp) DO XCOPY C:\Directorio de salida"%%f" C:\Directorio de copia de seguridad\Imágenes /m /y
The rendered batch command code causes that:
The script only works with files whose names have defined extensions , otherwise the corresponding documents will not be recognized in batch processing, even if they are properly formatted.