Automatically back up your hard drive, delete temp files, and backup a Web site using batch scripting

Posted on January 7, 2009 by Daniel Petri in Windows Vista with 0 Comments

Batch scripting is a powerful aspect of Windows. If you are unaware, batch scripts are files that are executed by the Command Prompt. In theory, every task that you can complete in Windows using a GUI can be completed using a batch script. This tutorial is an introduction to batch scripting in Windows Vista that will show you how to automatically back up your hard drive and delete your temp files without installing extra software. Additionally, this tutorial will show you how to backup a Web site using Wget and a batch script.

What is Batch Scripting?

Firstly, as an introduction to batch scripting, create a sample batch file, open Notepad (Start >> All Programs >> Accessories >> Notepad) and save the file as hello.bat onto your desktop. On the first line of the file, type echo “hello” on the second line of the file type echo “the end” and resave your batch file. It should look like the following: Open a Command Prompt session (Start >> All Programs >> Accessories >> Command Prompt) and type “cd Desktop” and then “hello.bat” and your output will look like the following: When Vista executes a batch file, it executes one line at a time. Our sample file is printing “hello” and “the end” to the screen. “Echo” is a very simple command to print text to the screen. You can probably imagine that batch scripting allows for the automation of some pretty powerful tasks. Let’s get to them… Note: This is a good place to mention that if you are ever executing a batch script and you would like to stop the sript before completion, hold “CTRL+C” in the Command Prompt and the script will stop.

Automatically back up your hard drive

Open Notepad and type the following on the first line:cd c:\ and on the second line type: xcopy c: e:\backup /s /e /h /D and your file should look like the following: This command will copy the entire contents of your C: drive to a folder called “backup” in your E: drive. If any of your drives have different letters, you will want to change them to match your drives. Save this file as “backup.bat,” and whenever you double-click the file it will backup the entire contents of your C: drive to your E: drive. What is particularly convenient about this script is that after the first time you run it, it will only backup the files that have been changed. In other words, it does not copy your entire hard drive every time you run it, it only copies files that have been changed. You can automate this process even more if you right click on the file, create a shortcut, and put that shortcut into your “Startup” folder. Now whenever you log into Vista, any files that you have modified will get automatically backed up, and never again will you unintentionally lose any data.

Automatically delete your temp files

The Windows Vista Disk Cleanup Wizard does a nice job, however it does not delete your temp files unless they are over a week old. If you would like to automatically do this more frequently, open Notepad and type the following on the first line: cd C:\Users\%username%\AppData\Local and on the second line type: rmdir /S /Q Temp and save the file as removeTemp.bat. Your file should look like the following: Similarly, you can automate this process even more if you right click on the file, create a shortcut, and put that shortcut into your “Startup” folder. Whenever you log into Vista, the temp files from your previous session will be deleted.

Automatically backup a Web site using batch and Wget

You can automatically make a backup of a Web site by using batch scripting in combination with a third party program called Wget. Wget will allow you to save a backup of every file of a Web site. You can download Wget from the Wget homepage. Create a folder at C:\ServerBackup and extract the contents of Wget into it. This is also the location where the contents of the Web site are going to be backed up. Open Notepad and type the following on the first line: cd C:\ServerBackup and on the second line type: wget -r -k -p http://www.YourSiteName.com and save the file as ServerBackup.bat. Your file should look like the following: Similarly, you can automate this process if you right click on the file, create a shortcut, and put that shortcut into your “Startup” folder. Whenever you log into Vista, a backup of a Web site will be made.

Summary

Here is what you’ve learned to do:

  • Write your own batch scripts
  • Automatically back up your hard drive without extra software
  • Automatically delete your temp files without extra software
  • Automatically backup a Web site using Wget and batch scripts

This is only an introduction to batch scripting. With a little imagination, you can accomplish some extremely powerful tasks by using batch scripts.

Sponsored