Links:
VitaminXP.com
EnjoyMP3s.com
eSoftie.com
Sign In:
Your IP: 18.225.175.230
MyForwards.com

How to Set Up/Use Windows Features Command Prompt, CMD Batch Files (Scripts)

    Batch Files (Scripts) in Windows

    Batch files or scripts are small easy-to-write text files that carry out a series of commands. They can be simple enough that even the average home computer user can take advantage of them.
    Systems administrators and power users are well aware of the utility of batch files but the average PC user is generally unacquainted with them or is intimidated by the notion of writing or even running a script. This is unfortunate since it means that many are foregoing the use of a powerful tool for carrying out routine or repetitive tasks. Although batch files can be quite sophisticated and used for complicated network and system administration, they can also be of the utmost simplicity and very brief. In this article, I will discuss how to create some uncomplicated examples for making basic tasks easier.

    What is a batch file?

    These are simple text files containing some lines with commands that get executed in sequence, one after the other. These files have the special extension BAT or CMD. Files of this type are recognized and executed through an interface (sometimes called a shell) provided by a system file called the command interpreter. In Windows XP the command interpreter is the file cmd.exe. The large assortment of versatile commands available in Windows XP makes batch files a powerful tool.

    Constructing a batch file consists of nothing more than opening any text editor like the accessory Notepad, entering some lines containing commands, and saving the file with an extension BAT or CMD. (The CMD extension is limited to newer Windows systems and is not recognized in Windows 9x/Me systems. In Windows XP, there is little practical difference between the two extensions.) Don't use Wordpad or Word unless you are very careful to save all files in pure text format. The commands themselves are often quite simple and there is no need to learn a programming language. Those who wish can explore the intricacies that are available with branching and looping but here I will confine the discussion to some straightforward application to everyday tasks. The focus will be on saving time and effort for some routine stuff like system housekeeping and simple file management.

    Running a batch file is a simple matter of clicking on it. Batch files can also be run in a command prompt or the Start-Run line. In that case, the full path name must be used unless the file's path is in the path environment.

    Constructing a batch file

    In the following discussion it is assumed that the Introductory page and the page on Commands have been read.

    The first line in a batch file often consists of this command @echo offBy default, a batch file will display its commands as it runs. The purpose of this first command is to turn off this display. The command "echo off" turns off the display for the whole script, except for the "echo off" command itself. The "at" sign "@" in front makes the command apply to itself as well. This nuance isn't really all that important in the context here but I mention it because it is often seen in scripts. The scripts we will discuss are very brief and omitting this line won't make any great difference. However, as a matter of good practice, we will enter it in our scripts.

    Our first batch file example is going to list all the files in a folder and put the list in a new text file . We will use the directory command "dir" that is discussed on another page. Open Notepad and enter the line "@echo off" (without quotes). Next enter another line dir "C:\Program Files" > C:\list_of_program_files.txt(I'm assuming that your Program Files folder is on the C: drive). This will give us the two-line file @echo offdir "C:\Program Files" > C:\list_of_program_files.txtNow save this two-line file as "listprograms.bat" (without quotes) to some convenient location. Be sure that Notepad is saving as "All files" and not as a text file. See the figure below.

    Three important points are illustrated in the example script. Note that complete paths are used for files including the drive letter. Also note the quotes around "C:\Program Files". Paths must be quoted whenever a file or folder name has a space in it. Finally note the redirection symbol ">" that is used to send the output to a file instead of the screen.

    All that has to be done to use the file is to double-click it. A file C:\list_of_program_files.txt will then be created.

    A more general version with arguments

    The file that we have been discussing is limited to listing one particular folder and putting the list in one particular file. However, it is easy to make the file able to list whatever folder we want and to put the list wherever we want. Batch files can use arguments or data that is input from the user. The process makes use of placeholders of the form %1, %2, These are replaced in the script by our input data. This type of situation cannot be clicked directly but should be run in a command prompt. The new batch file would be@echo offdir %1 > %2Enter in Notepad and save as "makelist.bat". To run the file , open a command prompt and enter{path}makelist somefolder somewhere\list.txtwhere somefolder is whatever folder (with complete path) that you want to list in somewhere\list.txt. Now you have a little program that will list the contents of a folder whenever you want. If you want a list of all the subfolders as well, use the command dir /s %1 > %2 If you want a list that only includes files of a certain type, MP3 files for example, use dir %1\*.mp3 > %2 The line above illustrates the use of the wildcard "*". The ability to use wildcards greatly enhances the power of batch files.

    Life will be easier if you put all batch scripts in a folder that is in the path environment.

    The Rem statement

    Very often batch files contain lines that start with "Rem". This is a way to enter comments and documentation. The computer ignores anything on a line following Rem. For batch files of any complexity, comments are a good idea.

    More examples

    Following the discussion on another page, it is easy to create batch files for some typical maintenance. To create a very simple backup script, use xcopy. The code might be xcopy %1 %2 /d /s This will update all files in the input source folder %1 and its subfolders by copying to the backup folder %2. In practice, a useful backup script would probably need a few more of the switches discussed at xcopy.

    Again following previous discussiondel %1\*.tmp

    These are simple examples and this page does not pretend to explain everything about batch files. The idea is to show how simple they are and to intrigue readers to look further into the subject. Batch files are discussed in many books on Windows XP, at numerous Web sites and at this Microsoft site. Even if you do not want to write them, there are many already available for your use. This page at a sister site lists a number of sources.


    Published on: 2006-09-11 Author: