How to open an archive with a bat file. How to open BAT? Creating a batch file using the Dr. Batcher"

With this article we continue our series of materials about the command line. In this article, you will learn how to create a BAT file and how to use it.

A BAT file (also known as a batch file or batch file) is a text document with the extension . BAT, which contains commands that need to be executed using the command line. When such a file is launched, the CMD program is launched, which reads commands from this file and executes them sequentially.

Using BAT files you can automate many processes. For example, using a BAT file, you can launch programs, backup files, archive data, and much more.

Create a BAT file

Creating a BAT file is very simple. To do this, just launch any simple text editor, such as Notepad, enter commands and save with the BAT extension.

All after this, a BAT file will appear in the folder that we specified when saving the file. In order to launch it, just double-click on it with the mouse.

You can also run the BAT file from the command line. To do this, just enter his address.

If you want to continue editing the BAT file, for example, adding new commands to it, you need to open the BAT file again using a text editor. This can be conveniently done using the context menu.

Right-click on the BAT file and select "Edit". After this, the Notepad editor will launch, in which you can edit your BAT file.

The use of a graphical interface in operating systems today seems to be something taken for granted and completely natural, but this was not always the case. The first operating system, MS DOS, developed by Microsoft, did not have a GUI, and was controlled by entering text commands. Almost 40 years have passed since then, but the command line scripting language is still popular, and not only among developers.

The command line is not so convenient, but with its help you can perform operations that are not possible from the GUI. On the other hand, launching the console every time, entering commands into it one after another - all this greatly slows down the work. However, you can significantly simplify the task by creating a bat file or simply a batch file - a text file with the BAT extension containing a list of instructions processed by the CMD command interpreter. Such files are used to automate various tasks, for example, to delete temporary files on a schedule or launch programs.

How to create a file with a BAT extension

So, how to create a bat file in Windows 7/10? Very simple. To do this, you will need any text editor and knowledge of the basics of the command line. You can use Notepad, or even better, Notepad++, since the latter has syntax highlighting. Create a new file in the editor, select “Save As” from the “File” menu, give the future script a name, and select “Batch file (*bat; *cmd; *nt)” in the “File type” drop-down list.

If you want to use Notepad to create a bat file, you need to assign the extension manually, and select “All files” in the “File type” list.

As you can see, creating a file with the bat extension is not difficult; however, there are some subtleties here. Line breaks cannot be used in command files; the encoding of the bat file must be set to UTF-8; if the body of the script uses Cyrillic, the encoding must be changed by inserting the chcp 1251 command in the appropriate place.

Instead of the BAT extension, you can use CMD, the result of executing the script will be exactly the same.

Basic commands, syntax and examples of using batch files

You know how to make a bat file, now it’s time for the most interesting part, namely the syntax of the CMD interpreter language. It is clear that an empty batch file will not work, it will not even launch when you double click on it. For the script to work, at least one command must be written in it. For a visual example, let's see how to write a bat file to launch programs. Let's say that when you start working, you launch three programs every time - Chrome, Firefox and VLC. Let's simplify the task by creating a script that will launch these programs itself at intervals of five seconds.

Open an empty batch file and paste the following commands into it:

Start "" "C:/Program Files/Google/Chrome/Application/chrome.exe" timeout /t 05 start "" "C:/Program Files/Mozilla Firefox/firefox.exe" timeout /t 05 start "" "C :/Program Files/VideoLAN/VLC/vlc.exe"

Team start launches the executable file of the desired program, and the command timeout/t sets the interval between starts. Pay attention to the placement of the quotes - they contain paths that contain spaces. Also, if there are Cyrillic characters in the path, you should insert a command that changes the encoding at the beginning of the script chcp 1251, otherwise the interpreter will not be able to read the path correctly.

When you run the script, four console windows will be opened sequentially; this is normal; after executing the commands, they will all automatically close, however, you can make sure that only the first window opens. To do this, the application launch code should be changed as follows:

Start /b "" "path"

It may also happen that at some point it will be necessary to pause the execution of the script so that the user can decide whether to execute all other commands or not. There is a command for this pause. Try replacing timeout with it and see what happens.

Start /b "" "path" pause

Let's look at another example of commands for a bat file. Let's write a script that will turn off the computer in one case, and restart it in another. For these purposes we will use the command shutdown with parameters /s, /r And /t. If you wish, you can add a request to perform an action to your body file, like this:

@echo off chcp 1251 echo "Are you sure you want to turn off your computer?" pause shutdown /s /t 0

Let's explain. The first command hides the text of the commands themselves, the second - sets the Cyrillic encoding, the third - displays a message for the user, the fourth - sets a pause, the fifth - turns off, and with the key /r instead of /s reboots your computer without the traditional one-minute delay. If you don’t want to stand on ceremony with requests and pauses, you can leave only the fifth command.

If instead of Russian text when executing the command you see kryakozyabra, try converting the script file to ANSI.

What else can you do with scripts? A lot of things, for example, deleting, copying or moving files. Let's say you have a certain data folder in the root of drive D, the contents of which need to be cleared in one fell swoop. Open the batch file and paste the following command into it:

Del /A /F /Q "D:/data"

Or you can do this:

Forfiles /p "D:/data" /s /m *.* /c "cmd /c Del @path"

Unlike the first, the second command deletes files recursively, that is, all files in the data folder will be deleted plus those located in subdirectories.

Here's another useful example. Let's write a script that will create a backup copy of the contents of one folder and save the data to another. The command is responsible for copying robocopy:

Robocopy C:/data D:/backup /e pause

By running such a batch file for execution, you will copy the entire contents of the data folder to the backup folder, including subdirectories, empty and with files. By the way, the robocopy command has many parameters that allow you to configure copy parameters very flexibly.

Run bat files as administrator and on a schedule, hidden bat launch

Now you know how to create batch files and have some general understanding of the CMD interpreter language. These were the basics, now it's time to get acquainted with some useful features of working with bat files. It is known that programs require administrator rights to perform some actions. Batniks may also need them. The most obvious way to run a script as an administrator is to right-click on it and select the appropriate option from the context menu.

In addition, you can make sure that a specific batch file will always be launched with elevated privileges. To do this, you need to create a regular shortcut to such a script, open its properties, click the “Advanced” button and check the “Run as administrator” checkbox in the window that opens. This method is also good because it allows you to select any icon for the shortcut, while a file with a BAT or CMD extension will always have a nondescript appearance.

Scripts, like all applications, can be launched on a schedule. Team timeout/t is not entirely appropriate here; for delayed launch, it is best to use the built-in Windows Task Scheduler. Everything is simple here. Open with the command taskschd.msc Scheduler, decide on the trigger, select the action “Run program” and specify the path to the bat file. That's all, the script will be launched at the scheduled time.

And finally, one more interesting point. When you run a bat file, a command line window appears on the screen, even if only for a fraction of a second. Is it possible to make the script run in hidden mode? It is possible, and in several ways. The simplest one is as follows. Create a shortcut for the bat file, open its properties and select “Collapsed to icon” from the “Window” menu. After this, the only visible sign of the script running will be the appearance of the CMD icon on the taskbar, but no windows will open.

If you want to completely hide the execution of the script, you can use a “crutch” - the VBS script, which will launch your batch file in hidden mode. The script text is below, save it to a file hidden.vbs, having previously replaced the path in the second line of code D:/script.bat path to your body file.

Set WshShell = CreateObject("WScript.Shell") WshShell.Run chr(34) & "D:\script.bat" & Chr(34), 0 Set WshShell = Nothing

There are also other options, for example, using the utility Hidden Start, which allows you to run executable and batch files in hidden mode, including without an invitation.

And that's all for now. Information regarding creating BAT scripts can be easily found on the Internet. It's also a good idea to check out William Stanek's Microsoft Windows Command Line tutorial. Despite the fact that more than ten years have passed since the publication of the book, the information contained in it is still relevant.

