views:

284

answers:

5

Hi whenever i run the code below it occurs to me I have made a mistake using the if exist lines, as no matter whether the directory exists or not, it acts as if the line was never there... either that or its not reading the else line.


echo off  
echo  
echo (c) Ryan Leach 2010  
echo Stockmaster Backup System for exclusive use of Riverland Paper Supplies  
echo  
echo Please ensure that all computers are out of stock master to the windows xp screen  
echo and that the backup usb with the day of the week labeled on it is inserted  

pause  

IF EXIST D:\RPS_BACKUP\backups_to_zip\ goto zipexist else goto zipexistcontinue  
:zipexist  
IF EXIST d:\RPS_BACKUP\backups_old\ rd /s /q D:\RPS_BACKUP\backups_old  
echo backup did not complete last time, backup will restart from zip-usb phase.  
pause  
call zip  
goto tidyup  
:zipexistcontinue  

IF EXIST D:\RPS_BACKUP\backups_old\ goto oldexists else oldexistscontinue  
:oldexists  
IF EXIST d:\RPS_BACKUP\backup_temp\ rename D:\RPS_BACKUP\backups_temp backups_to_zip  
rd /s /q D:\RPS_BACKUP\backups_old  
echo backup did not complete last time, backup will restart at the zip to usb phase.  
pause  
call zip  
goto tidyup  
:oldexistscontinue  

IF EXIST D:\RPS_BACKUP\backups_temp\ goto tempexists else goto tempexistscontinue  
:tempexists  
IF EXIST D:\RPS_BACKUP\backups_old\ goto backupfailed else goto tempexistscontinue  
:backupfailed  
@rd /s /q D:\RPS_BACKUP\backups_temp  
echo backup did not complete last time, backup will restart from start.  
pause  
:tempexistscontinue  

md D:\RPS_BACKUPS\backups_temp  
xcopy \\user1\c\* D:\RPS_BACKUP\backups_temp\user1\c /h /e /z /f /r /i /s /k  
IF NOT ERRORLEVEL == 1 GOTO ErrorHandler  
xcopy C:\* D:\RPS_BACKUP\backups_temp\user2\c /h /e /f /r /i /s /k  
IF NOT ERRORLEVEL == 1 GOTO ErrorHandler  
xcopy \\user3\c\* D:\RPS_BACKUP\backups_temp\user3\c /h /e /z /f /r /i /s /k  
IF NOT ERRORLEVEL == 1 GOTO ErrorHandler  
call sub  
call zip  
:tidyup  
rename D:\RPS_BACKUP\backups_to_zip backups  
pause  
goto :eof  

:ErrorHandler  
echo xcopyerrorcode is ERRORLEVEL contact ryan  
pause  
+1  A: 

If you want to rule out any problems with the else part, try removing the else and place the command on a new line. Like this:

IF EXIST D:\RPS_BACKUP\backups_temp\ goto tempexists
goto tempexistscontinue  
Prutswonder
thanks trying it now
Ryan The Leach
works thanks muchly i will accept your anwser when the timer is up, also i probably shouldnt have told it to copy hidden and system files, as now its copying the pagefile of a running pc....
Ryan The Leach
A: 

There's an ELSE in the DOS batch language? Back in the days when I did more of this kinda thing, there wasn't.

If my theory is correct and your ELSE is being ignored, you may be better off doing

IF NOT EXIST file GOTO label

...which will also save you a line of code (the one right after your IF).

Second, I vaguely remember some kind of bug with testing for the existence of directories. Life would be easier if you could test for the existence of a file in that directory. If there's no file you can be sure of, something to try (this used to work up to Win95, IIRC) would be to append the device file name NUL to your directory name, e.g.

IF NOT EXIST C:\dir\NUL GOTO ...
Carl Smotricz
im working in windows xp batch scripting, not sure about dos, but it works, discovered my error because of your post, http://www.robvanderwoude.com/ntif.php sums it up.also thanks for the handy nul tip, there isnt a file i can check reliably.
Ryan The Leach
also thanks for the not exist, tip to save a line, but i like the way ive got it, as i can imagine the labels as "pseudo braces"
Ryan The Leach
Carl: The vast majority of batch questions on this site have nothing at all to do with DOS. While many people erroneously claim they are asking a question about DOS batch files, they are usually all working on Windows and mean the Windows command processor `cmd.exe`. The syntax is superficially similar but greatly extended compared to what `command.com` was capable of. Simply lumping both together in one class is just plain wrong.
Joey
COMMAND.COM has been rewritten and called cmd.exe; and various workarounds to common problems have been bolted on to a subset of the commands. It is of course a question of opinion, but I deny that the change from DOS of old is as dramatic as you make it out to be. Case in point: My antiquated knowledge of DOS syntax helped the OP solve his problem.
Carl Smotricz
I acknowledge, though, that mentioning DOS was wrong in the given context, and unnecessarily confusing to the asker.
Carl Smotricz
A: 

Use parentheses to group the individual branches:

IF EXIST D:\RPS_BACKUP\backups_to_zip\ (goto zipexist) else goto zipexistcontinue

In your case the parser won't ever see the else belonging to the if because goto will happily accept everything up to the end of the command. You can see a similar issue when using echo instead of goto.

Also using parentheses will allow you to use the statements directly without having to jump around (although I wasn't able to rewrite your code to actually use structured programming techniques; maybe it's too early or it doesn't lend itself well to block structures as the code is right now).

Joey
A: 

From the help (if /?):

The ELSE clause must occur on the same line as the command after the IF.  For
example:

    IF EXIST filename. (
        del filename.
    ) ELSE (
        echo filename. missing.
    )

The following would NOT work because the del command needs to be terminated
by a newline:

    IF EXIST filename. del filename. ELSE echo filename. missing

Nor would the following work, since the ELSE command must be on the same line
as the end of the IF command:

    IF EXIST filename. del filename.
    ELSE echo filename. missing
Gabe
This doesn't tell the whole story, though. It isn't immediately obvious that `goto` consumes the complete line instead of only one token. It is documented here for `del` which makes sense but intuitively I'd say this isn't obvious for `goto`.
Joey
If the `del` commend needs to be terminated by a newline, is there any reason that any other command wouldn't also need to be terminated by a newline?
Gabe
A: 
@echo off
rem learn how to spell
rem this is another method

:START
rmdir temperary
cls
IF EXIST "temperary\." (echo The temperary directory exists) else echo The temperary directory doesn't exist
echo.
dir temperary /A:D
pause

echo.
echo.
echo Note the directory is not found
echo.
echo Press any key to make a temperary directory, cls, and test again
pause

Mkdir temperary
cls
IF EXIST "temperary\." (echo The temperary directory exists) else echo The temperary directory doesn't exist
echo.
dir temperary /A:D
pause
echo.
echo press any key to goto START and remove temperary directory 
pause 

goto START
Edoctoor