I’ve written a simple batch file for backing up files and folder onto a different drive letter.
In my case, the destination drive will be a USB drive. Even though I’ve configured Windows to always assign the same drive letter to that device, the possibility remains that I’ll connect a different drive that will be assigned the same drive letter. In order to be sure that I’m backing up to the correct drive, the batch file performs a few checks before copying files.
The first check confirms that a disk exists at that drive letter. The second check confirms that the path is valid. The third check looks for the presence of a file in the destination directory.
To use, simply paste the following code into a text file, change the variables to match your environment, add additional xcopy lines for other folders, then save it as a .bat file. Fire the batch file manually, or place it in your startup folder to automatically back up your files each time you log in to Windows.
:: Back up select files and folders to a location that may be an external drive @echo off :: Set some variables set destinationDrive=D: set destinationPath=backup Set destination=%destinationDrive%\%destinationPath% set validationFile=asdf.txt :: Check to see if the drive is available if not exist %destinationDrive%\. goto :nodestinationDrive :: Move to destination drive %destinationDrive% :: Check to see if the path is available if not exist "\%destinationPath%\." goto :nodestinationPath :: Move to destination path cd %destinationPath% :: Check to see if the validation file exists if not exist %validationFile% goto :novalidationFile :: Backup location is valid @echo The backup location "%destination%" is valid. :: Copy files and folders if source is newer than destination :: Desktop @xcopy "%USERPROFILE%\Desktop" "%destination%\Desktop" /d /e /c /i /q /h /r /k /y @echo. @echo Files copied. Please review output for errors. @pause goto eof :nodestinationDrive @echo The destination drive "%destinationDrive%" does not exist. goto :nocopy :nodestinationPath @echo The destination path "%destinationPath%" does not exist on drive %destinationDrive%. goto :nocopy :novalidationFile @echo The validation file does not exist. goto :nocopy :: No files have been copied :nocopy ::@echo A valid backup location cannot be confirmed. @echo No files have been copied. @echo. @pause
This file works with Windows XP through Windows 7.