Batch: if statement not working correctly - win x86 xp

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
isdigit
Posts: 2
Joined: 01 Sep 2010 09:03

Batch: if statement not working correctly - win x86 xp

#1 Post by isdigit » 01 Sep 2010 09:24

Hello, I am trying to use a batch file to automate mounting network drives. When the drive is not mounted to F:, net use returns 0 and it exits correctly. When it is already mounted, it returns 2 but my if statement is not working correctly. It never goes to the method retry.

Code: Select all


SET a = 0
@ECHO OFF

ECHO ________________________________________________________ >> C:\Mounting.txt
ECHO Trying to mount forms to F:\ drive: %date% %time% >> C:\Mounting.txt
net use F: \\grfs01\groupdirs$\forms
ECHO Error level: %errorlevel% %date% %time% >> C:\Mounting.txt
if ERRORLEVEL 0 (GOTO :exit) ELSE ( GOTO retry )

:retry
set /a a +=1
if %a == 6 (GOTO error)
ECHO Error: %date% %time% >> C:\Mounting.txt
ECHO Dismounting volume: %date% %time% >> C:\Mounting.txt
net use F: /Delete
ECHO Remounting volume: %date% %time% >> C:\Mounting.txt
net use F: \\grfs01\groupdirs$\forms 
ECHO Error level: %errorlevel% >> C:\Mounting.txt
IF errorlevel 0 (GOTO :exit) ELSE (GOTO retry)


:exit
ECHO Forms folder sucessfully mounted: %date% %time% >> C:\Mounting.txt
exit

::error
ECHO Unknown error: %data %time% >> C:\Mounting.txt
exit


Some output:

Code: Select all

_______________________________________________________________  
Trying to mount forms to F:\ drive: Wed 09/01/2010 11:29:37.04
Error level: 0 Wed 09/01/2010 11:29:37.08
Forms folder sucessfully mounted: Wed 09/01/2010 11:29:37.10
_______________________________________________________________ 
Trying to mount forms to F:\ drive: Wed 09/01/2010 11:29:45.60
Error level: 2 Wed 09/01/2010 11:29:45.66
Forms folder sucessfully mounted: Wed 09/01/2010 11:29:45.66

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Batch: if statement not working correctly - win x86 xp

#2 Post by jeb » 01 Sep 2010 09:39

This explains it: "if /?"

if errolevel number --- condition is fulfilled, if errorlevel is greater or equal than number :!:

I suppose, your errorlevel will always greater or equal than zero :)

At the end of "if /?" there is explained the other way.
So this should work

Code: Select all

if %ERRORLEVEL% EQU 0 (GOTO :exit) ELSE ( GOTO retry )

isdigit
Posts: 2
Joined: 01 Sep 2010 09:03

Re: Batch: if statement not working correctly - win x86 xp

#3 Post by isdigit » 01 Sep 2010 09:52

Yes, I now remember reading that.... Thanks for you assistance!

Edit:
Also, the ECHO right before the first if statement returns 0 so it will always go to method exit. I had to remove this part to make it completely functional:

Code: Select all


::ECHO Error level: %errorlevel% %date% %time% >> C:\Mounting.txt
if ERRORLEVEL 0 (GOTO :exit) ELSE ( GOTO retry )

isdigit

Post Reply