Creating BAT files
People who are familiar with the term batch file know that BAT files can significantly simplify life and save time if you can write and use them correctly. In this article I will talk about how to create BAT files and introduce you to the common mistakes that usually occur when writing them.
Creating a BAT file is very simple. It is enough to open the notebook and save an empty sheet with the extension .bat by selecting the Save as … option and writing in the File Name field something ending in .bat, for example test.bat.
Specify the file type as in the screenshot below – All files. Save and get the BAT file.
create bat file
You can edit the BAT file in notepad or any other text editor focused on working with code.
Now we turn directly to practical information. On the net, many are looking for the answer to the question How to deal with spaces in BAT files ?. In paths to folders and executables, a space causes an error. The most common answer: Enclose the path in quotation marks. And this answer is not correct. True, some will with foam at the mouth claim that it works. So, there are two why – why not true and why some will be.
On Windows (as well as on UNIX), the programs installed in 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 Run applet’s Start applet. One such program is Firefox:
If after this command you write the path to the executable file, the following happens: Firefox launches 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 so that it is not written after start firefox. That is why some comrades will assure that everything works fine. However, if you take a portable program, the situation will be completely different. Consider the Filezilla ftp client as an example. Since the system does not know about the program, the above line
will not work. To start 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
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 ~ character. Since I have singular folders for Program Files and Sound Club, 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 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
Mozill ~ 1
Mozill ~ 2
Mozill ~ 3
Now imagine that you wrote a BAT file mentioning these programs. If you delete 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:
start “C: \ Program Files \ Sound Club \ scw.exe”
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:
START [“header”] [/ D path] [/ I] [/ MIN] [/ MAX] [/ SEPARATE | / SHARED]
[/ LOW | / NORMAL | / HIGH | / REALTIME | / ABOVENORMAL | / BELOWNORMAL]
[/ NODE node NUMA] [/ AFFINITY affinity mask] [/ WAIT] [/ B]
[command / program] [parameters]
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 will turn out like this:
start “” “C: \ Program Files \ Sound Club \ scw.exe”
Also, the option of quoting all names with spaces separately will work:
start C: \ “Program Files” \ “Sound Club” \ scw.exe
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):
cd \ Program Files \ Sound Club \