#1 12 Aug 2016 17:39

jobzombi
New Member
Registered: 11 Aug 2016
Posts: 2

ERRORLEVEL ENV variable updates..but user variable %ERRORLEVEL% doesnt

I have the following batch script that runs on an ftp server.  I use a scheduler that passes 4 variables; SouRCe, EXTension, DESTination, and ZIP Directory.  It's a simple script that requires the first 3 variables, but the user can omit the last 4th one, if they do not need to zip the files. 

@ECHO OFF

SET SRC=%1
SET EXT=%2
SET DEST=%3
SET ZIPD=%4
SET DTE=%DATE:~4,2%%DATE:~7,2%%DATE:~10,4%
FOR /F "TOKENS=1-3 DELIMS=: " %%L IN ('TIME /T') DO SET hhmm=%%L%%M%

DIR /B /A:-d %SRC%
IF ERRORLEVEL 1 (
      	ECHO *** There are no files to process. *** RC=%ERRORLEVEL%
	SET ERRORLEVEL=0
      	GOTO EXIT
)

DEL /Q %SRC%\Thumbs.db

IF DEFINED ZIPD (
	WZZIP %ZIPD%\%DTE%%hhmm%.zip %SRC%\*%EXT%
	IF ERRORLEVEL 1 (
		ECHO *** WZZIP for "%SRC%\*%EXT%" failed. *** RC=%ERRORLEVEL%
		ECHO.
		GOTO EXIT
	)
	ECHO.
	ECHO successfully zipped %SRC% TO %ZIPD%: %DTE%%hhmm%.zip
	ECHO.
)

MOVE /Y %SRC%\*%EXT% %DEST%
IF ERRORLEVEL 1 (
   	ECHO ** MOVE "%SRC%\*%EXT%" TO "%DEST%"  Failed. *** RC=%ERRORLEVEL%
)

:EXIT
EXIT /B %ERRORLEVEL%

The issue I am encountering is that if nothing is in the directory at 8:15am,

DIR /B /A:-d %SRC%

fails with a errorlevel of 1. It goes into the loop and exits.  The test for errorlevel work both as

IF ERRORLEVEL 1

and as

IF NOT %ERRORLEVEL% == 0

. The issue I am encountering is that any subsequent failures are not updating the user variable of %ERRORLEVEL%.  This is why I HAVE to use

IF ERRORLEVEL 1

for the other checks.  Now if WZZIP fails, it goes into the loop following the command, but the ECHO message spits out "RC=0" even though ERRORLEVEL had to be greater-than-or-equal to 1 and as such should have updated %ERRORLEVEL% to whatever it is supposed to be.  I tested the script adding

ECHO %ERRORLEVEL%

every line just to see the changes and even though the DIR test updates %ERRORLEVEL% correctly, again DEL of the thumb.db, which fails 99.999% of the time, comes back %ERRORLEVEL% = 0 and WZZIP if forced to fail also comes out RC=0.  Tried SETLOCAL, SETLOCAL ENABLEEXTENSIONS, and a bunch of other stuff, even tried removing everything but the WZZIP part and still it does not update %ERRORLEVEL%.

It is an FTP server, that we are constantly running scripts to transfer files, so I was wondering if maybe there is some conflict with other streams changing the env variable as I am running my script?

Offline

#2 12 Aug 2016 17:42

jobzombi
New Member
Registered: 11 Aug 2016
Posts: 2

Re: ERRORLEVEL ENV variable updates..but user variable %ERRORLEVEL% doesnt

Forgot to add, most of the other scripts were not written by me and they LOVE using

SET ERRORLEVEL=whatevernumber

and I know this has caused me conflicts in the past.  I'm wondering if this could be the culprit.

Offline

#3 13 Aug 2016 04:49

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 139

Re: ERRORLEVEL ENV variable updates..but user variable %ERRORLEVEL% doesnt

jobzombi wrote:

Forgot to add, most of the other scripts were not written by me and they LOVE using

SET ERRORLEVEL=whatevernumber

and I know this has caused me conflicts in the past.  I'm wondering if this could be the culprit.

That is exactly what is causing it. When nobody messes with it, ERRORLEVEL and %ERRORLEVEL% have the same value. But you can overwrite %ERRORLEVEL%. Don't do that; it can break things since it would otherwise be a system variable.

Only idiots manually set %ERRORLEVEL%. Feel free to print this out and show it to your coworkers. If you manually set %ERRORLEVEL%, you are a complete moron.

Online

#4 13 Aug 2016 06:01

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 336

Re: ERRORLEVEL ENV variable updates..but user variable %ERRORLEVEL% doesnt


CMD and InstallShield

Offline

#5 13 Aug 2016 12:57

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: ERRORLEVEL ENV variable updates..but user variable %ERRORLEVEL% doesnt

Shadow Thief wrote:

Only idiots manually set %ERRORLEVEL%.

I think you're a little harsh there Shadow Thief, they are only inexperienced dumbo's.  wink


It's on par with using PATH as a general variable name and calling your pinging batch script "ping.bat"  big_smile

Offline

Board footer

Powered by FluxBB