People who are familiar with the term batch file know that BAT files can greatly simplify life and save time if you know how to write and use them correctly. In this article, I will talk about how to create BAT files and introduce you to common mistakes that usually occur when writing them.

Creating a BAT file is very easy. Just open notepad and save blank sheet with the .bat extension, selecting the Save As... option and writing something ending in .bat in the Filename field, such as test.bat .
Specify the file type as in the screenshot below - All files. Save and get BAT file.

You can edit the BAT file in notepad or any other code-oriented text editor.

Now let's move on to practical information. On the net, many are looking for an answer to the question How to deal with spaces in BAT files? . In paths to folders and executable files, the presence of a space causes an error. The most common answer is: Enclose the path in quotation marks. And this answer is not correct. True, some will argue with foam at the mouth that it works. So, two whys appeared - why it is not true and why some will be.

On Windows (as, indeed, on UNIX), the programs installed on the system are registered by the system accordingly. Therefore, some of installed programs can be launched with one simple command from a BAT file or from the Start panel's Run applet. One such program is Firefox:

start firefox

If after this command you write the path to the executable file, then the following happens: the Firefox browser starts and tries to process the request, that is, the file whose path is specified. That is, if you specify the following:

start firefox C:\Program Files\Mozilla Firefox\firefox.exe

The browser will open, whatever is written after start firefox . That is why some comrades will assure that everything works fine. However, if you take a portable program, the situation will be completely different. Consider as an example filezilla ftp client. Since the system does not know about the program, the above line

start filezilla

will not work. To run a program unknown to the system, you must specify the path to it:

start D:\FileZilla\FileZilla.exe

Long names in bat files

Now let's talk about paths and spaces. The first way to avoid this problem is to use a short name.

start C:\Program Files\Sound Club\scw.exe

In the example, there are two names with spaces. Let's replace them with short ones. The rules for creating short names are as follows: in the short name, the first six characters of the name are used without spaces, after the name, the sequence number of the folder is indicated using the symbol ~ . Since I have the Program Files and Sound Club folders in the singular, I get the following:

Program Files - Progra~1 Sound Club - SoundC~1 start C:\Progra~1 \SoundC~1 \scw.exe

If there are two folders nearby, for example Sound Club and Sound Clown , then following the rules, in the example above, you will need to specify SoundC ~ 2 , since in this case Sound Club will be the second name (names are considered in alphabetical order).

But this method is inconvenient because you have to specify sequence numbers. The situation with Program files is more or less normal. Few people will meet two similar folders on system drive. But if you choose to install multiple Mozilla products on your computer. You will get several folders, for example:

Mozilla Firefox Mozilla Thunderbird Mozilla Sunbird

Their short names would be

Mozill~1 Mozill~2 Mozill~3

Now imagine that you wrote a BAT file mentioning these programs. If you remove Firefox, the remaining entries will stop working, and if you remove Thunderbird, the entry for Sunbird will stop working. In short, the way with short names is not our way.

Spaces and quotes in bat files

Quotes actually work, but not in the ways that are usually advised. The following is usually advised:

start "C:\Program Files\Sound Club\scw.exe"

This will not work, because if you look at the help for it ( start /? ), you will see the following in the help:

START ["header"] [command/program] [options]

As you can see, the first parameter is the title of the window and it is in quotes. This parameter is optional, but it is still advised to specify () to avoid errors when executing the command. You can not write anything inside quotes. It will turn out like this:

start "" "C:\Program Files\Sound Club\scw.exe"

The option with quoting all names with spaces separately will also work:

start C:\"Program Files"\"Sound Club"\scw.exe

However, in some cases none of the above works. In such cases, I can advise using the cd command. Let's go to system partition, then cd into the Program Files folder and run the program ( start ):

%SystemDrive% cd \Program Files\Sound Club\ start scw.exe

I think this way will work everywhere. Now a couple more important points. Suppose you have created a batch file that launches three programs and you need to temporarily exclude the launch of one of the three. This can be done by deleting the line or by commenting it out. The first way is vandal, and the second one is below.

start firefox start jetaudio rem start defraggler

In this case, the launch of the Defraggler.exe program installed on the system is disabled. Comment lines by adding the rem command at the beginning of the line. All BAT files are executed in the console window. To make it disappear at the end of the execution of commands, do not forget to write the exit command at the end.

start firefox start jetaudio rem start defraggler exit

Launching applications from a bat file

In the first part of the article, I talked in general terms about BAT files. Now it became clear - what it is and what it is eaten with. In the second part, we will talk about more specific things. For example, about how to run several applications using a BAT file with certain settings or in automatic mode install the program so as not to waste time on answers like you agree with the terms license agreement? and don't push any extra buttons.

The above outlined several ways to launch applications using a BAT file. The very first one is a short command to launch the program installed in the system.

start firefox

It doesn't always work. Therefore, such a technique can be fully applied to a particular system, but it is not suitable as a universal solution. If there is a goal to make the BAT file work everywhere and always, you need to use full paths:

start C:\"Program Files"\"Mozilla Firefox"\firefox.exe

I also noted that the command to complete must be present in the BAT file:

start C:\"Program Files"\"Mozilla Firefox"\firefox.exe exit

Launching programs in bat-files with parameters (keys)

You can not just run the program, but give it additional commands at startup. For example, command to run minimized:

start /min D:\FileZilla\FileZilla.exe exit

To command in this case means to specify the key. The key is specified through a slash after the main command (command /key). The main command in this case is start . True, the min key works only half the time, because it refers specifically to the start command, and not to the programs that this command starts.

In general, there are a lot of keys and sets of keys different programs may differ significantly. There are, however, a few common ones. For example, the help key (/? or /help ). To see how this key works, let's look at a practical example. Open the console (Click + R , type cmd , then Enter ) and type the following in the console:

start/?

The console will display a list of valid keys with comments for the start command.

Notice the /wait switch. In some cases, it is simply irreplaceable. For example, you decided to unpack the archive with the program using the BAT file and run this very program. There will be two commands in the batch file - for unpacking and for launching. Since the commands will be executed almost simultaneously when the BAT file is launched, the archive will not have time to unpack and there will be nothing to run. Therefore, there will be an error. In this case, the key will come to the rescue. /wait:

Thus, the system will first perform the first action, wait for its completion, and only then proceed to the second. If you need to wait for a specific period of time, then it's easier to use the console utility. AT right place BAT file, write the following command (number - number of seconds):

start Sleep.exe 15

You can do a lot with keys. It is possible to install applications. To do this, several keys are used, depending on the type of installer used to install the program on a computer:

/S /s /q /silent and a number of others

In some cases it is very convenient. Avast antivirus has the option quiet installation in the corporate edition. The free (home) version allegedly does not have a silent installation. However, if you are aware of how the InstallShield installer works, you will understand that this is a duck, since this installer itself supports the /S silent install switch. And that means all the products made on its basis - too. And Avast is no exception. Just create a BAT file with content in the Avast folder

start avast.exe /S exit

run it and the program is installed on your computer almost without your participation. In this way, you can write a whole list of programs for silent installation and save time, for example, on reinstalling the system. In the article you can get more detailed information by keys.

There are other options for managing programs using BAT files. You can start a program by telling it to open a file on startup. I use this method when developing websites. It is very convenient when all your tools open the necessary documents and folders by pressing just one button:

rem connection to ftp server start /min D:\FileZilla\FileZilla.exe "ftp://login:password@server" rem opening index.php in Firefox start C:\"program files"\"mozilla firefox"\firefox.exe "http://localhost/site_folder/index.php" rem opening start.html in a text editor start /min C:\"Program Files"\text_editor.exe "E:\server\site_folder\index.html" rem open folder with site files start /min E:\server\folder_with_site rem console exit exit

I note that all the above methods can be used in various combinations and combinations.

start /min /wait program.exe /m /S start C:\Directory\program2.exe "C:\Files\file.odt" exit

But it is important to remember: everything related to the execution of the program launched in the batch file is written with it on the same line.

start C:\"program files"\"mozilla firefox"\firefox.exe "http://localhost/site_folder/index.php"

As an epilogue, I will offer for review the converter of BAT files to applications of the .exe format - . A BAT file is not always aesthetically pleasing, but with the help of a converter you can pack a batch file into an exe file, decorating it with any icon of your choice.

I came across another BAT to EXE converter, you can consider it as an alternative previous program: Advanced Bat To Exe Converter

