Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 GamesTitle=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 EndECHO "%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 origfileIF EXIST "%Backup drive%\%Folder%" goto Remove backup
:Remove backup
Title=Removing existing files...please wait
%removecmd% "%Backup drive%\%Folder%"
goto Start BackupIF 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 origfileIF EXIST "%origfile%\%Folder%" GOTO Remove files
:Remove files
Title=Removing existing files...please wait
%removecmd% "%origfile%\%Folder%"
goto Start restoreIF 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)

I would cut it down to something like this:
::== bb.bat
@echo offchoice /cBRQ backup restore or quit?
goto %errorlevel%:1
rd /s /q d:\blah
xcopy /s /c /e /d /h /i /r /y /v c:\blah d:\blah
goto :eof
:2
rd /s /q c:\blah
xcopy /s /c /e /d /h /i /r /y /v d:\blah c:\blah
goto :eof
:3
::==
=====================================
If at first you don't succeed, you're about average.M2

Thank you for the response, it would seem a much more straight forward method.
The problem I wanted to iron out however was the issue of using the command line variables to create the path names, I have had it working if I just set the 'Backup drive' parameter but not with the other two. I am just interested to know why?

Your code is not, strictly speaking, using 'command line variables'.
Here's an example of a bat using command line variables:
::== my.bat
@echo %1 %2
::==If you run:
my the quick
you will get:
the quick
==========================
To make some headway resolving yours, simplify it to a section which does the SETs and then ECHOs what was SET.
=====================================
If at first you don't succeed, you're about average.M2

I do not know why but my code now runs fine.
Not sure what your my.bat code does but i tried it, then whatever code I ran susequently was listed 1 line of code on a c:\ eg
C:\Batch\set a=1
C:\Batch\set b=2After I ran that, my code ran ok so not sure what my.bat did or if it changed something that allowed my code to run but thank you for your help on this.

You may gave some "leftover" vars.
Try a fresh prompt.
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |