Reputation: 21
Please excuse me if I have made an obvious mistake.
The batch file I have been working on is to prepare some folders (D:\Backups_pc and sub folders Backup_Main and Backup_Copy). Depending upon them existing or not on the D: drive when attached.
The first time the batch file runs it does create the folders and then Robocopies the necessary folders and files into 'Backup_Main'. The second and subsequent times the batch file is executed on the same D drive it should copy the Backup_Main to Backup_Copy and Robocopy updates the Backup_Main.
It doesn't do that but always GOES TO 'KDTFirstRun'.
I have tried IF EXIST D:\Backups_pc\Backup_Copy to evaluate presence of folders and that did similar so in this version I tried to check everything first and store it, thinking that something may be buffered or not evaluated correctly so try to preset it (so to speak). It hasn't worked so I am at a loss. I can't see white spaces in variable definitions, math or whatever to cause the problem or am I blind?
Some help would be very much appreciated. Thank you. Phil.
@ECHO off
SETLOCAL
:: Batchfile to do two types of backup
:: Attempt using variables defined here and set to ZERO
SET /A RootB=0
SET /A MainB=0
SET /A CopyB=0
:: Set the Variables based on folders being in place or not
IF EXIST "D:\Backups_pc\" SET /A RootB=1
IF EXIST "D:\Backups_pc\Backup_Main\" SET /A MainB=1
IF EXIST "D:\Backups_pc\Backup_Copy\" SET /A CopyB=1
:: Diagnostics Echo reports
ECHO R = %RootB%
ECHO M = %MainB%
ECHO C = %CopyB%
SET /A KDTCheck = %RootB% + %MainB% + %CopyB%
ECHO KDTCheck is %KDTCheck%
PAUSE
IF /I KDTCheck EQU 3 GOTO KDTUPdate
IF /I KDTCHeck EQU 2 GOTO KDTFolderError
IF /I KDTCheck EQU 1 GOTO KDTFolderError
IF /I KDTCheck EQU 0 GOTO KDTFirstRun
:KDTFirstRun
ECHO Initialising System and Preparing Backup_Copy.
:: Create the necessary folders, I know I could get smarter but lets get the IF stuff working first.
MD D:\Backups_pc
MD D:\Backups_pc\Backup_Main
MD D:\Backups_pc\Backup_Copy
ECHO Folders created on D Drive
ECHO Backing Up Backup_Main
:: Robocopy a lot of folders
ECHO Backup_Main has been updated with latest data from your computer.
GOTO KDTend
:KDTUpdate
ECHO Updating Backup_Copy.
:: Copy the Backup_Main to Backup_Copy
ECHO In KDTUPdateMain Updating Backup_Main
:: Re run the Robocopy stuff that was done in KDTFirstRun
ECHO Backup_Main has been updated with latest data from your computer.
GOTO KDTend
:KDTFolderError
:: Some limited diagnostics to view.
ECHO There is a problem with the Backup Folders on Drive D.
ECHO R = %RootB%
ECHO M = %MainB%
ECHO C = %CopyB%
ECHO KDTCheck is %KDTCheck%
:KDTend
ENDLOCAL
PAUSE
@ECHO ON
Upvotes: 1
Views: 47
Reputation: 881463
The problem is here:
IF /I KDTCheck EQU 3 GOTO KDTUPdate
You need %
variable markers around your variables:
IF %KDTCheck% EQU 3 GOTO KDTUPdate
Without those, it's comparing the string KDTCheck
rather than the variable content. And, because all the if
statements are failing, it's just dropping through to the code below.
You'll notice I've also removed the /I
, there's little sense doing a case-insensitive comparison on digits.
In short, that comparison code snippet would be better off written as:
REM Go to specific action or error part if invalid setup.
IF %KDTCheck% EQU 3 GOTO KDTUpdate
IF %KDTCheck% EQU 0 GOTO KDTFirstRun
GOTO KDTFolderError
:KDTFirstRun
As an aside, it's not a good idea to use ::
for comments. That actually is a label-type construct and will cause you much angst and teeth-gnashing if you use it in the wrong place, such as:
if %optn%==1 (
:: Option 1 chosen
goto option1_code
)
See here for further information on that.
Upvotes: 1