BAT files are Text Document, which has a special extension. Commands are written into it for their subsequent execution on the command line. By running the file, you activate the CMD program, it reads commands that are sequentially executed. In this way, you can make it easier to work with the command line by saving correct order. Of course, you can enter all the commands at once into the command line manually, however, if you need to repeat the same operations, it is much more convenient to do this by starting automatic reading of the list written in the form of a bat document. This option is also practical and convenient in that you can later change the order, supplement with new processes that you need, and also remove unnecessary ones.

BAT helps to automate processes: launching programs, archiving, backup. Therefore, it is useful to know how to create a bat file yourself, correct and supplement it.

Working with bat through Notepad

Creation algorithm

The process of working through Notepad is not at all complicated. If you have never done this before, then you can do it quite well by learning how to create a bat file and add commands to it. Let's start with creation. Here, proceed strictly according to the algorithm:

  1. Create a text document, you will initially get it with the txt extension. You can perform this operation in several ways, some depend on the level of Windows you have installed:
    • In the folder of your choice, click right click on the field, a menu will open, there we select "Create" - "Text Document";
    • Launch "Notepad" through "Start" by selecting "All Programs" - "Accessories" - "Notepad" (for Windows 7);
    • Late (after 7) Windows versions you can also open Notepad through "Start": go through "All applications", then "Standard" - "Windows" and there already select "Notepad" in the list;
    • Call the system command window using the Win + R combination, enter "notepad" in the line, click "Ok".
  2. Enter the command text. For a test, write down, for example, "START taskmgr.exe" - start task manager. "START" tells the command interpreter to start a program, followed by the program's executable.
  3. Specify the path where to save the created document.
  4. Specify in the save window:
    • in the line "Type" - "All files";
    • in the "Name" line, enter the name and extension of bat, adding it after the name, putting a dot between the name and extension, for example, "file.bat".
  5. Click on "Save".

A batch file with the .bat extension will appear in the folder you specified.

File launch

It is easier to open a bat file by double-clicking on it with the mouse. The second way is to run from command line: enter the address where the document with the .bat extension that you saved is located.

Editing

If you want to change the document - add commands to the bat-file, delete some of them, enter another program for subsequent work with it - this is not difficult to do. Open the document with a text editor. It is convenient to do this by right-clicking on the bat file, select "Edit" in the menu that opens. Notepad will start - there you can edit commands, change the content, make additions.

Application Dr.Batcher

For a beginner who is just starting to work with the command interpreter, who uses it quite rarely, Notepad is enough. If you want to reach a higher professional level, we recommend using the Dr.Batcher utility. In Dr.Batcher, you can number pages, there is support for bookmarks, a list system commands used in bat, the entered commands are highlighted.

Creating a Document

The algorithm is simple, does not require special skills and professionalism. By downloading the utility, you can quickly do everything you need.

Launch Dr. Batcher.

  1. Open new page: "File" - "New" or click on the blank sheet icon under the "File" menu.
  2. In the dialog box, click on "Empty Batch File".
  3. A program window is formed, where there will be functions for working with bat.
  4. Entering desired text, save.

Editing

If you need to change a bat file, Dr.Batcher does it in several ways:

  1. Right-click on the icon, select "Edit with Dr.Batcher" from the menu.
  2. Launch Batcher. Open the desired bat file by following the path:
  • "file";
  • "open";
  • specify the path to the document;
  • "Open".

Having followed one of the suggested paths, you will open the utility, there will be text in the window where you can make adjustments, change commands, supplement them, and delete unnecessary ones.

Bat file is handy when you need to automate tasks in Windows. Especially if it is necessary to perform actions several times (periodically or in a row) in a certain algorithm. This is usually required when creating a directory system, when bulk renaming files, and other simple and more complex operations. Having saved the order of actions, you can quickly and without unnecessary loss of time for the manual introduction of commands, you can start the process at any time, repeat it for different programs, adding only their name to the finished text. The recommendations on how to make a bat file above will help you not only create them yourself, but also edit them.

BAT file is batch file DOS, which executes commands through the command line Windows strings named "cmd.exe". BAT file extension is a regular text file, which was saved with a .bat extension. On MS Dos and Windows operating systems, given type file is recognized as batch file. Thus, the BAT file format applies a scripting programming language and works like a batch file.

The BAT format stores certain line commands. The use of a BAT file is common when launching various programs, and these batch files may also be necessary to control the utilities responsible for servicing the Windows system.

You can create BAT manually, in addition, the user has the ability to independently edit bat files in order to automate certain tasks along with the repeated execution of some operations. You can create a BAT file in a standard text editor, you just need to save the BAT extension.

