You are here: Computers > Batch files DinkostaOnline 
  

Home
Topics
Computers
Web design
Software

Batch files

 Batch files are text documents, which contain a series of commands and execute them, one after another. A batch file can be activated if you double-click on it or if you type the name of the batch file into the Command prompt. The purpose of such files is to automate certain repetitive actions and to enable you to work faster and with more comfort. Here is an example of a small batch file that enables you to restart Win 98/Me with a double-click on this batch file on the desktop (without going to "Start", then to "Shut Down" and then "Restart"):

rundll32.exe shell32.dll,SHExitWindowsEx 2

 In order to make a batch file of this, open Notepad, type the code from above (pay attention to the space after exe and before 2), save it as "restart.bat" (the name doesn't matter, but the extension .bat is important) and choose "All files". If you have something open that needs saving, you will be prompted to save it. To avoid this prompt type the number 6 instead of 2. Similar to this, you can make a Shutdown batch, which can be used with a batch file for delayed execution or with the Task Scheduler to automatically shut down the PC on a predefined time.

Some programs and tools (freeware, shareware and commercial) offered on the market for PC users could easily be made with batch files. Here are some examples:
• Backup automation
• Create virtual drives
• Joining MPEG files (MPG, MPA, MP3 etc)
• Create protected folders or files (can be opened, moved or deleted only by the maker; also an excellent way to protect normal folders and files from accidental deletion).
• Unattended ScanDisk - Defrag & Shutdown
• WinZip automation (without Command Line Support Add-On)
• Sending keystrokes to programs (for instance, opening OE and automatically checking and sending email and then closing OE)
• Register who used the PC and at what time
• Delete unnecessary files automatically on every startup (or shutdown)
• Delayed shutdown
• Change Desktop wallpaper with every reboot
• Create reminders (play sound or pop-up at specified time, on start-up, shutdown or specified date)
These are just a few examples (all contained in, and many more), but the important thing is that batch files can be adapted to ones particular needs and that they are very small in size, but execute their task very quickly.
Many of the batch files can also be done with JavaScript or VisualBasic Script. Some examples with JavaScript will be shown on this page from time to time.
Here is the JavaScript version of "restart.bat":
wq = new ActiveXObject("WScript.Shell");
wq.Run ("c:\\windows\\RUNDLL32.exe shell32.dll,SHExitWindowsEx 2")

Needs to be saved with a .js extension.

Playlists: The next example shows how a Batch file can be used to create a playlist for Winamp, Windows Media Player or any other player. Let's say, you want to make a playlist for all your MP3's you have on the C:\ drive. To do that, you could use a batch file like this one:

dir c:\*.mp3 /s /b>Music.m3u

After you execute the above batch, you'll have a playlist called Music.m3u (name it whatever you want, but a playlist needs the .m3u extension). When you double-click or hit ENTER on the Music.m3u playlist, your default Media player will start and play all the MP3's inside the playlist (to view the contents of a playlist: select the playlist, press SHIFT+F10 and from the context menu select Open with...; find Notepad and click OK (make sure the Always use this program for this type of file checkbox is not checked).
If you want to make a playlist for several media formats, then you'll need something like the following batch file:
dir c:\*mpg /b /s > vids.m3u
dir c:\*.avi /b /s >> vids.m3u
Here you need to pay attention to the double redirection sign (>>) in the second line, since otherwise the commands from the second line (if there were a single redirection sign) would erase the contents created by the commands from the first line. Of course, the spaces after dir and before the switches /b and /s are also important.
If your default player is Winamp, for example, and you want to use Windows Media Player to play a video playlist, then you need to drag the playlist to the player. Or make a batch file, containing the path to the Media Player and the playlist.
In order to join several MPEG files (.mpg, .mpeg, .mpa, .mp3 extensions) into one MPEG file, you could use a two-line batch file to do that (though you will have to type every filename manually). All the MPEG-Joiner software is using this method to join MPEG files (maybe there are some exceptions). Here is a MPEG-Joiner I've made and that is also using this method to join MPEG files, but it has a user-friendly GUI interface. You can download the program for free.
To play just video clips, you should use a ASX file. The procedure is the same as with M3U files, the only difference is that you need to place in the file as the first line ASF path and save it with an .asx extension.

The DOS window: If you don't want to see the DOS window during the execution of a Batch file right-click on the Batch file, select 'Properties' and then choose the Program tab, select in the Run: field Minimized and below that check Close on exit. This will create a .pif file, which you should use for the execution of the batch file. If you don't like the look of the pif icon, change it. You can put the batch file inside a folder (for example, in C:\Windows\Batch files) and the pif file on the desktop, but the target (path) has to be correct. A better way to do this would be to create a batch file inside a folder, then right-click the batch file and choose 'Send To' and select 'Desktop (create shortcut)'. After that go to the desktop, right-click the newly created pif file and select Properties, choose the Program tab etc (see above). But if you want a batch file that runs totally invisible, then you would need to use a one-line script to start the batch file. It can produce interesting effects, if this script is placed in the StartUp folder or the path is typed at the 'Run' key in the registry and you use delayed execution to start programs, tools or other batch files.
For a batch that is only sometimes used or has to be adapted before use, making a pif doesn't seem such a good idea. It would then be enough to put as the first line @echo off (so the commands are not shown in the DOS window), then type in the commands and in the last line put cls (stands for 'clear screen').
To edit or just to view the source of a Batch file right-click on its icon and select Edit (if you double-click the icon or press ENTER the Batch file will be executed). Be careful when naming batch files. Don't give them names already taken by other commands and programs - internal or external.
An easy way to use batch files is to use the command line to open folders, documents, and shortcuts, as well as running both DOS and Windows programs. Examples: type in Notepad just regedit and you have now a batch file for quick access to the Registry Editor. SNDREC32 starts the Sound Recorder, SNDVOL32 starts the sound Volume Control applet, WORDPAD starts WordPad etc.


Home  |  JavaScript  |  Windows tips  |  The Registry  |  Pirot  |  Software  |  Travel tips
Hosted by www.Geocities.ws

1