A bat file is a text file in Windows operating systems that contains a sequence of system commands for execution by the command interpreter “cmd.exe”. In general, it is a script file. After launching the bat file, the command interpreter reads its contents line by line and sequentially executes the received commands. Today we’ll look at how you can create a bat file, and also get acquainted with programs for creating and editing these files.

The purpose of bat files

Files with the bat extension are designed to automate many routine tasks in the Windows operating system. They will be very useful in cases where it is necessary to perform some operation or sequence of operations a large number of times in a row or periodically repeat a certain algorithm. First of all, this concerns working with the file system (creating a large number of directories, especially mutually nested ones, mass renaming of files). , You can find out by clicking on the above link.

Creating a bat file

Any user can create a text command file - there is absolutely nothing complicated here. To do this, you only need a text editor (the functionality of a standard notepad will be quite sufficient).

First method

  1. Create a new text document in any directory.

To perform this action, call up the context menu of the directory space free of folders and files and select “Create”. In the drop-down list, click on the “Text file” item.

  1. Enter the name of the created document and click “Enter”.
  2. Open the created file by double clicking the mouse.

  1. Let's enter a few simple commands into it to check its functionality:

@ echo Hello, im bat!

@echo – command to display text on the screen;

Hello, i’m bat! - the text itself;

pause – wait for user actions before closing the window. After executing the “@echo” command, the command interpreter will not close automatically, allowing the user to get acquainted with the results of its execution (we will see the entered text on the screen).

  1. Call up the “File” item in the main notepad menu and click on “Save as...”.

  1. In the dialog that appears, select the file type “All files”.

  1. Next, we add the following text “.bat” to the name of our document, which will ultimately give “file.bat”.
  2. Click on the “Save” button.

As a result, a document called “file.bat” will appear in the Explorer window, to launch which you need to double-click on it with the left mouse button or select it and press “Enter”.

Editing a batch file is done by selecting the “Edit” command from its context menu.

Second method

The second method of creating bat files is almost similar to the first, but we will not ignore it.

  1. We launch Notepad using any known method.
  • Through the shortcut in the Start menu - go to “Start - All Programs - Accessories - Notepad” in Windows 7 or “Start - All Applications - Accessories - Windows - Notepad” in later editions of Windows.
  • In the search bar “Start” / “Windows”, enter notepad and launch the program by clicking on its shortcut in the list of results.
  • Call up the window for executing system commands using the key combination “Win ​​+ R”, enter “notepad” and click “OK”.
  1. Let's move on to point No. 4 of the previous method.

Editing bat files using Windows

  1. Right-click on the bat file, calling up its context menu.

  1. From the menu that appears, select “Edit” or “Edit”.
  2. The contents of the bat file will appear in the window that opens with a standard text editor, usually Notepad.
  3. We make the necessary adjustments and save the result.

Dr.Batcher – a program for creating bat files and editing them

Even though the use of bat files is not so popular among users today, it is still more effective to resort to their help to solve many problems on a computer. If a beginner who occasionally uses a command interpreter is satisfied with the functionality of Notepad, then for a system administrator and a Vareznik who creates builds of Windows or automatically installed software for it, a more advanced and convenient tool for working with documents in bat format is needed. It is the Dr.Batcher utility. The interface and functionality of the program is not much different from notepads such as Notepad++. The main menu contains buttons for frequently used commands. Dr.Batcher numbers pages, supports bookmarks, contains a list of system commands for bat files and highlights the syntax of entered commands.

Creating a bat file in Dr.Batcher

  1. Let's launch the application.
  2. Call the “File” item in its main menu and click “New”. The same is done by clicking on the blank sheet icon located under the “File” menu.
  3. In the dialog box, select “Empty Batch File”.

A program window will appear on the screen with all the functions necessary for working with bat files.

Editing bat files in Dr.Batcher

There are several ways to open a bat format document for editing in Dr.Batcher, which will be discussed further.

Editing via the context menu of a bat file

  1. Call the context menu of the bat format document.
  2. In the drop-down list, select “Edit with Dr.Batcher”.

Then a program window will open with the contents of our text batch file, where you can make adjustments to its contents.

Editing bat files using Dr.Batcher

  1. Launch the Batcher application.
  2. Call the “File” item in its main menu.
  3. Select “Open” from the drop-down menu.

The same thing is done by clicking on the folder icon located under the main menu.

  1. In the file opening dialog box, specify the path to the required bat format document and click “Open”.

Our bat file will open in the Dr.Batcher window and you can make adjustments to it.

Batch or batch files are ordinary text files containing sets of interpreter commands and having the bat or cmd extension (cmd work only in 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 completed.
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 operation should 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 entering the command in the command line (Start - Run - cmd for Windows NT family or Start - Run - command for the 9x line)

The result of help is a list of available commands with brief explanations for them. To get more detailed information on the command you are interested in, enter help command_name at the command line. For example, to get detailed help on the AT command switches, run the following command:

As a result, a list of keys for running the AT command from the bat file will be displayed on the screen.
If the bat file is executed under Windows (not 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 when it completes its work. To do this, just include the following command in the bat file as the last line:

start filename.txt

The result of executing this command will be the opening of the file file_name.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, suggesting you use WordPad. But this problem can also be solved, as will be shown in further examples.

How to automate the launch of bat files

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

To launch batch files locally, you can use solutions from third parties; fortunately, there are a great many paid and free alternatives to the standard Scheduler.

Batch files can also be used as login scripts in domains. When used in this way, they will be executed every time the user logs into the network, regardless of his desire. With their help, you can automate the collection of information about machines or software installed on user computers, forcefully change Windows settings, install software without the user noticing, and automate the solution of other tasks that would take a lot of time to perform manually.

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

A redirection symbol is used to create a file while a batch file is running. It looks like this:
>
Those. to create a file you need to redirect the stream from the screen to the file. This can be done using 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 from the DATE system variable. Thus, now the temporary variable datetemp contains only the current date. With the next line, we redirect the output of the dot symbol to a file, the name of which is taken from the datetemp variable, and the txt extension is specified explicitly. The file will be created on the system disk of the computer where the bat file is executed.

When an administrator collects information about computers on the network, it will be more convenient to add the computer name to the file name. This can be easily done using the following command:

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

This command, while executing a batch file, will create a text file on drive C with the name of the computer on which the batch file is running.
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 a folder from a bat file

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

After executing this command, a FolderName folder will be created in the folder from which the bat file was launched. To create a file in a location other than where you started the bat file, 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 will 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 thereof. The following example demonstrates the creation of 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 design works as follows.
The first command creates a datetemp variable in memory, which will be destroyed when the bat file finishes running. 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 the current date. The DATE variable has the format Day DD.MM.YYYY. The first characters on 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 does not limit the list of possibilities when creating folders. You can manipulate variables the way you want, creating folders with unique, easy-to-read names. You can get a list of all variables using the SET command.

How to redirect the result of command execution to a file

Often, when executing a complex bat file in automatic mode, checking the results of its work can be difficult for many reasons. Therefore, it is easier to write the results of batch file commands to a text file (log file). and then analyze the correct operation of the bat file using this log.
Redirecting the result of 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 a test.txt file
The command from the fourth line creates a 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 prints to the log file an explanation that the following command copies a file from one location to another.
The command in 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 a command to display the completion time of the batch file. Together with the recording of the start time of the batch file in the log file, these two time values ​​make it possible to estimate the running time of the batch file.

Save this batch file with a name like 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) named 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, you must use either quotes or 8.3 format. That is, if the path to the bat file is C:\Program Files\1.bat, for example, then in the Scheduler command line to run the bat file you need to specify one of the following lines:

"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, a file will be created in the C:\LOG folder 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 1.bat batch file
Running the bat file, an example of which is shown in the first listing at the top of the page, using the above command, will lead to the creation of 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 a bat file to a 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 even be formatted as an html page (the corresponding tags can be output to a log file in the same way as comments were output in example 1.bat) and copied to a 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 before 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 it will wait for the user to select one of the given options. Moreover, if the result of executing a batch file is redirected to a log file, then the user will not see a confirmation request and the batch file will appear frozen.

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

This command should remove all files from the specified folder. But first you will be prompted to confirm the following content:

C:\Folder\*, Continue ?

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

echo Y|del C:\Folder

The screen will display a request to confirm the deletion of all files in the Folder folder, but with a positive answer (Y). All files in the Folder folder will be deleted.
Be careful with this command.

How to disable commands being displayed when executing a batch file

When executing a batch file, in addition to the results of the command, the commands themselves are also displayed. You can use the @ symbol to suppress command output.
To avoid printing one command on the screen, you can put an @ sign at the beginning of the command.

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

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

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

How to run another from one bat file

Sometimes, while 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 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 just one command

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

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

In another case, you need to launch 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 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 correct the line in file 1.bat to

and run the 1.bat file. 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 another from one batch file, without stopping the first batch file, you need to use the start command.
The start and call commands discussed above can be used not only to launch other batch files, but also to launch any applications or open files.
For example, the start log.txt command in the body of a batch file will open the log.txt file in Notepad without stopping the batch file.

How to send a message from a bat file

When a batch file is being executed on one of the machines on the network, it is convenient to inform the administrator that its execution has finished using a message sent 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 whom the message is addressed, and Message text is the text of the message. After running this command, a message will be sent to user name.
Please note that when using Cyrillic in the text of a message, the text must be typed in MS-DOS encoding (866 code page). Otherwise, the message will arrive in the form of unreadable characters. You can type text in DOS encoding using any text editor that supports this encoding. This could be, for example, FAR. Open a batch file for editing in FAR (F4) and press the F8 button. The top line of the editor should indicate the DOS encoding, and at the bottom, at the tooltip about the shortcut keys, the F8 key should have the inscription Win, indicating that the current encoding is DOS and to switch to the Win encoding you need to press F8.

How to automate file deletion by type

To clear your 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 - deletes files from all subdirectories
/q - disables the request to confirm file deletion
C: is the drive on which files will be found and deleted. You can specify not the entire disk, but a folder, for example, C:\WinNT
*.tmp - type of files that will be deleted

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

How to change a computer's IP address 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. This can be done on the command line using 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 say it's called FASTNET.
Let's assume that you need to change the IP address to 192.168.1.42, the network addressing is static, without using DHCP, the gateway is 192.168.1.1, the mask is 255.255.255.0. In this case, the command that must 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's IP address will change to 192.168.1.42.
The netsh command provides extensive capabilities for managing network settings from the command line. For other functionality, see help using netsh /?

How to find out the computer name from a bat file

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

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

How to rename files by mask from a batch file

Sometimes it becomes necessary to rename all the files in a folder using a template from a batch file. This can be done using the following command in the 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 is happening, otherwise it will be renamed too. If there are subfolders in the folder where the files are renamed, 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 the 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 percent 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 command echo 50%, then only the number 50 will be displayed on the screen.
Take this feature into account when using the % symbol when writing batch files.

How to export the registry from a batch file

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

This command, when executing a batch file, will dump the HKEY_CURRENT_USER\Environment branch into the file C:\environment.reg When you need to restore the parameter values ​​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.
Don't forget that if there is a space in the path where the output file should be saved or in the name of the registry hive, they must be enclosed in quotes.

How to import registry variable values ​​from a batch file

If there is a need to import previously saved or new variable values ​​into the registry from a batch file, 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 checking from a bat file

Some software checks the current system date upon startup. If the date is greater than what was set by the developer, then the program does not start. For example, a developer believes that a version of a program can work for a month, and then the user will have to install an updated version of the program. On the one hand, this is a concern for the user, who will have at his disposal the latest version of the program with the shortcomings eliminated in relation to previous versions. On the other hand, the manufacturer forces the user to download a 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 using the following batch file, which will run the program, wait for it to complete and return the date to the one that was before the program was launched.

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

In this example, the current system date is first stored in a variable, then (in 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 is Notepad. As long as Notepad is open, the batch file waits without completing or setting 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 that runs the program 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, a message will be displayed stating that “the specified file is not an internal or external command...”.

If a program checks the current system date only when it starts and does not do this again during operation, then the batch file can be modified by adding a start statement before the name of the program's 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 it to complete, return the date to the one that was before the program was launched.

How to wait for a specific file to appear in a bat file

Sometimes it is necessary to perform some action when a certain file appears in a folder. To organize a check for the appearance 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 at 10-second intervals for the presence of file 1.txt in the root of the C drive and when file 1.txt appears, the action specified after the go label will be performed, i.e. this example will launch Notepad.
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 is being copied from somewhere, it may happen that the batch file will check for its presence while the file has not yet been copied or is busy with another application. In this case, trying 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 has not been completely copied to drive C, or is occupied by another application, an attempt to rename it will cause an error and the cycle will be repeated until the file is copied completely or is freed. After the rename c:\1.txt 1.txt command is executed without an error (i.e. the file is free), you can perform any actions with it. In the last example it is removing it.

How to add comments to a 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 understand what these blocks do over time.

Bat files are text documents that contain a list of sequentially executed commands (instructions). They can be so simple that the average user can use them. In today’s topic, we’ll look at what a batch file is, how to create a bat file in Windows 7, 8, and also consider an example batch file.

System administrators (advanced users) are well aware of the usefulness of bat files, but ordinary users are not familiar with them, they are intimidated by the structure of the code and running this script. This is unfortunate because many people shy away from using a powerful tool to perform routine and repetitive tasks. Bat files can be complex, requiring professional programming skills, or simple.

A body file contains one or more lines of commands that are executed sequentially. Bat files have an extension (bat or cmd) and are executed through the command interpreter interface. All commands can be entered into cmd, but each time writing the code is a chore, so it’s easier to create a bat file.

Creating a bat file

To create a batch file, you will need a text editor (notepad, notepad++) and . See the approximate algorithm below.

1. Create a text document with the extension txt. Right-click on an empty area of ​​File Explorer or other location. Point to "create", then select "text document" from the submenu. Give it a name, for example, batnik.

2. Download notepad++ then install it. Open batnik.txt in notepad++, select “encodings”, hover over “encodings”, “Cyrillic”, click on OEM 866. Do this step if you are using the output of Russian words (explanations) in the script, otherwise you will see crappy language ( not valid characters). Next, write down the commands. For example, copy this text:

@echo off
echo Hello – bat file test
pause
dir c:\windows
pause

4. Double-click to run batnik.bat to execute a sequence of commands. After the bat file is executed, it will automatically close.

You can also launch notepad, press Win + R, enter notepad in the line and click Enter. After entering the code, click “file” and then “save as”. In the window, specify the save path, file name with the bat extension and click “save”. Do the same with notepad++ to change the encoding (step 2) if you use Russian characters.

Example bat file

Now let's look at creating a bat file in Windows 7, 8, which provides information about the network, checks the Internet connection and clearly shows the action of batch files. Create a bat file and write these lines.

ECHO OFF
:: cleaner.
ECHO Connection information.
:: displays the text on the screen that comes after the service word ECHO.
IPCONFIG /ALL
:: Prints information about all network connections.
PAUSE
:: Pauses the script so you can view network information.
PING yandex.ru
:: Ping, checking the presence (absence) of an Internet connection
ECHO Operation completed. Press enter!
:: Displays words after ECHO that indicate PING has been completed.
PAUSE
:: This last command is written in order to see connection information. If it were not there, then the batch script would stop and the window would close.

In fact, there are a huge number of examples of baht files for training and downloading on the Internet. You just need to study the syntax and commands to create your own batch file to suit your needs. I hope you understand why batch files are needed, and how to create a bat file in Windows 7.8.