If a user encounters the presence of such file extensions in his computer, downloaded from third-party resources, it is recommended that they be pre-scanned by an anti-virus complex, because an unknown Batch File may also contain a virus threat.

Even in our time, BAT format is considered to be very common among system administrators, because the .bat file allows you to perform tasks quite quickly and comfortably. OS Windows files BAT is used to perform operations related to moving, copying and deleting certain files, as well as directories.

BAT file opens special programs. To open this format, download one of the suggested programs.

How to open BAT file

The BAT extension is system file OS MS-Dos or Windows, which is used as an executable in Command Prompt(cmd.exe).

A BAT file is also commonly called a batch file, but this is by no means about archiving data, as is commonly believed in the standard view. The format supports a package (set) of executive commands that are executed interactively when loading a BAT file. In the absence of such a file, the user would have to manually enter them from the keyboard.

By its structure, the extension is a typical text file saved in the BAT format.

The file description is based on a scripting programming language that executes BAT file commands in MS-Dos and Windows OS.

The most common example this format on the MS-DOS platform, this is AUTOEXEC.BAT. It starts the system boot and initializes all its components. In order to initiate the execution of this format, you just need to enter its name on the command line. A set of standard BAT files required for the correct functioning of the OS is usually supplied with the operating system. However, the user can edit such a file manually using a regular test editor, adding or removing any executable programs. It is strictly not recommended to format the extension for an inexperienced user. This may jeopardize the functioning of the entire operating system.

Despite the apparent vulnerability of the format, it is very popular among system administrators. With its help, many tasks can be performed quite quickly and conveniently.

Also, you should not open unknown BAT files, as they may be carriers of malicious code.

Programs for opening BAT files

The BAT extension is unpretentious to the platform of a particular operating system and can equally successfully operate on Windows, Mac and Linux.

To open a BAT file in Windows OS, it is customary to use:

Based on Mac OS, the BAT format will be available using the same standard text editors or Parallels Desktop 7 for Mac.

In order to reproduce the extension in Linux, you need to use gedit .

If an error occurs when opening the BAT extension, the reasons may be as follows:

  • the file is damaged or infected;
  • the file is not associated with the OS registry (an incorrect application for playback was selected or a specific plug-in was not installed);
  • insufficient device or OS resources;
  • corrupted or outdated drivers.

Convert BAT to other formats

Using any test editor, such as Microsoft WordPad, Microsoft Notepad, BAT extension can be converted to . However, as practice shows, there is no need for this broadcast. The reverse data conversion (from TXT to BAT) is much more popular.

Be that as it may, the format conversion should be performed experienced user, which has the skills to format OS executive commands. Otherwise, attempts to convert the extension to another format may not only not give any practical results, but also damage original file, which will inevitably lead to the collapse of the entire operating system.

Why exactly BAT and what are its advantages?

BAT extension is a MS-Dos or Windows system file containing a set of executable commands. It is not popular among ordinary users, but is in demand only among system administrators and experienced programmers.

Batch or batch files are plain text files containing sets of interpreter commands and having a bat or cmd extension (cmd work only on NT family operating systems). You can edit such files using notepad or any other text editor.

Open notepad and type the following two lines:

@echo This batch file
@pause

This batch file
Press any key to continue...

After pressing any key, the window will close, because bat file is done.
Please note that the dog symbol before each command in the bat file indicates that the command itself does not need to be displayed on the screen, but only the result of its work needs to be displayed. To experiment, remove the dog character from the beginning of each line, save and run the resulting bat file.

Commands used in bat files

The list of commands that can be used in bat files can be viewed by typing in the command line (Start - Run - cmd for Windows NT family or Start - Run - command for 9x line) command

The result of help is a list of available commands with brief explanations for them. To get more detailed information on the command of interest, type help command_name at the command line. For example, to get detailed help on the AT command keys, run the following command:

As a result, a list of keys for running the AT command from a bat file will be displayed on the screen.
If the bat file is executed under Windows control(not in pure DOS), then you can run any applications or open files from it. For example, you need to automatically open the log file of the bat file after its work is completed. To do this, just include the following command in the bat file on the last line:

start filename.txt

The result of executing this command will be the opening of the file filename.txt, and the bat file itself will complete its work. This method is good if the log file is small, otherwise Notepad will refuse to open it, offering to use WordPad. But this problem is also solvable, which will be shown in further examples.

How to automate startup bat files

Very often it is necessary to automate the launch of bat files to perform their routine operations. To run bat files on a schedule, the Scheduler, which is included in the standard delivery of Windows, is most suitable. With this help, you can very flexibly configure the launch of a batch file on certain days or hours, at a certain interval. You can create multiple schedules, etc.

To run batch files locally, you can use third-party solutions, the benefit of paid and free alternatives the standard Scheduler a great many.

Batch files can also be used as login scripts in domains. With their use in this way, they will be executed every time the user enters the network, regardless of his desire. With their help, you can automate the collection of information about machines or software installed on users' computers, forcibly change windows settings, install invisibly to the user software and automate other tasks that would take a very long time to perform manually.

How to create a file with an arbitrary name from a bat file

A redirect character is used to create a file during batch file execution. It looks like this:
>
Those. to create a file, you need to redirect the stream from the screen to a file. You can do this with the following command:

@echo Start file>C:\1.txt

After executing this command, a text file with the line Start file will be created in the root of drive C.
When creating a file, you can use system variables or parts of them in its name. For example, you can create a report file about the operation of a bat file with a name equal to the date the bat file was launched. To do this, you can use the following lines in the bat file.

set datetemp=%date:~-10%
@echo .>%SYSTEMDRIVE%\%DATETEMP%.txt

These two lines work like this. First, we create a datetemp variable in memory, to which we assign 10 characters from right to left of the DATE system variable. Thus, now the temporary variable datetemp contains only the current date. In the next line, we redirect the output of the dot character to a file, whose name we take from the datetemp variable, and the txt extension is specified explicitly. The file will be created on the system drive of the computer where the bat file is running.

When an administrator collects information about computers on a network, it may be more convenient to append the computer name to the file name. This can be easily done with the following command:

@echo .>C:\FolderName\%COMPUTERNAME%.txt

This command, during the execution of the batch file, will create a text file on drive C with the name of the computer on which the batch file is being executed.
To create a file with a specific name, you can use any system variables, or create your own based on system variables and/or other data.

How to create folder from bat file

To create a folder, use the MKDIR command or its abbreviated counterpart MD. To create a folder from a bat file, you need to use the following command:

After executing such a command, the FolderName folder will be created in the folder from which the bat file is launched. To create a file in a location other than the bat file launch, for example, in the root of drive D, use an explicit indication of the location of the new folder. The command will look like this:

MD D:\FolderName

When creating folders, you can use system variables. For example, you can create a folder in the root of drive D with the name of the current user. To do this, you need the %USERNAME% variable, and the command will look like this:

MD D:\%USERNAME%

You can further complicate the command and create a folder with the name of the current user on the system drive of his computer. The command for this would look like this:

MD %SYSTEMDRIVE%\%USERNAME%

When creating folders or files, you can use any system variables or parts of them. The following example demonstrates how to create a folder on the system drive of the user's computer with a name equal to the current date.

set datetemp=%date:~-10%
MD %SYSTEMDRIVE%\%datetemp%

This construction works as follows.
The first command creates a datetemp variable in memory, which will be destroyed when the bat file ends. Until the bat file has finished its work, it is possible to operate with the value of this variable. The datetemp variable is assigned 10 characters from right to left of the DATE system variable, i.e. from current date. The DATE variable has the format Dn DD.MM.YYYY. The first characters from the left are the name of the day of the week, so we discard them and assign only the current date to the temporary variable datetemp.
This is not limited to the list of possibilities when creating folders. You can manipulate variables however you like, creating folders with unique, easy-to-read names. You can get a list of all variables with the SET command.

How to redirect the output of commands to a file

Often, when executing a complex bat file in automatic mode, it is difficult to check the results of its work for many reasons. Therefore, it is easier to write the results of the batch file commands to a text file (log file). and then analyze the correct operation of the bat file according to this log.
Redirecting the result of the bat file commands to a log file is quite simple. The following will show how this can be done.
Create a .bat file with the following content (copy these lines into Notepad and save the file with the .bat extension):

@echo off
echo Start %time%
echo Create test.txt
echo test>C:\test.txt
echo Copy Test.txt to Old_test.txt
copy C:\test.txt C:\Old_test.txt
echo Stop %time%

The first line disables the output of the commands themselves. Thus, only the results of their execution will be written to the log file.
The second line writes to the log file the start time of the batch file.
The third line writes to the log file an explanation that the following command will create the file test.txt
The command from the fourth line creates the file test.txt from the root of drive C. The file is created for example. This command writes the word test to the file C:\test.txt
The fifth line outputs to the log file an explanation that the following command is copying a file from one location to another.
The command on the sixth line copies the created file C:\test.txt to the file C:\Old_test.txt, i.e. a copy of the file is created under a new name.
The last, seventh line contains the command to display the time when the batch file ended. Combined with logging the start time of the batch file to the log file, these two time values ​​provide an estimate of the run time of the batch file.

Save this batch file with a name such as 1.bat
Let's assume that we would like to store a report on the operation of a batch file in a separate folder and write a report every day with a new file name so that we can access the logs for previous days on any day. Moreover, I would like to have the name of the log file in the form of the date of operation of the batch file. To implement all this, let's create a folder on drive C (for example) with the name LOG, i.e. the full path to it will look like C:\LOG. We will run the created batch file 1.bat with the following command:

1.bat>C:\LOG\%date~-10%.txt

If the batch file will be launched from the Scheduler, then you need to specify the full path to the bat file. Remember that if there are spaces in the path, then you must use either quotes or the 8.3 format. That is, if the path to the bat file is C:\Program Files\1.bat, for example, then one of the following lines must be specified in the Scheduler command line to run the bat file:

"C:\Program Files\1.bat">C:\LOG\%date~-10%.txt
C:\Progra~1\1.bat>C:\LOG\%date~-10%.txt

After running the 1.bat file in the C: \ LOG folder, a file will be created with a name equal to the date the bat file was launched, for example, 01/13/2004.txt This will be a report on the operation of the batch file 1.bat
Running the bat file, an example of which is shown in the first listing at the top of the page with the above command, will create a log file with the following content:

Start 19:03:27.20
Create test.txt
Copy Test.txt to Old_test.txt
Files copied: 1.
Stop 19:03:27.21

Thus, to redirect the results of the bat-file to the log-file, you need to use the redirection symbol> The syntax is as follows:

Path\FileName.bat>Path\LogFileName.txt

The log file extension can be anything. If desired, a report on the execution of a batch job can be issued even in the form html pages(corresponding tags can be output to a log file as comments were displayed in example 1.bat) and copy it to the corporate server.

How to automatically respond to a confirmation request

Some commands require confirmation of a potentially dangerous action when executed. For example, commands such as format or del will first ask for confirmation for further execution. If one of these commands is executed in a batch file, then the confirmation prompt will stop the batch file from executing and wait for the user to select one of the options. Moreover, if the result of batch file execution is redirected to a log file, then the user will not see a confirmation prompt and the batch file will look frozen.

To fix such annoyances, you can redirect the desired response to the command. Those. execute reverse action to redirect the output of the command to a file.
Let's look at an example of how a request to confirm a potentially dangerous action looks like. Let's create on drive C, for example, a Folder folder. Let's create in it or copy any two files into it. Next, open a command prompt and run the following command:

This command should remove all files from the specified folder. But beforehand, a request will be issued to confirm the following content:

C:\Folder\*, Continue ?

The execution of the command will stop until either the Y key or the N key is pressed. When a batch file is executed in automatic mode, its execution will stop.
To avoid this, we use a redirect. Redirection is carried out using the symbol
The vertical bar indicates that instead of displaying the character on the screen, it must be “given away” to the command following the character. Let's test the redirection. Run the following command on the command line:

echo Y|del C:\Folder

The screen will show a confirmation request to delete all files in the Folder, but with a positive response (Y). All files in the Folder will be deleted.
Be careful with this command.

How to disable the output of commands on the screen when executing a batch file

When a batch file is executed, the commands themselves are displayed on the screen, in addition to the results of the command. You can use the @ symbol to disable command output.
In order not to display a single command, you can put the @ sign at the beginning of this command.

This command will display the command echo Testing, and on the next line - the result of its work, the word Testing.

This command will display only the result of the command, i.e. the word testing. The command itself will not be displayed.
If you do not need to display commands on the screen during the execution of the entire file, then it is easier to write the following command in the first line in the batch file:

This command will disable the output of commands to the screen for the duration of the entire batch file. To prevent the command itself from being displayed, it begins with the @ symbol.

