Often the tips for various 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 doesn’t always know how to do this and what a file .bat is.
This manual details how to create a bat command file and run it. Wikiext.com will provide you with more interesting options of a .bat file.
Creating a .bat file using Notepad
The first and easiest way to create a bat file is to use the standard Notepad program that is present in all current versions of Windows.
Steps to create a bat file:
- Start Notepad (located in Programs – Standard, in Windows 10 – through the search in the taskbar, if notepad is not in the Start menu, you can start it from C:\Windows\notepad.exe.
- Enter the code for your bat file into the notepad (for example, copy from somewhere, or write your own).
- In the Notepad menu, select “File” – “Save As”, select the location where the file is saved, specify the file name with the .bat extension, and, in the “File Type” field, select “All Files”.
- 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 location”, save it to the Documents folder or desktop, and then copy to the desired location (the cause of the problem is that Windows 10 requires administrator rights to write to some folders, and since the notepad was not started as an 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 (if there are no errors and you have administrator rights: 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).
In the future, if you want to edit the created file, just right click on it and select “Edit”.
There are other ways to make a bat file, but they all boil down to writing one command per line into a text file in any text editor (without formatting), which is then saved with a .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 enabled the display of file extensions (it changes in the control panel – Explorer options – view – hide extensions of registered file types), you can simply create a .txt file, then rename the file by installing the .bat extension.
Running programs in the bat file and other basic commands
Most often, the following tasks occur: launching a program or several programs from a .bat file, launching a function (for example, clearing the clipboard, distributing Wi-Fi from the laptop, turning off the computer by the timer).
To run the program or programs, use the command:
start “” path_to_program
If the path contains spaces, take the entire path in double quotes, for example:
start “” “C:\Program Files\program.exe”
After the path to the program, you can also specify the parameters with which it should be run, for example (similarly, if the startup parameters contain spaces, take them in quotation marks):
start “” c:\windows\notepad.exe file.txt
In double quotes, after start, the specification must specify the name of the batch file displayed in the command line header. This is an optional parameter, but in the absence of these quotes, executing bat files containing quotes in paths and parameters can go in unexpected ways.
Another useful feature is the launch of another bat file from the current file. You can do it using the call command:
call path_to_file_bat options
The passed parameters 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 for launching other programs in this way:
echo% 1
echo% 2
echo% 3
pause
For each parameter we use its serial number with a percent sign. The result in the above example will output all the passed parameters to the command window (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 in the window, use the pause command – it will stop executing the commands (or closing the window) before pressing any key in the console from the user’s side.
Sometimes, before executing the next command, you need to wait a while (for example, before the first program is fully launched). To do this, you can use the command:
timeout/t time_in_seconds
If desired, you can run the program in minimized or expanded video with 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 the commands have been executed (although, usually it is already closed when using start to start), use the exit command in the last line. If the console still does not close after starting the program, try using this command:
cmd/c start/b”” path_to_program parameters
In this command, if the paths to the program or parameters contain spaces, there may be problems with the launch, which can be solved as follows:
cmd/c start “”/d “path_to_space_folder”/b program_file_name “parameter_space_blocks”