As in the bat program. Creating a .bat file

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 write the path to executable file, then the following happens: the Firefox browser starts and tries to process the request, that is, the file, the path to which 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. Let's take the Filezilla ftp client as an example. 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 serial 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. We go to the system partition, then using cd to 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 Do you agree with the terms of the 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 to the previous program: Advanced Bat To Exe Converter

Often, tips for certain actions and fixes in Windows 10, 8 and Windows 7 include steps like: “create a .bat file with the following content and run it.” However, a novice user does not always know how to do this and what such a file is.

This instruction details how to create a bat batch file, run it and some Additional Information, which may be useful in the context of the topic under consideration.

In a batch file, you can run any programs and commands from this list: https://technet.microsoft.com/ru-ru/library/cc772390(v=ws.10).aspx (however, some of these may be missing in Windows 8 and Windows 10). The following is just some basic information for novice users.

Most often, there are the following tasks: launching a program or several programs from a .bat file, launching some function (for example,).

To run a program or programs, use the command:

Start "" path_to_program

If the path contains spaces, enclose the entire path in double quotes, like so:

Start "" "C:\Program Files\program.exe"

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, put them in quotation marks):

Start "" c:\windows\notepad.exe file.txt

Note: in double quotes after start, according to the specifications, the name must be indicated batch file, displayed in the header of the command line. This is an optional parameter, but in the absence of these quotes, the execution of bat files containing quotes in paths and parameters may go unexpectedly.

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

Call path_to_bat_file parameters

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

Call file2.bat parameter1 parameter2 parameter3

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

echo %1 echo %2 echo %3 pause

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

By default, the command window closes immediately after all commands are executed. If you need to read the information inside the window, use the pause command - it will stop the execution of commands (or closing the window) until any key is pressed in the console by the user.

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

Timeout /t time_in_seconds

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

Start "" /MIN c:\windows\notepad.exe

To close the command window after all commands have been executed (although it usually does so when you use start to run), use the exit command on the last line. In case the console still does not close after starting the program, try using the following command:

Cmd /c start /b "" path_to_program parameters

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

Cmd /c start "" /d "folder_path_with_spaces" /b program_file_name "options_with_spaces"

As already noted, this is only a very basic information about the most commonly used commands in bat files. If you need to perform additional tasks, try searching necessary information on the Internet (search, 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.

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. It is enough to open notepad and save a blank sheet with the .bat extension by selecting the Save as... option and writing something ending in .bat in the File name field, for example 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 the 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. Let's take the Filezilla ftp client as an example. 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 serial numbers. The situation with Program files is more or less normal. Few people will meet two similar folders on the 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. We go to the system partition, then using cd to 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 launch several applications with certain settings using a BAT file or install the program automatically so as not to waste time on answers like Do you agree with the terms of the 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 the sets of keys for different programs can vary 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. In the right place in the 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 a silent installation option in the corporate version. 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. You can get more detailed information on the keys in the article.

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 to the previous program: Advanced Bat To Exe Converter

With this article, we continue a 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 a . BAT which contains the commands to be executed using the command line. When you run such a file, the CMD program is launched, which reads commands from given file and execute them sequentially.

BAT files can be used to automate many processes. For example, using a BAT file, you can run programs that backup files, data archiving and much more.

Create BAT file

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

After that, 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 start executing a BAT file from the command line. To do this, just enter his address.

If you want to continue editing the BAT file, for example, add new commands to it, you need to open the BAT file again with text editor. It is convenient to do this using the context menu.

The use of bat files (otherwise - "batch file", "batch file") can often make your work easier, for example, automate installation software, according to predetermined parameters; date and time synchronization; running command sequences and so on. However, to use such scripts, you need to be able to create them. At their core, files with the .bat extension are a set of console commands, the same as those used on the command line.

The article discusses three methods for creating .bat files:

  1. Using the command line.
  2. Using Notepad.
  3. Using the Dr.Batcher utility.

Step 1. To launch the command line in the Start menu, use the "Search programs and files" dialog. You must set the search parameter to cmd and left-click on the icon in the "Programs" section.

Step 2 It is recommended to use a separate directory for storing written batch files, so select a location on your hard drive and create a special folder by entering the command "MD D:\Bat".

On a note! The command is entered without quotes, a space is placed only after useMD. In this case, the folder named « Bat"will be created on diskD. Command line case insensitive, i.e. the result of executing "MDD:\Bat", "mdD:\Bat" and "mdd:\Bat" will be the same.

Step 3 Create the actual batch file with the command "@echo off > D:\Bat\probnik.bat".

On a note! In this case, the case is important when specifying the location path, that is, the folder "bat", "Bat" and "BAT" - three independent and completely different directories. If you point to a non-existent folder, the command will not be executed. Note that there will be no error message either.

Step 4 To change the contents of a file, open its storage location, call context menu and select the "Change" line.

Create a batch file with Notepad

Step 1. To launch the notepad, go to the "All Programs" menu, the "Accessories" item and click on the launch icon.

Step 2

Learn how to create a file without much programming knowledge in a new article -

As an example, we suggest you use following code, which displays the image of a star:

« @echo off

mode con cols=32 lines=50

title star!

for %%i in (

88888888888888881888888888888888,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,

) do echo %%i

pause > null

Attention! The code must be entered without quotes. Always check the syntax for writing commands. If you forget to put a comma at the end of any line, then when you run the program, it will merge with the next one.

Step 3 Save the resulting file to the desired directory. To change the permission, use the "File type" line, select "All files" in it, and when entering the name, after the dot, specify "bat".

Creating a batch file using the "Dr.Batcher"

Specified software is paid, but provides an opportunity to use the test version for 30 days.

Step 1. To create a file, use the "File" menu, the "New ..." item, or the key combination "Ctrl + N".

Step 2 In the dialog box, check "Empty Batch File" and click "OK".

Step 3 Specify which commands the bat file should execute.

On a note! The right frame contains key commands that you can use when writing code. All you have to do is select the one you want and click on the button.InsertCommand", which helps to save time when working.

Step 4 Save the resulting file to the desired directory.

Important! Please note that this utility allows you to debug the program on the fly. It is enough to use the menu "Batch" to start the already entered sequence. Progress is displayed in the lower frame. When using the "ExecuteinExternalWindow…” will be executed batch file, similar to its launch by means of the operating system.

Conclusion

We have described three ways to create .bat files. Two of these methods are in standard ways available with Windows, one requires the installation of additional software. The evaluation of each of the methods is given in the summary table.

Information\NameCommand lineNotebookDr. Batcher
LicenseDelivery with WindowsDelivery with WindowsPaid
Russian languageDepending on Windows versionDepending on the version
Create a batch fileYesYesYes
Batch file editingNotYesYes
User friendliness (from 1 to 5)4 4 5

Video - Creating a Bat file

Internet