How to run another from one bat file

Sometimes, when executing a batch file, it becomes necessary to run another batch file. Moreover, in some cases, the execution of the main batch file must be suspended while the auxiliary file is being executed, and in others, the auxiliary file must run in parallel with the main one.
For example, let's create two bat files. One named 1.bat and containing only one command

The second one is named 2.bat and also contains one command

Now let's run the file 1.bat. A window will open in which you will be prompted to press any key to continue, after pressing which the window will close. Thus, calling from one batch file to another using the call command stops the execution of the batch file until the execution of the batch file called by the call command completes.

Otherwise, you need to run either an application or another batch file from a bat file without interrupting the execution of the main batch file. This often needs to be done, for example, by forcibly opening the operation log of a batch file scheduled for the night, so that in the morning the user can check the correctness of its execution. To do this, use the start command Let's fix the line in file 1.bat with

and run the file 1.bat Now a window has opened in which you need to press any button to continue, and the window of the main batch file (1.bat) has closed.
Thus, to call from one batch file to another, without stopping the work of the first batch file, you need to use the start command.
The above start and call commands can be used not only to launch other batch files, but also to launch any application or open files.
For example, start log.txt in the body of a batch file will open log.txt in Notepad without stopping the batch file.

How to send message from bat file

When a batch file is being executed on one of the machines on the network, it is convenient to inform the administrator of the completion of its execution by sending a message to the administrator's machine. You can do this by including the command in the batch file

net send name Message text

Where name is the name of the machine or user to which the message is addressed, and Message text is the text of the message. After executing this command, a message will be sent to the user name.
Please note that when using Cyrillic in the text of the message, the text must be typed in MS-DOS encoding (866 code page). Otherwise, the message will come in the form of unreadable characters. You can type text in DOS encoding using any text editor that supports this encoding. It can be, for example, FAR. Open the batch file in FAR for editing (F4) and press the F8 button. The top line of the editor should be DOS encoding, and at the bottom, at the hint about keyboard shortcuts, the F8 key should have the inscription Win, indicating that the current encoding is DOS and to switch to the Win encoding, press F8.

How to automate file deletion by type

To clear the disk of temporary files, you can use the command

del /f /s /q C:\*.tmp

Where
/f - deletes all files, even if they have the read-only attribute set
/s - removes files from all subdirectories
/q - disables the prompt for confirmation of file deletion
C: is the drive where the files will be found and deleted. You can specify not the entire drive, but a folder, for example, C:\WinNT
*.tmp - type of files to be deleted

Be careful with the /q switch and the types of files you delete. The command deletes without asking for permission, and if the wrong file type is specified, it can delete unnecessary ones.

How to change the IP address of a computer from a batch file

The IP address can be changed using the netsh command.
To correctly change the IP address, you first need to find out the current configuration. You can do this on the command line with the command

netsh interface ip show address

The result of this command is to display the current configuration of the network interface. We are interested in the name of the interface. Let's assume it's called FASTNET.
Suppose that you need to change the IP address to 192.168.1.42, the addressing in the network is static, without using DHCP, gateway 192.168.1.1, mask 255.255.255.0 In this case, the command to be executed from the batch file will look like this:

netsh interface ip set address name="FASTNET" static 192.168.1.42 255.255.255.0 192.169.1.1 1

After executing this command, the FASTNET interface will change its IP address to 192.168.1.42.
The netsh command provides extensive control over network settings from the command line. To get to know others functionality get help with netsh /?

How to get computer name from bat file

To find out the computer name when executing a bat file (to use this value later), use the command

This command returns the name of the computer it is running on.

How to rename files by mask from a batch file

Sometimes it becomes necessary to rename all files in a folder according to a template from a batch file. You can do this with the following command in a bat file:

for /f "tokens=*" %%a in ("dir /b PATH\*.*") do ren PATH\%%a Prefix%%a

In this line, you need to replace PATH\ with the path to the files that will be renamed, and Prefix with those characters that will be added to the file name when renaming.
Don't put the batch file in the folder where the rename happens, otherwise it will be renamed too. If the folder where the files are being renamed has subfolders, then a prefix will also be added to the name of the subfolder, i.e. subfolders will be renamed like files.
If you specify a specific mask for file types that are subject to renaming, for example, *.txt, and not *.* as in the example, then only files of the specified types will be renamed. Other files and folders will not be renamed.

