Useful Tips

How to create a bat file in Windows


Bat is the file extension (bat). Surely many users know that each file has its own extension. This is the last line after the period in the title. It is this “tail” that is needed to define the system as music, video, image or any other. You can even experiment. Take any picture and rename along with the extension. Windows will immediately notify that you are changing the extension, and the file may not be detected by the system. Accept the request and see that your picture is no longer displayed as an image. If you rename as before, everything will be restored again. If you don’t see extensions, you need to activate this function in Explorer, in the “view” tab.

For Windows, bat is a file with instructions for certain actions that are in the file itself. An empty bat file will start, but no action will be taken. A batch file is a set of instructions for the Windows console.

How to create a bat file to run a program

To create a batch file, you need to start the text editor, then save the file with the final name bat. If your editor does not have a save function with such an extension, then you need to register it yourself. When you run such a file, the console will open and if there are no commands, it will close immediately.

Now let's give a command to run the program. To do this, open our created batch file in notepad or another editor and register a command.

Using the example, we will launch the browser. You must first enter the Start command and write Opera after a space.

It should look like this:

Start opera

This will work for programs known to the system, but if you start software that is not known to the system, you will need to register the entire path to the program. For example, if you have ftp installed in the Program Files folder, then in order to start it, you need to enter the command

start filezilla

And if the ftp client is installed in a different folder, you will need to register the entire path.

The scope of body shirts is quite wide. A variety of commands can be written into the configuration. You can find them on the Internet if you are interested in more detailed information on the teams write in the comments, I will try to answer you. VBS works the same way. This is the same thing, only the execution of commands does not go through the console, and the commands are slightly different.

This technology will help in performing various tasks, and in automatic mode. Some users even write viruses this way. In order to not be able to change the file configuration using the text editor, you must convert the bat file to exe. This will help to make various auxiliary software that you can find on the network. For example, the Bat to exe converter program will help to convert bat to exe.

The bat function is very useful and sometimes necessary. With it, you can write entire programs with a narrow spectrum of action. The scope depends only on the knowledge of the teams and the ability to combine them. Therefore, use and expand your skills and ability to use a computer at the Super User level.

Creating a .bat file using notepad

The first and easiest way to create a bat file is to use the standard Notepad program, which is present in all current versions of Windows.

The steps to create will be as follows.

  1. Launch Notepad (located in Programs - Accessories, in Windows 10 it’s faster to start through a search in the taskbar, if notepad is not in the Start menu, you can start it from C: Windows notepad.exe).
  2. Enter the code of your bat file into the notebook (for example, copy it from somewhere, or write your own, about some commands - further in the instructions).
  3. In the notepad menu, select "File" - "Save As", select the location to save the file, specify a file name with the extension .bat, and be sure to set "All Files" in the "File Type" field.
  4. Click the "Save" button.

Note: if the file is not saved to the specified location, for example, to drive C, with the message “You do not have permission to save files in this place,” save it to the “Documents” folder or to the desktop, and then copy it to the desired location ( the cause of the problem is that in Windows 10 you need administrator rights to write to some folders, and since notepad was not launched as administrator, it cannot save the file to the specified folder).

Your .bat file is ready: if you run it, all the commands listed in the file will be automatically executed (provided there are no errors and administrator rights are needed: in some cases you may need to run the bat file as administrator: right-click on the .bat file - run as administrator in the context menu).

Note: in the future, if you want to edit the created file, simply right-click on it and select “Edit”.

There are other ways to make a bat file, but they all come down to writing commands one command per line to a text file in any text editor (without formatting), which is then saved with the .bat extension (for example, in Windows XP and 32-bit Windows 7 you can even create a .bat file on the command line using the text editor edit).

If you have the display of file extensions turned on (it changes in the control panel — Explorer settings — view — hide extensions of registered file types), then you can simply create a .txt file, then rename the file by installing the .bat extension.

Running programs in bat file and other basic commands

In the batch file, you can run any programs and commands from this list: (although some of these may not be available on Windows 8 and Windows 10). The following is just some basic information for novice users.

The most common tasks are: launching a program or several programs from a .bat file, launching some function (for example, clearing the clipboard, distributing Wi-Fi from a laptop, turning off the computer on a timer).

To start a program or programs, use the command:

If the path contains spaces, enclose the entire path in double quotes, for example:

After the path to the program, you can also specify the parameters with which it should be launched, for example (similarly, if the launch parameters contain spaces, quote them):

Note: in double quotes after start, according to the specifications, the name of the command file displayed in the command line header should be indicated. This is an optional parameter, but in the absence of these quotes, executing bat files that contain quotation marks in paths and parameters may go unexpectedly.

Another useful feature is to launch another bat file from the current file, you can do this using the call command:

The parameters passed at startup can be read inside another bat file, for example, we call the file with the parameters:

In file2.bat you can read these parameters and use them as paths, parameters for launching other programs in this way:

Those. for each parameter we use its serial number with a percent sign. The result in this example will be the output to the command window of all the parameters passed (the echo command is used to display text in the console window).

By default, the command window closes immediately after all the commands have been executed. If you need to read the information inside the window, use the pause command - it will stop the execution of commands (or close the window) before any user presses a key in the console.

Sometimes, before executing the next command, you need to wait a while (for example, until the first program is fully launched). To do this, you can use the command:

If desired, you can run the program in minimized or expanded video using the MIN and MAX parameters before specifying the program itself, for example:

To close the command window after all the commands have completed (although it usually closes when using start to start), use the exit command on the last line. If the console still does not close after starting the program, try using the following command:

Note: in this command, if the path to the program or parameters contain spaces, there may be problems with the launch, which can be solved as follows:

As already noted, this is only very basic information about the most commonly used commands in bat files. If you need to perform additional tasks, try to find the information you need on the Internet (look, for example, “do something on the command line” and use the same commands in the .bat file) or ask a question in the comments, I will try to help.

Instruction manual

  1. Files with the bat extension can perform various operations on the computer. First you need to create the appropriate file, this is done quite simply: open the standard Windows application “Windows Notepad” (This program is included in the package of the licensed operating system Windows 10, which can be bought at a discount in our online store). Next, you need to enter a specific text, it will be different for each operation. For example, using the bat file, you can connect to the Internet, even if you need to enter a username and password.
  2. Before creating a bat file that will include an Internet connection, you need to create an appropriate connection account. In the notebook, enter the following command: Radial “Connection account name” “Login” “Login password”. For example, radial beeline skm256 12345 (skm256 and 12345 are login and password, respectively, and beeline is the name of the connection).
  3. Save the created file with the “Save As” option from the “File” menu. In the file name, change the extension from txt to bat. Saving bat file

To automatically connect your computer to the Internet, create a shortcut for the created file and add it to the autorun section. To open programs, you need to use the word start, for this, write in the notebook the following entry: start "The full path to the location of the file or program on the computer." All long directory names must be abbreviated with the symbol "

”, For example, if you need to enter the“ Program File ”folder, then“ Progra

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.

The example has two names with spaces. Replace them with short ones. The rules for creating short names are as follows: the first six characters of the name without spaces are used in the short name, after the name indicate the serial number of the folder with the symbol

. Since the Program Files and Sound Club folders are singular, I get the following:

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 the Sound Club will be the middle name (names are counted in alphabetical order).

But this method is inconvenient in that you have to specify serial numbers. The situation with Program files is more or less normal. Few people will encounter two such folders on the system drive. But if you decide to install several Mozilla products on your computer. You will get several folders, for example:

Short names for them will be

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

Spaces and quotation marks in bat files

Quotation marks actually work, but not with the methods that are usually advised. Usually advise the following:

So the command will not work, because if you look at the help on it (start /?), Then in the help you will see the following:

As you can see, the first parameter is the window title and it is just in quotation marks. This parameter is optional, but it is still advised to specify it (for example, here) to avoid errors when executing the command. You can’t write anything inside quotation marks. It turns out like this:

Also, the option of quoting all the names with spaces separately will work:

However, in some cases, none of the above works. In such cases, I can advise you to use the cd command. Go to the system partition, then use cd to the Program Files folder and run the program (start):

I think this way will work everywhere. Now a couple of important points. Suppose you 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 commenting it out. The first way is vandal, and the second, see below.

In this case, the launch of the Defraggler.exe program installed in the system is disabled. They comment on lines by specifying the rem command at the beginning of a 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 a command at the end to exit.

Running applications from a bat file

In the first part of the article, I outlined the 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, how to use the BAT file to launch several applications with certain settings or automatically install the program so as not to waste time on answers like Do you agree to the terms of the license agreement? and do not press extra buttons.

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

This does not always work. Therefore, this technique can be completely applied on a particular system, but it does not fit as a universal solution. If there is a goal to make the BAT file work everywhere and always, you need to use the full paths:

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

Running 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 start minimized:

In this case, to command is to indicate the key. The key is indicated by a slash after the main command (command / key). The main command in this case is start. True, the min key only works in half the cases, because it refers specifically to the start command start, and not to the programs that this command starts.

In general, there are a lot of keys, and the key sets of different programs can vary significantly. True, there are several common ones. For example, the help key (/? Or / help). To trace the operation of this key, consider a practical example. Open the console (click + R, type cmd, then Enter) and type the following in the console:

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

Pay attention to the / wait switch. In some cases, it is simply irreplaceable. For example, you decided to unzip the archive with the program using a BAT file and start this program. There will be two teams 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 decompress and there will be nothing to launch. Therefore there will be a mistake. In this case, the / wait switch will come to the rescue:

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

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

In some cases it is very convenient. Avast Anti-Virus has the option of silent installation in the corporate version. The free (home) version of the silent installation is supposedly not. However, if you are aware of how the InstallShield installer works, you will understand that it is a duck, since this installer itself supports the silent installation key / S. So all the products made on its basis - too. And Avast is no exception. Just create a file with the contents in the folder with Avast BAT

run it and the program installs on your computer with almost no involvement from you. Thus, you can write a whole list of programs for silent installation and save time, for example, by reinstalling the system. In the article about silent installation, you can get more detailed information on the keys.

There are other options for managing programs using BAT files. You can start the program by ordering it to open the file at startup. I use this method when developing sites. It is very convenient when all your tools open the necessary documents and folders with the press of just one button:

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

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

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

Наткнулся ещё на один конвертер BAT в EXE, можете рассмотреть в качестве альтернативы предыдущей программе: Advanced Bat To Exe Converter