Bat Commands (Windows)

When working on a computer, it often becomes necessary to repeat the same DOS commands to carry out periodically executed actions, to build a complex and multi-level sequence of executable commands or applications, to automate working with applications and files with or without a user. In the operating system, it is possible to write several commands in a bat-file. At the same time, simply by running this batch file (often it is called a script, or simply a “batch file”), it is possible to execute the prescribed commands, and by registering it in the events of the Windows scheduler, you can automate this process.

A feature of this type of file is the absence of any code inside - there is only clock information that is read and executed by a DOS command processor that is compatible with almost all versions of the Windows operating system. One of the striking examples is the autoexec.bat file, which is necessary to run this OS, since it is he who carries out its settings during startup.

bat file commands

Key features of batch files

The commands that the bat-file contains can be divided into several groups according to their functionality:

- designed to display a specific value, phrase, application or information;
- for launching batch documents;
- for work with cycles;
- to create branching execution using conditions;
- for working with applications.

bat command file

Batch file execution

Bat files can be launched in various ways. When working on the command line in the current directory, you can simply enter file names with or without additional parameters. In this case, bat file commands after launch will be executed sequentially one after another, already without user intervention.

When the batch file is in a different directory, in addition to the full name of this file, it is necessary to register the full path to its storage directory. The file extension can also be omitted. For example, the current directory is d: \ photo \ work, and the batch file vera.bat, which must be run with the resize.doc and / p parameters, is located in the d: \ photo \ home directory. Then, in order to launch our file, you need to register the d: \ photo \ home \ vera resize.doc / p command or the .. \ home \ vera resize.doc / p command.

Work interruption

Bat-file commands executed after the launch can be interrupted by the combinations of the buttons <Ctrl> + <Break> and <Ctrl> + <C>. A message appears on the display to interrupt the execution of the batch file with a request waiting to enter Yes or No. If you enter Y, then execution will be interrupted, and the remaining list of bat file commands will be ignored. If you enter the character N, execution will continue with the next command from the list.

bat command file launch program

Call another batch file

Bat-files may contain links to the execution of other scripts. If you simply register a bat-file in the script body, the commands after it will not be executed already, since control is transferred to another batch file and the commands will be executed from it already. If you need to return after starting the internal file, you can call it with the CALL command. The format of the command looks like this: CALL bat-file name [startup parameters].

The parameters that are passed to the called batch file (s) are usually the characters% 1 -% 9. When the bat-file commands are all successively passed, the execution of the source script from the next line will continue.

several commands in bat file

Step by step execution

There are times when bat-file commands need to be executed step by step (with a stop after each line). Most often, this mode is required when debugging or testing scripts. The spelling of the command looks like this: COMMAND / y / c script_name [parameters].

When executed, a request Y or N will be issued for each command. If a command is required, press the Enter or Y key. If you want to skip the command, press ESC or N.

commands for writing bat files

Disabling and enabling the output of commands to the screen

When the bat-file is run, the commands are displayed line by line before execution and are then executed. Sometimes this causes inconvenience, since with a large batch file teams run a long list in front of the user on the screen, and sometimes they need to be shown, for example, for dialogue or debugging. In order for the command bat-file to display, the ECHO ON command is used. To disable this mode, enter ECHO with the OFF parameter. In this case, all the commands executed below will not be displayed on the screen.

To disable the output of only any one line of a batch file to the screen, you can use the @ symbol at the very beginning of this line. Most often, this symbol can be seen paired with the @ECHO OFF command to exclude its display on the monitor display. This mode is often used by attackers, creating viruses based on bat-files or delivering small and large troubles to computer users.

bat command file examples

Command Execution Messages

If characters other than ON or OFF are located after the ECHO command, they will be displayed in any mode. If ECHO is without parameters, then it displays the status of the display mode of executable commands - on or off.

If you wish, you can even play a sound (squeak) computer using a bat-file. Commands, examples of which are given below, are based on the ECHO operator with the addition of special characters.

To play a sound signal, you need to display the standard character with code 7. You can do this by pressing the <alt> and "7" keys on the numeric keypad (labeled home).

In order to increase the readability of messages, you can use the ECHO command with the “.” Parameter (it is placed immediately after the command without a space) or with a special character with the code 255. On the screen it will look like an empty line.

The ECHO command can be used to output messages to a separate file. Its essence is to redirect the output of messages to write to a file, and not to the monitor screen. To save logs, questionnaires, record events, you can use bat bat files . Output commands have the following format:

- ECHO message >> file name - the command adds a line to the end of an existing file. If there is no such file in this directory, a new file is created.

- ECHO message> file name - this command creates a new file, and the current line is written to it. If such a file exists in the current directory, its contents will be deleted and the command or its result will be recorded.

