Batch file: Win 32 xp

if statement not working

Page 1 of 1

1 Replies - 1080 Views - Last Post: 01 September 2010 - 09:07 AM Rate Topic: -----

#1 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Batch file: Win 32 xp

Posted 01 September 2010 - 08:19 AM

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.



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:
_______________________________________________________________  
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 


This post has been edited by redhotfire0: 01 September 2010 - 08:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Batch file: Win 32 xp

#2 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: Batch file: Win 32 xp

Posted 01 September 2010 - 09:07 AM

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


Had to remove the echo command right before the first if statement because it will return zero (0) and therefore made the if statement always go to method exit.

Also, IF ERRORLEVEL evaluates and returns turn if anything equals or is greater then, so I instead used
IF %errorlevel% EQU 0 () ELSE ()
:: same as if (errorLevel == 0) 



Lastly, I had to remove all semicolons to the right of my GOTO statements.

Updated script:

@ECHO OFF
SET a = 0

ECHO _______________________________________________________________  >> C:\Mounting.txt
ECHO Trying to mount forms to F:\ drive: %date% %time% >> C:\Mounting.txt
net use F: \\grfs01\groupdirs$\forms
set /a iNetUseVariable = %errorlevel%
ECHO Net use returned: (%iNetUseVariable%) %date% %time% >> C:\Mounting.txt
if %iNetUseVariable% EQU 0 (GOTO close) ELSE ( GOTO retry )

:retry
set /a a=%a% + 1 
if %a% == 6 (GOTO error) ELSE (GOTO remount)

:remount
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
set /a iNetUseVariable = %errorlevel%
ECHO Net use returned: (%iNetUseVariable%) %date% %time% >> C:\Mounting.txt
IF %iNetUseVariable% EQU 0 (GOTO close) ELSE (GOTO retry)


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

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


This post has been edited by redhotfire0: 01 September 2010 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1