Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help with looping batch script!

82 views
Skip to first unread message

Damsel

unread,
Dec 24, 2007, 3:44:57 PM12/24/07
to
I must be doing this wrong, but I can't figure out what I'm doing... I
want the CHECK_WORKFLOW (a batch file that calls a job) to continue
to be called by CHECK_RUNNING_FILE until the job program puts a
"failed" or another string besides "running" in the logfile. Can
someone look at this and see what I'm doing wrong?

Thanks so much.

@ECHO ON

:START_WORKFLOW
ECHO cmd startworkflow -sv %1 -d %2 -u %db_login% -p %db_password% -f
%3 %4>%5
CALL %5

:CHECK_WORKFLOW
ECHO cmd getworkflowdetails -sv %1 -d %2 -u %db_login% -p %db_password
% -f %3 %4>%6
CALL %6>E:\test\awdev_scripthome\exec\logfile.log
SLEEP 5

:CHECK_RUNNING_FILE
TYPE E:\test\awdev_scripthome\exec\logfile.log|FIND "running">nul
IF NOT ERRORLEVEL 1 GOTO:CHECK_WORKFLOW
IF ERRORLEVEL 1 GOTO:RUNCHECK

:RUNCHECK
TYPE E:\test\awdev_scripthome\exec\logfile.log|FIND "failed">nul
IF NOT ERRORLEVEL 1 GOTO:FAILED
IF ERRORLEVEL 1 GOTO:CHECK_SUCCESS_FILE

:CHECK_SUCCESS_FILE
TYPE E:\test\awdev_scripthome\exec\logfile.log|FIND "successful">nul
IF NOT ERRORLEVEL 1 GOTO:SUCCESS
IF ERRORLEVEL 1 GOTO:UNKNOWN

:FAILED
ECHO "THERE IS AN ERROR"
TYPE E:\test\awdev_scripthome\exec\logfile.log
EXIT /b 1

:SUCCESS
ECHO "THIS COMPLETED SUCCESSFULLY"
TYPE E:\test\awdev_scripthome\exec\logfile.log
EXIT /b

:UNKNOWN
ECHO "THIS IS AN UNKNOWN ERROR"
TYPE E:\test\awdev_scripthome\exec\logfile.log
EXIT /b

billious

unread,
Dec 24, 2007, 6:47:16 PM12/24/07
to

"Damsel" <kimberly...@gmail.com> wrote in message
news:2b8e5b7d-d841-4ce5...@b40g2000prf.googlegroups.com...

NT+ is better dealt with in alt.msdos.batch.nt as NT+ batch concepts are
different from DOS/9x.

It would appear that yor procedure should work, so it would be reasonable to
conclude that getworkflowdetails is placing "running" into the logfile.
Really need to see the getworkflowdetails procedure - it might be putting
"Now running ....." or something similar as a sign-on, then appending the
final status.

call %6>filename will direct ALL output of %6 to the file, not just the last
line...that might be the problem.


Todd Vargo

unread,
Dec 24, 2007, 8:34:03 PM12/24/07
to
billious wrote:
>
> "Damsel" <kimberly...@gmail.com> wrote in message
> news:2b8e5b7d-d841-4ce5...@b40g2000prf.googlegroups.com...
> >I must be doing this wrong, but I can't figure out what I'm doing... I
> > want the CHECK_WORKFLOW (a batch file that calls a job) to continue
> > to be called by CHECK_RUNNING_FILE until the job program puts a
> > "failed" or another string besides "running" in the logfile. Can
> > someone look at this and see what I'm doing wrong?
> >
> > Thanks so much.
> >
> > @ECHO ON
> >
> > :START_WORKFLOW
> > ECHO cmd startworkflow -sv %1 -d %2 -u %db_login% -p %db_password% -f
> > %3 %4>%5
> > CALL %5
> >
> > :CHECK_WORKFLOW
> > ECHO cmd getworkflowdetails -sv %1 -d %2 -u %db_login% -p %db_password
> > % -f %3 %4>%6
> > CALL %6>E:\test\awdev_scripthome\exec\logfile.log
snip...

>
> NT+ is better dealt with in alt.msdos.batch.nt as NT+ batch concepts are
> different from DOS/9x.
>
> It would appear that yor procedure should work, so it would be reasonable
to
> conclude that getworkflowdetails is placing "running" into the logfile.
> Really need to see the getworkflowdetails procedure - it might be putting
> "Now running ....." or something similar as a sign-on, then appending the
> final status.
>
> call %6>filename will direct ALL output of %6 to the file, not just the
last
> line...that might be the problem.

Would may be worthwhile to see a sample command line that causes the error.
Also, I seem to recall reading there is a bug in some Windows versions (XP?)
where GOTO:Label fails without a space between GOTO and the colon.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Damsel

unread,
Dec 26, 2007, 3:07:51 PM12/26/07
to
I did get this to work; however, now I'm running into something odd.
Here is my revamped code (please see comments below)

@ECHO OFF
echo awscripthome=%AWSCRIPTHOME%

REM: This starts the job process.
:START_WORKFLOW
pmcmd startworkflow -sv %1 -d %2 -u %db_login% -p %db_password% -f %3
%4

REM: This starts the process that outputs a log from the job into a
logfile for checking.
:CHECK_WORKFLOW
pmcmd getworkflowdetails -sv %1 -d %2 -u %db_login% -p %db_password% -
f %3 %4>%AWSCRIPTHOME%\exec\logfile.log
%AW_HOME%\utilities\sleep 10 GOTO:CHECK_RUNNING_FILE

REM: This checks the logfile to see if it's still running.
:CHECK_RUNNING_FILE
TYPE %AWSCRIPTHOME%\exec\logfile.log|FIND "[Running]">nul


IF NOT ERRORLEVEL 1 GOTO:CHECK_WORKFLOW
IF ERRORLEVEL 1 GOTO:RUNCHECK

REM: This checks the logfile to see if it's failed.
:RUNCHECK
TYPE %AWSCRIPTHOME%\exec\logfile.log|FIND "[Failed]">nul


IF NOT ERRORLEVEL 1 GOTO:FAILED
IF ERRORLEVEL 1 GOTO:CHECK_SUCCESS_FILE

REM: This checks the logfile to see if it's succeeded.
:CHECK_SUCCESS_FILE
TYPE %AWSCRIPTHOME%\exec\logfile.log|FIND "[Successful]">nul


IF NOT ERRORLEVEL 1 GOTO:SUCCESS
IF ERRORLEVEL 1 GOTO:UNKNOWN

REM Here's where I'm running into issues... if it has succeeded it
returns the correct errorcode (0) and echoes this - but then it
continues on and also
echoes out the :UNKNOWN and :FAILED statements as well. If it gets to
this part, how do I stop it from continuing on?


:SUCCESS
ECHO "THIS COMPLETED SUCCESSFULLY"

SET err_tmp=%errorlevel%
if not %err_tmp% == 0 set err=%err_tmp%
TYPE %AWSCRIPTHOME%\exec\logfile.log
call errnotify %err%

:UNKNOWN
ECHO "THIS IS AN UNKNOWN ERROR"

SET err_tmp=%errorlevel%
if not %err_tmp% == 0 set err=%err_tmp%
TYPE %AWSCRIPTHOME%\exec\logfile.log
call errnotify %err%

REM: This works okay, it echoes what I need it to do and then stops.


:FAILED
ECHO "THERE IS AN ERROR"

SET err_tmp=%errorlevel%
if not %err_tmp% == 0 set err=%err_tmp%
TYPE %AWSCRIPTHOME%\exec\logfile.log
call errnotify %err%

On Dec 24, 5:34 pm, "Todd Vargo" <tlva...@sbcglobal.netz> wrote:
> billious wrote:
>
> > "Damsel" <kimberly.shaf...@gmail.com> wrote in message

Damsel

unread,
Dec 26, 2007, 4:09:15 PM12/26/07
to
Never mind; I put in an EXIT after every call errnotify %err% and it
stopped the script there. If anyone sees something wrong with that,
please let me know.

ks