Using comments

For ease of editing scripts, the REM command is used. Windows ignores all bat file commands after it when executed. They can be seen when editing and in step-by-step mode. In all other cases, comments will not be displayed on the screen.

list of bat file commands

Delay command in bat file

Sometimes during execution of a batch file it becomes necessary to temporarily stop the execution of commands, for example, to wait for a user’s response, to increase the time it takes to read a long message, to insert a disk, to confirm the presence of a user, or to debug a batch file. The delay command in the bat file is written as PAUSE without parameters. At the same time, a message like “Press any key to continue” will be displayed, and the script will be paused until the keyboard button is pressed. If the combination <ctrl> + <break> or <ctrl> + <c> is pressed, the system will perceive this as shutting down the executable file and will ask you to confirm the completion of its work: “Abort the batch file? (Y / N). " If you press Y, the script will be interrupted in this place and all remaining outstanding commands will be ignored. When you press N, execution will continue with the next command. When paused, pressing any other alphanumeric keys, as well as "Space" and "Enter", the execution of the executable file will continue with the next command.

batch files bat commands

Executing a command for a list of files

For sequential execution of several files from the list located in the same directory, you can also use the bat-file. The commands “run the program by name” and “run the program to expand the file” can often help with automating the processing of documents, such as sorting mail attachments. They differ in the parameters for the FOR command. The format for writing it is given below:

- FOR% x IN (file list) DO command, where:

- x - any character, with the exception of numbers from 0 to 9;

- list - space-separated file names or the name of a single file; you can use special characters "?" and “*” to replace one to several characters in the file name;

- command - any DOS-command or program, except FOR; if there is a need to use FOR, then %% x must be specified instead of% x.

copy command bat

When using this command to write bat-files it is necessary to take into account that names in DOS format programs have a restriction on the name length. In order to switch the FOR command to work with long file names, you must remember to run the LFNFOR ON command in front of it to turn on the long name mode and LFNFOR OFF to turn it off and return to the standard file length.

Many functions with programs, such as comparing files with certain types of extensions located in one directory with files of the same types from another directory, copying all files in the current directory to another, sequential execution of files with extensions of a certain type and names starting with numbers, can be done with just one command entered in the bat-file. Launching the program can be combined with user requests, which helps to increase the convenience of working with data.

windows file bat commands

Transitions in bat files

For convenience, controlling the order in which batches are run in bat files, they often use transition instructions and labels by which these transitions are performed.
A label is any line in an executable file that begins with a colon. The label name is the character set after the colon to the end of the line or to the first space, after which the rest of the line is ignored, which can be used as a comment on the jump label.

The jump command has a GOTO spelling with the name of the label separated by a space. If there is no parameter or the name of the label in the batch file is not found, then the script stops working on this command. Example:

Goto zzz

...

: zzz

REM (required commands are run).

In this example, sequential execution of the batch file, reaching GOTO zzz, goes to the label: zzz and continues with REM, ignoring everything that is between the jump command and the specified label.

command to copy bat files

Using conditions when working with bat-files

A bat file may or may not execute commands depending on various conditions, if it uses the IF event check. The format of this command is “IF condition command”.

The condition is a variety of expressions:

- ERRORLEVEL number - in the case when the previous executable program ended with the exit code equal to or greater than the specified numerical value, the condition will be true.

- LINE 1 == LINE 2 - in case of complete coincidence of lines, the condition becomes true. Instead of strings, you can substitute the characters% 0 -% 9, then the parameters of the command file will be compared.

- EXIST - file name - if the specified file exists, then the condition is true.

- NOT - condition - if the specified condition is false, then at the output of the command we get the true value.

bat cmd command file

Applied Commands in Bat Files

If you click the "Start" button and run the command line (cmd), then you can see all the commands inserted into the bat-file . CMD mode can be used as a tool for debugging, as an assistant, a program for obtaining information about commands. To do this, you can simply dial HELP. This gives a list of commands with brief descriptions of them. For more detailed information, you can enter HELP with the name of the desired command.

delay command in bat file

Using bat files when backing up data

Command files greatly simplify the life of system administrators in the implementation of daily work. The most common areas of their application are backing up, creating, deleting, renaming and much more. All this is based on using the COPY bat file command. For example, you can create a program that makes archive copies of documents from a selected folder with user data of certain formats, excluding, for example, large media files, providing backup in a folder with a user name and current date, and name it "Copy_file_file.bat" command. After that, it is necessary to ensure its launch at a certain time using the built-in scheduler or by the program itself and thereby ensure the safety of user data.

Source: https://habr.com/ru/post/C33814/


All Articles