Creating A BATCH File: Simplify Text Files
Creating A BATCH File: Simplify Text Files
Creating A BATCH File: Simplify Text Files
They can also potentially hide damaging code, so a good understanding of what they are, how they work, and how to create your own, is crucial to today's IT force. - Version 1.2.0
Bookmark this PCstats guide for future reference. In this Beginners Guide, PCSTATS is going to walk you through one of the simplest but potentially most powerful ways to customize and simplify the management of your computer: batch files. These text files are easy to create, and only as complex as you want them to be, but they can perform many useful operations from file backups to system configuration quickly and automatically. At their simplest, batch files are text files which execute one or more command prompt commands in a specific order. The power of a batch file lies in the way that it allows you to combine multiple commands into one batch file 'program' and customize the way that each command operates. In this article PCSTATS will illustrate what batch files are good for and how to create them. By creating a useful series of batch files designed to allow you to selectively back up files from one location to another, we'll demonstrate the ways that batch files can make your computing life easier while you learn the various option involved in creating them. What can batch files do for you? If you read PCSTATS Guide to the Windows XP Command Prompt, you'll have a passing familiarity with several very useful command prompt commands. Batch files can incorporate any command prompt command (including the switches for that command), execute multiple commands in sequence and choose which commands to use based on user input or the results of previous commands. Essentially, anything you can do in the command prompt you can do better and faster with batch files once you have prepared them. This article deals exclusively with using the Microsoft Windows XP/2K command prompt to create batch files, so commands here may not be 100% compatible with earlier versions of Windows which used a true DOS prompt (and the same holds true in reverse; true DOS batch files may not work under Windows XP). The range of commands available to produce batch files actually decreased with the Windows XP/2K command prompt implementation, but the essential functions are still present. How are batch files created? As we've said, batch files are simply text files, so all you need to create one is the Windows notepad application. Don't use WordPad or a word processor like Microsoft Word; these do not produce 'pure' text files by default, since they add their own text formatting. To create a simple batch file, all you need is a single command you want to run, typed into a text file and saved with the .BAT extension, like 'mybatchfile.bat'. Double click this file and it will run your command. Let's test it out...
Open the notepad application by going to 'start\all programs\accessories\notepad' or 'start\run' and type 'notepad'. In the blank notepad window, type: md c:\testsource md c:\testbackup Now go to 'file' and 'save as'. (in case you did not read our guide to the command prompt, the 'md' command instructs the system to create a directory using a name and location following the command.) Save your first batch file on the desktop as 'myfirstbatch.bat'. Close notepad and you'll see that 'myfirstbatch.bat' has appeared on the desktop. Double click the file to run it. Check your c: window. The 'testsource' and 'testbackup' directories have appeared. Your first simple batch file is a success! Delete the 'myfirstbatch.bat' file from your desktop.
First we need to create a couple of dummy files in our c:\testsource directory to give us something to work with: Navigate to your c:\testsource directory in Explorer and right click on the empty space inside. Select 'new\text document.' Call your new text file 'testdoc1'. Now right click again and create a new bitmap image. Call your image 'testbit1'. Your c:\testsource directory should now have the following contents:
Creating your second batch file Now to create a batch file to backup these files into your c:\testbackup directory automatically. Open up notepad and type the following: @echo off xcopy c:\testsource c:\testbackup /m /e /y The '@echo off' line tells the computer not to display anything onscreen when it runs this batch file. The second line uses the xcopy command to copy all contents of the c:\testsource' directory to c:\testbackup the first time the batch file is run. The second time and all remaining times, it will only copy new files and files which have changed since it was last run. It will not copy unchanged files which it previously copied, even if you delete the copies it made from the c:\testbackup' directory. Now save your batch file as 'testbackup.bat' on your desktop and double click it to run the script. Check the contents of your c:\testbackup directory. It should now have copies of the two files you created in c:\testsource. Good stuff. Now open 'testdoc1' in your c:\testsource directory and add some text then save it. Run your testbackup.bat batch file again, and go to the 'testdoc1' file in the c:\testbackup folder. It should have been updated with the changes you made in the other folder. You've now created a useful backup utility with a simple two-line batch file that just takes a double click to run. Starting to see the potential usefulness of knowing your batch files yet?
In addition to the standard command prompt commands that can be used in batch files, there are a few additional extras like the '@ECHO off' command we used in the last batch file we created. These can be used to modify how a batch file works. Let's run through some of them with examples of how they work: Additional Batch file commands Call Can be used to call another batch file from within the current one call c:\batchfile2.bat Echo Used to display information and commands on the screen or prevent them from being displayed. Echo on causes all commands in the batch file to be displayed onscreen. This is the default setting. @Echo off causes no commands to be displayed. The batch file will run silently unless you use the echo command specifically as below. echo what you want on the screen Causes whatever text comes after the echo command on the same line to be printed on the screen. For Used to select a specific set of files and run a command on each of them. for (variable) in (set of files) do (command) In another example, the batch file line; for %%F in (*.txt) do del "%%F" Gives the variable '%%F' the value of every file ending in .txt in the current directory, then passes that variable to the DEL command. This means that every file with the .txt extension in the current directory will be deleted. Goto Moves to different points within a batch file. The destination point must be indicated with a colon. For example goto end :end echo this is the end, beautiful friend the end If Performs a command depending on a condition. IF must include an ELSE statement which says what happens if the condition is not met. For example: if exist c:\myfile.txt (copy c:\myfile.txt d:\myfiles) else echo myfile.txt does not exist
In this example, if the 'myfile.txt' file exists, it will be copied to d:\myfiles. If it does not, a message will be shown indicating this. Look here for more IF command options. REM Rem is used to create comments, or ignored sections in batch files. The computer will ignore any line that begins with REM, so you can use this command to add notations explaining what your file does. For example: @echo off rem this batch file is rem designed to format rem floppy disks in the a: drive format a:
the c:\testsource directory is 'texttest1.txt', the batch file would look at it, see that it had a .txt extension and assign it as the value of '%%F'. The second part of the command do xcopy c:\testsource\"%%F" c:\text /m /y takes whatever %%F is (in this case your 'texttest1.txt' file) and copies it to the c:\text directory. The quotation marks around %%F are to allow the command to deal with file names containing spaces. The command then loops until it has looked at every file in the current directory before moving on to the next part of the batch file. for %%F in (*.jpg *.bmp *.gif) do xcopy c:\testsource\"%%F" c:\pics /m /y The only thing that is different here is that we are looking for graphics file extensions instead and copying them to the 'c:\pics' directory. Save your third batch file on the desktop as 'trickybackup.bat' and try it out. You'll see that your newest creation neatly differentiates between text documents and pictures and splits them up accordingly.
Find out about this and many other reviews by joining the Weekly PCstats.com Newsletter today! Catch all of PCstats latest hardware reviews right here.