Batch Files - Backing Up, Restoring Data

For those who still use Windows 98 and Mellinium (yes, a lot of people do, especially outside of the USA), Batch Files remain a powerful and useful tool in troubleshooting and maintaining the computer. Some batch files will run on Windows 2000 and XP as well. Here's a basic tutorial and some examples on how to use batch files to automate backups, delete temporary files, and keep your computer running lean and clean.

A Batch File is a set of instructions written in basic DOS, that will execute the commands written within when you activate it, usually by clicking on it with the mouse. If you are unfamiliar with DOS, go to Easydos.com for a minor education and a full list of DOS commands and what they do. In the examples given here, you will learn some of the basics of DOS.



Part 1 -- For our first example, we will back up your user data for future restoration.

Decide where you want your backup files saved to. Usually, you want that to be somewhere other than on your C drive, as that is the purpose of backups, to preserve certain files in case your C drive crashes. Do you want to save them to your floppy (A drive)? This will work only if the files add up to less than 1.44 meg. If you have a CD burner, you can create a backup CD that should hold everything you have. You can also install a second hard drive, save everything to it, then back that hard drive up on a CD. You can never have enough backups! For this example, let's save everything to a second hard drive, the "D" drive.

A batch file is written on Notepad or other text editor, so first, open a new Notepad document, (Start menu --> Programs --> Accessories --> Notepad). Once you have the Notepad document open, just type in the commands on the keyboard.

In this example, we will start by saving the Favorites folder. Pick a name for your backup folder. This has to be no more than eight letters long, to make it DOS compatable. For this example, I am using the name "cbkup" (short for "C drive backup"). First, type cd \, hit ENTER, then type d: and hit ENTER. This will direct the batch file to focus on the D drive, no matter where you run it from.

Now, type md cbkup and hit ENTER. The "md" command will make a folder ("make directory") on the D drive named "cbkup."

Folder PropertiesNow type cd cbkup and hit ENTER. The "cd" command means "change directory." This will all ow the Batch File to access the newly created folder, which is where we want to save your Favorites folder.

Next, we will create a subdirectory named "fav" for the contents of your Favorites folder, by typing md fav and hitting ENTER. Now here's the tricky part. We need to copy all the folders and bookmarks from within the Favorites into our newly created subdirectory, keeping all the normal filenames. DOS specifies filenames in only eight letter names, while Windows uses extended names. "Favorites" is more than eight letters, so we can't use the full title in our DOS batch file. With any folder in Windows, you can right-click on the folder and click "Properties" on the menu that appears. Properties will show you that folder's MS-DOS name, which is what we use (see the picture at right). For the Favorites folder, it is Favori~1. (The squiggle, or "tilde," is to the left of the 1 on your keyboard).

Now we use the "xcopy" command. Type
Batch File Tutorialxcopy c:\windows\favori~1 d:\cbkup\fav\ /a /s
and hit ENTER. Let's break this down for clarity: XCOPY is a more powerful version of the standard COPY command. It can move entire hard drives. We XCOPY c:\windows\favori~1, which is the DOS pathway to Favorites, to d:\cbkup\fav\ which is the DOS pathway we have created on our second hard drive. The last tags, /a /s are known as "attributes," with the "/a" meaning it preserves the original Windows names (more than eight letters long), the "/s" meaning it will copy all the subdirectories within the Favorites folder, and the "/e" to copy any empty subdirectories. Look up XCOPY at Easydos.com for a full list of these attributes that give you total control over the XCOPY command.
(Important note: The file pathways given here are for Windows 98. Other versions of Windows may vary, so track your pathway to the desired folder carefully.)

At this point, your Notepad document should look like this:


Backup Batch File

Batch Icon Save as a Batch File: Now close the Notepad document, and when prompted to save changes, save as file name backup.bat...whatever you call it, make sure you add the ".bat" which will convert it into a batch file. (IMPORTANT: Make sure your folder options are set to show the file extentions. On your top menu bar, click "View," go to "Folder Options," select "View" on the top tab, and uncheck "Hide file extensions for known file types." Now click "Apply," and close the window.) When saved, your document should appear as the icon at right.

You can now place that Batch File anywhere on your computer, or even on a floppy or CD disk, and it will back up your Favorites folder every time you double-click on it. If it doesn't work, recheck your batch file by right-clicking on it and choosing the "Edit" function.



Now, to back up all your other important files and folders, you simply add the appropriate commands to the Batch File. Shown below is an example of a complete Backup Batch File. This backs up the Favorites folder, the My Documents folder, the Application Data folder, and the Local Settings folder.


Complete Backup Batch File

You will notice that the "cd\" and "d:" lines are repeated at the top. Windows has a small quirk in DOS mode, in that when we return to a hard drive, in this case the D drive, during the same session, by default it will go to the last folder used on that drive. The Batch File operated within the "cbkup" folder, so when we try to use the Batch File again, it returns to "d:\cbkup" instead of just "d:" By adding the second set of commands at the top, we assure that it will always return to "d:" so that we can use the Batch File as often as we like during the same session.

Also notice the last two lines at the bottom. The "cls" stands for "clear screen," which removes all the text from the DOS window. The "@exit" command then closes the window.






We aren't done yet! Now we need to create a batch file to restore your backups.

Make a Restore batch file: A "Restore" file is simple to write. We don't need to create any foldeers or directories. All we are doing is writing the "xcopy" commands in reverse. For example, the command to save the Favorites folder was,
Batch Files Tutorialxcopy c:\windows\favori~1 d:\cbkup\fav\ /a /s /e.
We now just reverse the process by writing
Batch Files Tutorialxcopy d:\cbkup\fav\ c:\windows\favori~1 /a /s /e.
Do the same for all the other lines.


Restore Batch File

     Save this file as a batch file named "Restore," as described above. When it is time to restore your data, this batch file will restore your folders to exactly the same state they were in when you used the Backup batch file.



Return to Tutorials
Learn about the DOS commands we use at Easydos.com
or Go to Greenwood Technical

If you have questions or comments about this tutorial, contact me at

EMail

Copyright 2009 Greenwood Technical