Second option:
set thePATH=C:\test
for %%I in (*.txt) do ren "%thePATH%\%%~nxI" "%%~nI.dat"
How to use the percentage symbol in a batch file

To use the percentage symbol (%) in a batch file, you must write it twice. For example
echo 50%%
This command in the bat file will display 50%. If you use the echo 50% command, then only the number 50 will be displayed on the screen.
Keep this in mind when using the % symbol in batch files.

How to export the registry from a batch file

regedit.exe -ea C:\environment.reg "HKEY_CURRENT_USER\Environment"

This command, when executed in a batch file, will unload the HKEY_CURRENT_USER\Environment branch into the C:\environment.reg file. When you need to restore the settings in HKEY_CURRENT_USER\Environment, it will be enough to run the environment.reg file. This command can be used to make a daily backup of software and system settings that are stored in the registry.
Do not forget that if there is a space in the path where the output file should be saved or in the name of the registry branch, then they must be enclosed in quotes.

How to Import Registry Variables from a Batch File

If there is a need to import previously saved or new variable values ​​from a batch file into the registry, this can be done using the command

regedit.exe -s C:\environment.reg

This command imports data from the environment.reg file into the registry without prompting for confirmation by using the -s switch.

How to bypass date check from bat file

Some software checks the current system date on startup. If the date is greater than the one set by the developer, then the program does not start. For example, the developer believes that the version of the program can work for a month, and then the user will have to install updated version programs. On the one hand, this is a concern for the user, who will have at his disposal a fresh version of the program with the bugs fixed, in relation to previous versions. On the other hand, the manufacturer forces the user to download new version even if the user is completely satisfied with the version of the program that he has installed. This problem can be easily solved with the following batch file, which will run the program, wait for it to complete, and return the date to the date before the program was run.

set tempdate=%date:~-10%
date 01-01-04
notepad.exe
date %tempdate%

AT this example the current system date is first stored in a variable, then (on the second line) the system date is set to January 1, 2004, and then a program is called that checks the system date. In this example, it's Notepad. As long as Notepad is open, the batch file is pending, not ending, and not resetting the system date back. Once Notepad is closed, the batch file will continue executing and set the system date to the value stored in the tempdate variable, i.e. to the one that was before running the batch file.

Do not forget that if the path to the file with which the program is launched contains spaces, then it (the path) must be enclosed in quotes. If the path contains Cyrillic, then when writing a batch file, you must use a text editor that supports DOS encoding (for example, FAR). Otherwise, when you run the batch file, you will get a message stating "The specified file is not an internal or external command...".

If the program checks the current system date only when it is started and does not do this again during operation, then the batch file can be modified by adding the start statement before the name of the program executable file, i.e. our example will look like this:

set tempdate=%date:~-10%
date 01-01-04
start notepad.exe
date %tempdate%

In this case, the batch file will change the system date, launch the program and, without waiting for its completion, return the date to the one that was before the program was launched.

How to wait for a specific file in a bat file

Sometimes it is necessary to perform some action when a certain file appears in a folder. To check the existence of a file in a folder, you can use the following batch file

:test
if exist c:\1.txt goto go
sleep 10
goto test
:go
notepad

Such a batch file will check with an interval of 10 seconds for the presence of the 1.txt file in the root of the C drive, and when the 1.txt file appears, the action indicated after the go label will be performed, i.e. in this example, Notepad will be launched.
The sleep utility is freely distributed as part of the Resource Kit. You can download it here.
If the 1.txt file is large and copied from somewhere, it may happen that the batch file checks for its existence while the file has not yet been copied or is being occupied by another application. In this case, an attempt to perform some actions with the 1.txt file will result in an error. To prevent this from happening, the batch file can be modified as follows

:test
if exist c:\1.txt goto go
sleep 10
goto test
:go
rename c:\1.txt 1.txt
if not errorlevel 0 goto go
del c:\1.txt

When the 1.txt file is not completely copied to drive C, or is occupied by another application, an attempt to rename it will cause an error and the cycle will repeat until the file is completely copied or freed. After the rename c:\1.txt 1.txt command is executed without error (that is, the file is free), then you can perform any actions with it. In the last example, this is its removal.

How to add comments to bat file

When writing a large batch file, it is very useful to add comments to its main blocks. This will make it easy to figure out what these blocks do over time.