billious

unread,
Dec 26, 2007, 7:44:58 PM12/26/07
to

"Damsel" <kimberly...@gmail.com> wrote in message
news:14162fb1-20c3-462c...@s19g2000prg.googlegroups.com...

> On Dec 26, 12:07 pm, Damsel <kimberly.shaf...@gmail.com> wrote:
>> On Dec 24, 5:34 pm, "Todd Vargo" <tlva...@sbcglobal.netz> wrote:
>>
>> > billious wrote:
>>
>> > > "Damsel" <kimberly.shaf...@gmail.com> wrote in message
>> > >news:2b8e5b7d-d841-4ce5...@b40g2000prf.googlegroups.com...
[snip]
> Never mind; I put in an EXIT after every call errnotify %err% and it
> stopped the script there. If anyone sees something wrong with that,
> please let me know.
>
> ks
>
>

I've worked on this for a while, and I've come to the conclusion that the
problem is the flow-control mechanism that batch uses.

Since we're in the dark about the executables involved - whether they are
.exes or .bats for instance, and since we don't actually know what you want
to eventually achieve, perhaps an indication would be appropriate?

I modified your (modifed) batch as follows:

----- batch begins -------
[1]@ECHO OFF
[2]set awscripthome=c:\106x
[3]set AW_HOME=c:\106x
[4]echo awscripthome=%AWSCRIPTHOME%
[5]
[6]REM: This starts the job process.
[7]:START_WORKFLOW
[8]call pmcmd startworkflow -sv %1 -d %2 -u %db_login% -p %db_password% -f
%3 %4
[9]
[10]REM: This starts the process that outputs a log from the job into a
logfile for checking.
[11]:CHECK_WORKFLOW
[12]echo CHECK_WORKFLOW %time%
[13]call pmcmd getworkflowdetails -sv %1 -d %2 -u %db_login% -p
%db_password% -f %3 %4>%AWSCRIPTHOME%\exec\logfile.log
[14]call %AW_HOME%\utilities\sleep 2 GOTO:CHECK_RUNNING_FILE
[15]
[16]REM: This checks the logfile to see if it's still running.
[17]:CHECK_RUNNING_FILE
[18]TYPE %AWSCRIPTHOME%\exec\logfile.log|FIND "[Running]">nul
[19]IF NOT ERRORLEVEL 1 GOTO:CHECK_WORKFLOW
[20]IF ERRORLEVEL 1 GOTO:RUNCHECK
[21]
[22]REM: This checks the logfile to see if it's failed.
[23]:RUNCHECK
[24]TYPE %AWSCRIPTHOME%\exec\logfile.log|FIND "[Failed]">nul
[25]IF NOT ERRORLEVEL 1 GOTO:FAILED
[26]IF ERRORLEVEL 1 GOTO:CHECK_SUCCESS_FILE
[27]
[28]REM: This checks the logfile to see if it's succeeded.
[29]:CHECK_SUCCESS_FILE
[30]TYPE %AWSCRIPTHOME%\exec\logfile.log|FIND "[Successful]">nul
[31]IF NOT ERRORLEVEL 1 GOTO:SUCCESS
[32]IF ERRORLEVEL 1 GOTO:UNKNOWN
[33]
[34]REM Here's where I'm running into issues... if it has succeeded it

returns the correct errorcode (0) and echoes this - but then it continues on
and also echoes out the :UNKNOWN and :FAILED statements as well. If it gets
to this part, how do I stop it from continuing on?

[35]:SUCCESS
[36]ECHO "THIS COMPLETED SUCCESSFULLY"
[37]SET err_tmp=%errorlevel%
[38]if not %err_tmp% == 0 set err=%err_tmp%
[39]TYPE %AWSCRIPTHOME%\exec\logfile.log
[40]call errnotify %err%
[41]
[42]:UNKNOWN
[43]ECHO "THIS IS AN UNKNOWN ERROR"
[44]SET err_tmp=%errorlevel%
[45]if not %err_tmp% == 0 set err=%err_tmp%
[46]TYPE %AWSCRIPTHOME%\exec\logfile.log
[47]call errnotify %err%
[48]
[49]REM: This works okay, it echoes what I need it to do and then stops.
[50]:FAILED
[51]ECHO "THERE IS AN ERROR"
[52]SET err_tmp=%errorlevel%
[53]if not %err_tmp% == 0 set err=%err_tmp%
[54]TYPE %AWSCRIPTHOME%\exec\logfile.log
[55]call errnotify %err%
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

