Computing.Net > Forums > Programming > Batch programming

Batch programming

Reply to Message Icon

Original Message
Name: Batchboy
Date: July 3, 2007 at 13:55:03 Pacific
Subject: Batch programming
OS: XP
CPU/Ram: intel p4 1.8 512
Model/Manufacturer: Acer
Comment:

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)



Report Offensive Message For Removal


Response Number 1
Name: Mechanix2Go
Date: July 3, 2007 at 15:08:24 Pacific
Subject: Batch programming
Reply: (edit)

I would cut it down to something like this:

::== bb.bat
@echo off

choice /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



Report Offensive Follow Up For Removal

Response Number 2
Name: Batchboy
Date: July 4, 2007 at 15:19:05 Pacific
Subject: Batch programming
Reply: (edit)

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?


Report Offensive Follow Up For Removal

Response Number 3
Name: Mechanix2Go
Date: July 4, 2007 at 22:30:07 Pacific
Subject: Batch programming
Reply: (edit)

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



Report Offensive Follow Up For Removal

Response Number 4
Name: Batchboy
Date: July 6, 2007 at 16:11:11 Pacific
Subject: Batch programming
Reply: (edit)

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=2

After 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.



Report Offensive Follow Up For Removal

Response Number 5
Name: Mechanix2Go
Date: July 6, 2007 at 23:35:41 Pacific
Subject: Batch programming
Reply: (edit)

You may gave some "leftover" vars.

Try a fresh prompt.


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch programming

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge