Friday 18 July 2008

Backup USB Memory Stick Files

This is a little short Windows command script to enable you to backup your USB memory stick to your PC's hard drive. (Although I say USB Memory stick, it can backup any directory). You'll need to create a directory to place the script in, the backups are then placed in dated sub directories, so you can keep them as archives if you wish.

1) Decide where you want to keep your backups and create a directory.

2) Create a file called "USBbackup.cmd"

3) Edit the file using Notepad, copying the following text into the file.

@echo off
title USB BackUp
cd /d %~dp0
set /p BACKUPDIR="Enter drive letter to be backed up..: "

REM ====
REM Check if backup source is drive or directory

if exist "%BACKUPDIR%:\." ( set BACKUPDIR=%BACKUPDIR%:
) else ( if not exist %BACKUPDIR%\. goto :error )

REM ====
REM Create sub directory based on date.

for /f "tokens=1,2,3 delims=/" %%A in ('echo %DATE%') do set TARGET=Backup-%%C%%B%%A
for /f "tokens=1,2,3 delims=:." %%A in ('echo %TIME%') do set TARGET=%TARGET%-%%A%%B%%C

REM ====
REM Check source and target

echo Backing up from drive "%BACKUPDIR%" to "%TARGET%"
pause

REM ====
REM Create target directory and backup files

title Backing up files
if not exist ".\%TARGET%\." mkdir .\%TARGET%
XCOPY /E "%BACKUPDIR%\*.*" ".\%TARGET%\"

REM ====
REM Finalise

title Usb Backup Complete
goto :end
:error
title Usb Backup Error
echo Unable to locate drive "%BACKUPDIR%"
:end
pause

4) Save the file and run it by double clicking on it.

5) When prompt, enter the drive letter or directory to be backed up.


USB Stick backup... Done.

No comments: