+5 votes
275 views
in Tools by (242k points)
reopened
Create a batch file: so you can write your own batch file

1 Answer

+3 votes
by (1.6m points)
edited
 
Best answer

What is a batch or bat file?
Create a batch: step by step tutorial
Examples of more complex and often-used batch scripts
Step 1: choose and start an editor
Step 2: become familiar with batch commands
Step 3: create and save the first batch file
Step 4: run the batch script created
Step 5: edit post-created batch files
Batch script with simple backup mechanism
Batch file with a more complex backup function

image

Create a batch file: so you can write your own batch file

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..

Index
  1. What is a batch or bat file?
  2. Create a batch: step by step tutorial
    1. Step 1: choose and start an editor
    2. Step 2: become familiar with batch commands
    3. Step 3: create and save the first batch file
    4. Step 4: run the batch script created
    5. Step 5: edit post-created batch files
  3. Examples of more complex and often-used batch scripts
    1. Batch script with simple backup mechanism
    2. Batch file with a more complex backup function

What is a batch or bat file?

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.

Note

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 .

Done

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..

Create a batch: step by step tutorial

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.

Step 1: choose and start an editor

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:

image

Step 2: become familiar with batch commands

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:

  • ECHO : connect the output text for commands executed on the screen
  • @ECHO OFF : turn off the output text for commands executed on the screen
  • START : run a file with the corresponding standard application
  • REM : mark a comment line
  • MKDIR / RMDIR : create or delete a directory
  • DEL: delete selected file (s)
  • COPY: copy selected file (s)
  • TITLE : edit the title of the CMD window
Note

You can consult a more detailed list of the most important commands in our extensive article dedicated to? Batch commands ?.

Step 3: create and save the first batch file

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.

image

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 ?:

image

Step 4: run the batch script created

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 .

image
Note

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:

  1. Access the Windows search function and type? cmd ?.
  2. Left click on? Command Prompt ? to start the command conventionally. Select the command with the right mouse button and then click? Run as administrator ? if administrator rights are required.
  3. Change to the directory where the file is located using the command? Change directory? (CD).
  4. Type in the name of the batch script (including the file extension) and hit the "Enter" key.
image

Step 5: edit post-created batch files

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 ?.

image

Examples of more complex and often-used batch scripts

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.

Batch script with simple backup mechanism

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  
image

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:

  • / m - Ensures that only updated files are copied so that files that have already been saved in previous backups are not copied again.
  • / e : indicates that all subdirectories included in the indicated directory are also taken into account for the backup.
  • / y - Suppresses the commit request to overwrite an existing target file (where changes have been made since the last backup).

Batch file with a more complex backup 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  
image

The rendered batch command code causes that:

  • Will all files in the output directory with the extensions .doc and .txt be copied to the backup folder? Texts ?
  • Will all files in the output directory with the extensions .jpg, .png and .bmp be copied to the backup folder? Pictures ?.
  • Only updated data will be copied.
  • The confirmation request will always be deleted.
Note

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.


...