I was trying to write a simple batch file to copy files from my main OS to a backup drive so that when I rebuilt my machine i could then restore them back. I know that there are bits of software that do this but I wanted to have a go myself.
What started out as a simple task has now become a challenge (it would've been quicker to manually copy the files!)but now I want to know why this does not work.
I wanted a generic back up program that could be modified by just changing a few command line variables, but the file names variables will not resolve.
I was hoping someone might be able to point me in the right direction.
Here is my code
:: Backup Sims.BAT
:: Copies files from Sims saved files to D:\Backup\The Sims 2
::
@echo off
:: variables - enter settings here
set Backup drive=D:\Backup
set Folder=The Sims 2
set origfile=C:\Documents and Settings\Laura\My Documents\EA Games
Title=Backup/Restore %Folder% files
set backupcmd=xcopy /s /c /e /d /h /i /r /y /v
set removecmd=rmdir /s /q
CLS
:LOOP
ECHO Please select one of the following actions to perform
Echo.
ECHO 1. Backup
ECHO 2. Restore
ECHO Q. Quit
ECHO.
:: SET /P prompts for input and sets the variable
:: to whatever the user types
SET Choice=
SET /P Choice=Type the letter and press Enter:
:: The syntax in the next line extracts the substring
:: starting at 0 (the beginning) and 1 character long
IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%
ECHO.
:: /I makes the IF comparison case-insensitive
IF /I '%Choice%'=='1' GOTO Backup
IF /I '%Choice%'=='2' GOTO Restore
IF /I '%Choice%'=='Q' GOTO End
ECHO "%Choice%" is not valid. Please try again.
ECHO.
GOTO Loop
:Backup
:: This section removes any previously backed up files then copies new files to the back up origfile
IF EXIST "%Backup drive%\%Folder%" goto Remove backup
:Remove backup
Title=Removing existing files...please wait
%removecmd% "%Backup drive%\%Folder%"
goto Start Backup
IF NOT EXIST "%Backup drive%\%Folder%" goto Start backup
:Start backup
Title=Backing up Sims 2 files...please wait
%backupcmd% "%origfile%" "%Backup drive%"
:Restore
:: This section removes any new files restores the backup files to their origional origfile
IF EXIST "%origfile%\%Folder%" GOTO Remove files
:Remove files
Title=Removing existing files...please wait
%removecmd% "%origfile%\%Folder%"
goto Start restore
IF NOT EXIST "%origfile"\%Folder%" GOTO Start restore
:Start restore
Title=Restoring Sims 2 files...please wait
%backupcmd% "%Backup drive%\%Folder%" "%origfile%"
(This code works if I type in the paths in full)