Create batch files
To create and program batch files, all you need is the Microsoft text editor - and of course batch commands. In this practical tip, you will learn how to write batch files using small sample programs.
The first steps - prepare the batch file
Before you can convert the file into a batch file, you must first have the file extensions displayed. Then open the editor. First we create a program that allows you to shutdown the computer with a comment and 20 seconds.
- Start the input with: @echo off. This makes the output clearer.
- On the next line, type "echo, do you really want to shut down the computer?" on.
- Below this is the "pause" command.
- In the next line enter "shutdown -s -c 'This PC shuts down in 20 seconds' -t 2000". Overall, the text looks like this:
- @echo off echo Are you sure you want to shut down the computer? pause shutdown -s -c "This PC shuts down in 20 seconds" -t 20000
- Note that you only enter one command per line (see graphic)
- Save the text file.
Convert the text file.txt into a batch file.bat and execute it
You still can't do anything with the text file and code. Therefore, you need to convert the text file to a batch file.
- Right-click on the text file.txt and select "Rename" from the context menu.
- Change the file extension from ".txt" to ".bat". The "Textfile.txt" thus becomes a "Textfile.bat". Windows warns you that the file may no longer work. Confirm the change with "Yes".
- Double-click the batch file to run it. Now your PC shuts down.
More batch commands
Even if batch files are just simple programs, you can do a lot with them. You receive all batch commands if you enter "help" in the console. As an example, here is a small program that ends processes, creates a text file with personal text, and starts the browser with google.de. You have to confirm each step by pressing a button.
- Start again with "@echo off".
- Enter "taskkill / IM iexplorer" below to exit Internet Explorer. You can find more processes by entering "tasklist" in the console.
- Enter "pause" in the next line. Windows is now waiting for your next entry.
- Enter "echo This is now written to a file> C: \ Test.txt".
- Now the "pause" command again.
- Finally google.de should be opened. To do this, enter "start //www.google.de/". Overall, the code looks like this:
- @echo off taskkill / IM iexplorer pause echo This is now written to a file> C: \ Test.txt pause start //www.google.de/
Was tested under Windows 7. Be careful when programming. Some commands, when used in the wrong combination, can cause serious damage to your computer. Here are more useful tips for Windows XP and Windows 7.