Today I will move on with the second part of the article about Windows command tips. This part will discuss how to create and edit batch files.
Creating batch files
Batch files can be created in graphical environment of Windows as well as in CMD. While there are lots of tasks that are much more efficient to be conducted in CMD, in the case of creating batch files I find it more convenient to do it in GUI environment.
Just right-click on empty area of any folder or desktop and select New -> Text Document. This will create an empty plain-text document in the folder named “New Text Document.txt” or similar name (possibly with a number in brackets if there were previously created empty text documents this way). Now click on it once, wait for a while and click on it second time (or click and press F2, or right-click and select Rename). The filename will be highlighted. Rename the filename and change the extension .txt to .bat.
There is one common feature (but at least in my opinion issue) in Microsoft Windows operating systems that is in newer versions set to be default, which is really a shame. It is Hide extensions for known file types folder option. When I work on someone’s else computer, they actually often have this enabled… It has some pseudoaesthetic motivation of hiding these extensions, but otherwise it’s highly impractical. If you have this option enabled, you will not see .txt part of the filename. If you add it manually, you actually get filename.txt.txt or in a case of .bat filename.txt.bat. So if this is the case open any folder (for example (My) Computer folder). Choose Tools -> Folder options… from the file menu (if you don’t see it, press Alt + F and it will show up). Then Folder Options window will appear. Select View tab and in the Advanced settings: list uncheck Hide extensions for known file types option. Now you can return to the previous step.
In Windows NT, 2000, XP, Vista and 7 you can use cmd extension instead of bat. In old Windows 9x versions and in DOS you could use just .bat. (Command prompt cmd.exe can handle both .cmd and.bat, command.com only .bat). We might say that it would be correct to use .bat extension for command.com and newer .cmd for cmd.exe, but this is actually not the case. Because simply people are get used to bat and it’s still much more common. There are some (truly) minor differences between the two, but most of the time they are absolutely interchangeable in modern Windows operating system.
Editing batch files
After creating the batch file, open it in some plain-text editor (Notepad is just enough). We have introduced some basic commands in the previous part of this article. So let’s type
and save the file. Now we can run test.bat (suppose that we named it so). A window will appear very shortly and then disappear.
To prevent this add pause command in the second line.
Now the window will stay open and its output will be similar to this:
C:\Users\moya\Desktop\bat-test>dir Volume in drive C has no label. Volume Serial Number is 20EB-818C Directory of C:\Users\moya\Desktop\bat-test 02. 11. 2011 11:36 <DIR> . 02. 11. 2011 11:36 <DIR> .. 02. 11. 2011 12:07 10 test.bat 1 File(s) 10 bytes 2 Dir(s) 747 433 140 224 bytes free C:\Users\moya\Desktop\bat-test>pause Press any key to continue . . .
The pause command caused the suspension of processing of a batch program and the last line now contains Press any key to continue . . . message. Processing will continue after pressing any key as message states. Of course pause command may be inserted anywhere in the file, not just at the end, for example:
dir pause dir pause
Another thing you may notice are lines C:\Users\moya\Desktop\bat-test>dir and C:\Users\moya\Desktop\bat-test>pause. It appears as if we had typed these commands manually in the command prompt, but we hadn’t. To prevent this behavior you can preppend @ sign before each command like this:
Now the output will be:
Volume in drive C has no label. Volume Serial Number is 20DA-972D Directory of C:\Users\moya\Desktop\bat-test 02. 11. 2011 11:36 <DIR> . 02. 11. 2011 11:36 <DIR> .. 02. 11. 2011 12:44 12 test.bat 1 File(s) 12 bytes 2 Dir(s) 747 431 907 328 bytes free Press any key to continue . . .
It’s a bit awkward to preppend @ before every command. Luckily, there is a way to apply this behavior globally. Try:
@echo off dir pause
The effect is the same. echo off applies demanded behavior for all following commands in the batch file and @ echo off applies this to that command itself.
We introduced echo command. This command is also useful when we are creating batch files in the command prompt. We will tell more about it in the next part of this series .