ComputersFile Types

Bat file commands (Windows)

When working on a computer, it often happens that you need to repeat the same DOS commands for performing periodic actions, building a complex and multilevel sequence of executable commands or applications, automating work with applications and files with or without the user. In the operating system, it is possible to write several commands in a bat file. Simply by running this batch file (often called a script, or simply a "batch file"), you can execute the commands that you have written, and by registering it in the Windows Scheduler events, you can automate this process.

The peculiarity of this type of files is the absence of any code inside - there is only the clock information that the command DOS processor reads and executes, compatible with almost all versions of the Windows operating system. One of the striking examples is the autoexec.bat file, which is required to run this OS, since it is the one that configures it during startup.

Main features of batch files

Commands that contain a bat-file can be divided into several groups according to the functional:

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

Executing Command Files

You can run bat-files in various ways. When working in the command line in the current directory, you can simply enter file names with or without additional options. In this case, bat-file commands after startup will be executed sequentially one after another, already without user intervention.

If the command file is found in a different directory, you must specify the full path to the storage directory, in addition to the full name of the file. The file extension can also be omitted. For example, the current directory is d: \ photo \ work, and the vera.bat command file that you want to run with the resize.doc and / p options is in the d: \ photo \ home directory. Then in order to run our file, you need to set the command d: \ photo \ home \ vera resize.doc / p or the command .. \ home \ vera resize.doc / p.

Interruption of work

Executed after running the command bat-files can be interrupted by combinations of buttons + and + . The display will ask you to interrupt the execution of the batch file with a query that is waiting for the Yes or No. input. If you enter Y, the execution is interrupted, and the remaining list of commands for the bat file will be ignored. If you enter the symbol N, execution will continue with the next command from the list.

Calling another batch file

Bat-files can contain links to the execution of other scripts. If you just write a bat file in the script body, the commands after it will not be executed, since the control is transferred to another batch file and the commands are already executed from it. If you want to return after starting an internal file, you can call it with the CALL command. The format of the command looks like this: CALL filename -bat file [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 sequentially traversed, the execution of the original script will continue from the next line.

Step-by-step implementation

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

At execution on each command Y or N query will be issued. If it is required to execute the command - press Enter or Y. If you need to skip a command - press ESC or N.

Disable and enable the output of commands on the screen

When a bat-file is run, the commands are displayed line by line before execution and are then executed. Sometimes this causes inconvenience, because with a large command batch the long list runs before the user on the screen, and sometimes their display is required, for example, for dialogue or debugging. The command ECHO ON is used to display the command bat-file. To disable this mode, enter ECHO with the parameter OFF. In this case, all the commands that are executed next will not be displayed on the screen.

To disable the output of just one line of the batch file, you can use the @ symbol at the very beginning of this line. Most often, this symbol can be found paired with the @ECHO OFF command to exclude its output to the monitor display. This mode is often used by attackers, creating on the basis of bat-files viruses or delivering minor and major trouble to computer users.

Command execution messages

If, after the ECHO command, characters other than ON or OFF are located, 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 desired, you can play even the sound signal (squeak) of the computer, using the bat-file. Commands, examples of which are given below, are based on the ECHO statement with the addition of special characters.

To play the audio signal, you need to output a standard character with code 7. You can do this by pressing the and "7" keys on the numeric keypad (with the label "home").

In order to increase the readability of messages, you can use the ECHO command with the parameter "." (It is put immediately after the command without a space) or by a special symbol 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, rather than on the monitor screen. To save logs, questionnaires, record events, you can use bat files. The output commands have the following format:

- ECHO message >> file name - the command adds a line to the end of the 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 there is such a file in the current directory, its contents will be deleted and the command or its result will be recorded.

Use of comments

For the convenience of editing scripts, use the REM command. All Windows bat-files that are after it ignore when executed. They can be seen during editing and in step-by-step mode. In all other cases, comments on the screen will not be displayed.

Delay command in bat file

Sometimes, during the execution of a batch file, it becomes necessary to temporarily stop the execution of commands, for example, to wait for the user to respond, to increase the time for reading a long message, to insert a disk, confirm the presence of the user, or to debug a batch file. The delay command in the bat file is written as PAUSE without parameters. A message like "Press any key to continue" appears on the display, and the script will be suspended until the keyboard button is pressed. If the combination + or + is pressed, the system will take this as the completion of the executable file and issue a request to confirm the completion of its work: "Interrupt the execution of the batch file? (Y / N). " If you press the Y key, the script will stop working in this place and all remaining uncompleted commands will be ignored. If you press N, execution will continue from the next command. When paused, pressing any other alphanumeric keys, and also "Space" and "Entering" executing the executable file will continue with the next command.

Run the command for a list of files

For consecutive execution of several files from the list that are in the same directory, you can also use the bat-file. Commands "running the program by name" and "starting the program to expand the file" can often help with the automation of document processing, for example, sorting mail attachments. They differ in the parameters for the FOR command. The format of its writing is given below:

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

- x is any character, except for digits from 0 to 9;

- list - space-separated file names or the name of one file; In this case, you can use the special characters "?" And "*" to replace from 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 instead of% x you need to specify %% x.

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

Many functions with programs, such as comparing files with certain types of extensions in the same directory, with files of the same types from another directory, copying all the files of the current directory to another, sequential execution of files with extensions of a certain type and names beginning with numbers, You can do this with just one inscribed command in the bat file. Running the program can be combined with user requests, which increases the convenience of working with data.

Transitions in bat-files

For the convenience of managing the order of launching commands in bat-files, they often use the jump commands and the labels on which these transitions are made.
A label is any line of an executable file that starts with a colon character. A label name is a character set after a colon to the end of a line or up to the first space after which the rest of the line is ignored, which can be used as a comment to the transition label.

The jump command has a GOTO spelling with the label name via a space. If there is no parameter or the label name is not found in the command file, then the script is stopped on this command. Example:

GOTO zzz

...

: Zzz

REM (the required commands are run).

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

Using conditions when working with bat-files

The bat-file can execute or not execute commands depending on various conditions if in it the check of approach of event IF is applied. The format of this command looks like an "IF condition command".

The condition is a variety of expressions:

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

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

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

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

Applied commands in bat-files

If you click the "Start" button and start the command line (cmd), you can see all the commands that are 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 type HELP. This gives a list of commands with brief descriptions to them. For more information, you can enter the HELP with the name of the command you need.

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 sphere of their application are backup, creation, deletion, renaming and much more. All this is based on using the command bat-file COPY. For example, you can create a program that makes archival copies of documents from the selected folder with user data of certain formats, excluding, for example, large-scale media files, providing a backup in the folder with the user name and current date, and name it "Copy_Copy_File.bat". After that it is necessary to ensure its start at a certain time with the help of the built-in scheduler or by the program itself, and thus ensure the safety of the user's data.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.delachieve.com. Theme powered by WordPress.