Where:
C:\106x is my test directory
[8,13] CALLed pmcmd - I can only assume PMCMD is an executable
[12] to provide on-screen notification of looping
[14] SLEEP is presumably an executable. I substituted a batch and reduced
the sleep time to 2 seconds. The GOTO would be seen by SLEEP as superfluous
parameters. Presumably, it should actually be the next batch line - but it's
superfluous there as well.
[40,47,55] - not sure what errnotify is, presumably it's a batch file.

Comment:
[20,26,32] are superfluous. If ERRORLEVEL is zero, the previous line will be
effected and control transferred to the appropriate label, otherwise the
next line is executed and ERRORLEVEL must implicitly be 1 or greater. There
are circumstances where ERRORLEVEL may be negative - but these are very
esoteric, and not possible after a FIND command has been executed AFAIAA.

[40,47,55] CALL batchfilename will execute the batch file and then continue
with the next line of the routine, hence the phenomenon you experienced. Had
you omitted the CALL keyword, then control would have been transferred to
the start of the target batchfile and there would be no return made to this
routine. Your EXIT command is a "big stick" method to accomplish the same
thing.

You appear to have stuck to the "goto:label" construct, and it appears to
work - but it is not common practice.

Also - essentially, labels are only significant to the eight character. This
can be a trap (but is not an issue here - I don't enjoy typing)

Here are the batches I constructed to simulate the unknown commands:
----- batch sleep.bat begins -------
[1]@echo off
[2]ping -n %1 127.0.0.1 >nul
------ batch sleep.bat ends --------

Note : the parameter following "-n" is numberofsecondstodelay+1 and the
timing approximate. Uses inbuilt features and avoids the use of 3rd-party
utilities.

----- batch errnotify.bat begins -------
[1]@echo off
[2]echo errnotify -^> %1
------ batch errnotify.bat ends --------

Note: ^ is to escape the ">" which would otherwise be interpreted as an
output redirector. Should show "errornotify -> parameterprovided" on the
screen.

----- batch pmcmd.bat begins -------
[1]@echo off
[2]echo %1
[3]if %1%3%time:~6,1%==getworkflowdetails41 echo [Error two]&goto :eof
[4]if %1%3==getworkflowdetails4 echo [Running]
[5]if %1%3==getworkflowdetails3 echo [Failed]
[6]if %1%3==getworkflowdetails2 echo [Successful]
[7]if %1%3==getworkflowdetails1 echo [Error]
[8]
------ batch pmcmd.bat ends --------

Since the first parameter to pmcmd in the main batch is either
"startworkflow" or "getworkflowdetails" and the 3rd parameter is the first
parameter from the main batch's invoker, this allows a test using "mainbatch
1" (or 2,3,4) to output the significant string and simulate the real
procedure. The "%time:~6,1%" item gets the tens-of-seconds digit (at least
it does on my machine) so that for the "mainbatch 4" test, if the
tens-of-second digit is "1" the string "[Error two]" is produced, otherwise,
"[Running]" is produced.
Note the ":eof", which is a special label (the colon IS required here) for
NT+ meaning end-of-THIS-batchfile.

Here's a modified procedure with the superfluous details removed:


----- batch damsel2.bat begins -------
[1]@ECHO OFF
[2]set awscripthome=c:\106x
[3]set AW_HOME=c:\106x
[4]echo awscripthome=%AWSCRIPTHOME%
[5]
[6]REM: This starts the job process.
[7]call pmcmd startworkflow -sv %1 -d %2 -u %db_login% -p %db_password% -f
%3 %4
[8]
[9]REM: This starts the process that outputs a log from the job into a
logfile for checking.
[10]:CHECK_WORKFLOW
[11]echo CHECK_WORKFLOW %time%
[12]call pmcmd getworkflowdetails -sv %1 -d %2 -u %db_login% -p
%db_password% -f %3 %4>%AWSCRIPTHOME%\exec\logfile.log
[13]call %AW_HOME%\utilities\sleep 2
[14]
[15]REM: This checks the logfile to see if it's still running.
[16]TYPE %AWSCRIPTHOME%\exec\logfile.log|FIND "[Running]">nul
[17]IF NOT ERRORLEVEL 1 GOTO CHECK_WORKFLOW
[18]
[19]REM: This checks the logfile to see if it's failed.
[20]:RUNCHECK
[21]set result=Unknown Error
[22]TYPE %AWSCRIPTHOME%\exec\logfile.log|FIND "[Failed]">nul
[23]IF NOT ERRORLEVEL 1 set result=Failed
[24]TYPE %AWSCRIPTHOME%\exec\logfile.log|FIND "[Successful]">nul
[25]IF NOT ERRORLEVEL 1 set result=Succeeded
[26]echo Result is %result%
[27]TYPE %AWSCRIPTHOME%\exec\logfile.log
[28]errnotify %err%
------ batch damsel2.bat ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

[2,3] are to suit my environment
[7,12,13] - the CALL is simply to invoke the return mechanism for batch
flow-control. If PMCMD and SLEEP are executables, the CALL keyword may be
removed
[11] is simply to provide on-screen notification of loop-progress.
[13] reduced to 2 for testing. This line could be replaced by
ping -n 11 127.0.0.1 >nul
to give an approximate 10-second delay without 3rd-party utilities.
[20] is superfluous
[21-26] sets the environment-variable "result" according to what is found in
the file; [27] shows the logfile regardless and [28] invokes ERRNOTIFY,
never to return.

We're not familar with EXACTLY what you wnat to do, whether you want to put
specific valus into specific variables, or whether you want ERRORLEVEL to be
established (0=success, non-0=failure) at the end of the procedure.

Top-posting, where new comments are added at the top of the message, is
normal in emails where the recipient is presumed to be familiar with the
subject.
End-posting, where new comments are added at the end of the message, is
normal in newsgroups so that a new entrant to the discussion may follow what
has already been said.


PeterD

unread,
Dec 27, 2007, 11:17:43 AM12/27/07
to
On Wed, 26 Dec 2007 12:07:51 -0800 (PST), Damsel
<kimberly...@gmail.com> wrote:


>
>REM Here's where I'm running into issues... if it has succeeded it
>returns the correct errorcode (0) and echoes this - but then it
>continues on and also
>echoes out the :UNKNOWN and :FAILED statements as well. If it gets to
>this part, how do I stop it from continuing on?
>:SUCCESS
>ECHO "THIS COMPLETED SUCCESSFULLY"
>SET err_tmp=%errorlevel%
>if not %err_tmp% == 0 set err=%err_tmp%
>TYPE %AWSCRIPTHOME%\exec\logfile.log
>call errnotify %err%

goto :donenow

>
>:UNKNOWN
>ECHO "THIS IS AN UNKNOWN ERROR"
>SET err_tmp=%errorlevel%
>if not %err_tmp% == 0 set err=%err_tmp%
>TYPE %AWSCRIPTHOME%\exec\logfile.log
>call errnotify %err%

goto :donenow

>
>REM: This works okay, it echoes what I need it to do and then stops.
>:FAILED
>ECHO "THERE IS AN ERROR"
>SET err_tmp=%errorlevel%
>if not %err_tmp% == 0 set err=%err_tmp%
>TYPE %AWSCRIPTHOME%\exec\logfile.log
>call errnotify %err%
>

:donenow

REM loop around the undesired code.

Damsel

unread,
Jan 3, 2008, 2:09:29 PM1/3/08
to
On Dec 27 2007, 8:17 am, PeterD <pet...@hipson.net> wrote:
> On Wed, 26 Dec 2007 12:07:51 -0800 (PST), Damsel
>

thank you!

0 new messages