[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with a batch file and GOTO sections

Posted on 2009-12-22
19
Medium Priority
?
434 Views
Last Modified: 2012-05-08
Hi guys hope you are all well and have a fantastic christmas to all of you.
Guys id love your assistance on this one.
I have a large batch file called master.cmd

================================= master.cmd

call test1.cmd

:DRIVEAREA

:EOF



Now, in the following batch file, called test1.cmd, there is a goto section as follows:

GOTO DRIVEAREA


When I run master.cmd, it correctly calls test1.cmd, but when I get to the GOTO section in test1.cmd to go to section DRIVEAREA, which exists in master.cmd, i get an error, saying cannot find batch label specified.

The reason i am doing this is because master.cmd is huge, so i wanted to break it up into chunks of code, leaving the section labels in the code, and putting in GOTO sections referencing the section names that exist in master.cmd in those other files.

Guys, what am i doing wrong?
Is there a way to reference goto sections in master.cmd from other files?

Any help greatly appreciated.
0
Comment
Question by:Simon336697
  • 8
  • 7
  • 3
  • +1
19 Comments
 
LVL 59

Accepted Solution

by:
Bill Prew earned 1840 total points
ID: 26105820
The GOTO statement can only get to labels within the current batch file, not from any calling files.

What you will likely need to do is return different values from the called script via the EXIT command, and then check those in the calling script, and then GOTO different labels depending on that value.

~bp
0
 
LVL 1

Author Comment

by:Simon336697
ID: 26105899
Hi Bill thanks so much for your help.

Im going to place an example here and hope you can offer advice.

there are 2 scripts below:

main.cmd
and child.cmd

when i kick off main.cmd, i get an error stating that the label :PRELIMINARY_FOLDERFILEBUILDS
cannot be found in main.cmd from child.cmd



=============================================== main.cmd

@echo off

rem standard.cmd %1 (where %1 is a text file of machine names)

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
rem enforce correct usage:
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

if %1'==' echo usage is: %0 FILENAME.EXT && goto :eof
if not exist %1 echo %1 was not found && goto :eof

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
rem configure initial cmd window environment:
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

mode con: cols=200 lines=3000
IF DEFINED mywindowtitle set mywindowtitle=
IF DEFINED scriptpurpose set scriptpurpose=
set mywindowtitle=PARENT:multi___runas:%userdomain%\%username%..........script:%0...%1..........scriptpath:%~dp0
set scriptpurpose=do system-generic checks prior to running specfic.cmd.
title %mywindowtitle%
echo.

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
rem standard tests (5):
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

rem 1.location		{ <domain> }
rem 2.status		{ enabled | disabled }
rem 3.availability	{ alive | dead(inqip) | dead(notinqip) }
rem 4.identity		{ wronghost | righthost(<ip>) }
rem 5.accessibility	{ yes | no }

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
rem include the following paths:
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

set pth_standard="includes\standard"

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
rem clean/remove/undefine all variables:
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

call %pth_standard%\1.undefine_variables_all.cmd

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
rem declare variables:
rem 1.declare THIS script's variables (to be defined here & not called, since if called, those vars will act on the calling & not called (this) script)
rem 2.declare non-test specific variables (must be included before specific) such as date, time and sleep variables.
rem 3.declare script execution-specific variables (create a folder for each run)
rem 4.declare test 1 variables: location
rem 5.declare test 2 variables: status
rem 6.declare test 3 variables: availability
rem 7.declare test 4 variables: identity
rem 8.declare test 5 variables: accessibility
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

rem 1.
Set fil_scriptname=%~n0
Set fil_scriptpath=%~dp0
Set fil_fullscriptname=%0
Set fil_systemlist=%1
set fol_backups=%~dp0backups
set fol_tools=%~dp0tools
set fol_standard=%~dp0logs\standard
Set fil_backup=%fol_backups%\zz_backup_%fil_scriptname%_%1
Set fil_log=zz_log_%fil_scriptname%.log
Set fil_launchedby_runasspc=%fil_scriptpath%specific.cmd

rem 2.
call %pth_standard%\2a.declare_variables_nontests.cmd

rem 3.
set fol_scriptrundatetime=%fol_standard%\%scriptdate%_%scripttime%
Set fil_retry="%fol_scriptrundatetime%"\zz_RETRY.txt

rem 4.
call %pth_standard%\2b.declare_variables_test_1.location.cmd

rem 5.
call %pth_standard%\2b.declare_variables_test_2.status.cmd

rem 6.
call %pth_standard%\2b.declare_variables_test_3.availability.cmd

rem 7.
call %pth_standard%\2b.declare_variables_test_4.identity.cmd

rem 8.
call %pth_standard%\2b.declare_variables_test_5.accessibility.cmd


::mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
::SCRIPTSTART	SCRIPTSTART	SCRIPTSTART	SCRIPTSTART	SCRIPTSTART	SCRIPTSTART	SCRIPTSTART	SCRIPTSTART	SCRIPTSTART
::mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
rem pre-loop section
rem 1.build script runtime banner
rem 2.continue/re-run or abort script execution by checking file size of system list
rem 3.create a script execution folder, retry file, and backup systemlist if not exist along with creating a master log file on first run.
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

rem 1.
call %pth_standard%\3a.preloop_build_script_banner.cmd

rem 2.
call %pth_standard%\3b.preloop_rerunorabort_scriptexecution.cmd

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
:PRELIMINARY_FOLDERFILEBUILDS
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

rem 3.
call %pth_standard%\3c.preloop_preliminary_folderfilebuilds.cmd


::mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
:LOOPSECTION
::mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

for /f %%c in (%1) do CALL :PROCESSSYSTEM %%c


::mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
::POSTLOOP	POSTLOOP	POSTLOOP	POSTLOOP	POSTLOOP	POSTLOOP	POSTLOOP	POSTLOOP	POSTLOOP
rem overwrite systemlist with retry file by moving retry file with failed test systems over the currently run/finished systemlist.
rem unset all variables if required
rem run script again so it runs continuously if applicable
::mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

call %pth_standard%\5.postloop.cmd


::mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
::LOOP	LOOP	LOOP	LOOP	LOOP	LOOP	LOOP	LOOP	LOOP	LOOP	LOOP	LOOP	LOOP	LOOP	LOOP	LOOP	LOOP	
::mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm


rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
:PROCESSSYSTEM
rem 1.reset certain vars each time through loop such as date and time vars for each system processed.
rem 2.test 1:location
rem 3.test 2:status
rem 4.test 3:availability
rem 5.test 4:identity
rem 6.test 5:accessibility
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

call %pth_standard%\4a.loop_redefine_eachtimethrough.cmd

:LOCATION
call %pth_standard%\4b.loop_test_1.location.cmd
:STATUS
call %pth_standard%\4b.loop_test_2.status.cmd
:AVAILABILITY
call %pth_standard%\4b.loop_test_3.availability.cmd
:IDENTITY
call %pth_standard%\4b.loop_test_4.identity.cmd
:ACCESSIBILITY
call %pth_standard%\4b.loop_test_5.accessibility.cmd


::mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
:END
::mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm


:ADDBLANKLINES
echo.

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
rem Write system to master log file:
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

echo %systemdatetime%,%1,%log_location%,%log_status%,%log_availability%,%log_identity%,%log_accessibility%,>>%fil_log%


rem mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
:UNSETVARIABLES
rem mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

call %pth_standard%\6.end_unset_variables.cmd


rem mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
:DELETETEMPFILES
rem mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
rem this will delete the %1_results.txt file each time through the loop so that the new system will be fresh.
if exist "%fol_scriptrundatetime%"\%1_results.txt del /Q "%fol_scriptrundatetime%\%1_results.txt
if exist "%fol_scriptrundatetime%"\testhost.txt del /Q "%fol_scriptrundatetime%"\testhost.txt



rem mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
:CLOSEEXITTHISSCRIPT
rem mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

rem echo.
rem sfk echo [red]To close this script, press a key...[def]
rem pause
rem exit



::mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
::SCRIPTEND	SCRIPTEND	SCRIPTEND	SCRIPTEND	SCRIPTEND	SCRIPTEND	SCRIPTEND	SCRIPTEND	SCRIPTEND
::mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm



======================================= child.cmd

@echo off

::aim:	check file size of %1 so that the script knows to stop once filesize is 0, 1 or 2 bytes, then if required, goto script schedule section.

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
rem check file size of %1:
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

IF DEFINED siz_systemlist set siz_systemlist=
for %%R in (%fil_systemlist%) do @set siz_systemlist=%%~zR
set /p LastLine=0.Checking if any systems remain in system list %fil_systemlist%..If size greater than 2 bytes, run script, if less, abort..<nul
for %%R in (%fil_systemlist%) do if %siz_systemlist% GTR 2 GOTO SCRIPTRUN
GOTO SCRIPTABORT

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
:SCRIPTABORT
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
set /p LastLine=file size of: %fil_systemlist% is: %siz_systemlist% bytes..SCRIPTRUN ABORTED!..<nul
echo.
%fol_tools%\sfk echo "[red]Press Control C to keep this window open by cancelling the script or any key to exit window..[def]"
echo.
pause
exit

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
:SCRIPTRUN
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
set /p LastLine=file size of %fil_systemlist% is: %siz_systemlist% bytes..SCRIPTRUN APPROVED!..<nul
echo.
GOTO SCRIPTSCHEDULE

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
:SCRIPTSCHEDULE
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
set /p LastLine=1.Determine script-execution frequency. If size of %fil_systemlist% is less than 100 bytes (eg.lt 10 systems), run every 6 hrs. If greater than this, run every hour..<nul
if %siz_systemlist% GTR 100 GOTO SCRIPTEVERY1HOUR
GOTO SCRIPTEVERY6HOURS

rem sleep <time to sleep in seconds>
rem 1 min = 60 sec
rem 1 hr = 3600 sec
rem 6 hrs = 21600 sec
rem 24 hrs = 86400 sec

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
:SCRIPTEVERY1HOUR
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

set /p LastLine=CONTINUING in 1 HR (%sleep_gtr10_systems% sec) FROM NOW!..<nul
echo.
%fol_tools%\sleep %sleep_gtr10_systems% >nul 2>&1
rem sleep 3600 >nul 2>&1
GOTO PRELIMINARY_FOLDERFILEBUILDS

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
:SCRIPTEVERY6HOURS
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

set /p LastLine=CONTINUING IN 6 HRS (%sleep_lt10_systems% sec) FROM NOW!..<nul
echo.
%fol_tools%\sleep %sleep_lt10_systems% >nul 2>&1
rem sleep 21600 >nul 2>&1
GOTO PRELIMINARY_FOLDERFILEBUILDS

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 26105910
Sorry bill, just assume that child.cmd is being called from main.cmd
0
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

 
LVL 59

Expert Comment

by:Bill Prew
ID: 26105912
Without seeing the current large script file it's hard to comment on how best to split it up.  One thought I did have though is you may want to consider breaking out the areas that are currently executed via a GOTO, place them in separate BAT files, and then instead of a GOTO do a CALL to them.

In general GOTO is not desired in a BAT file unless there is no other way to work around it.

If you want to share the large script you have now I'd be happy to try and make some specific recommendations.

~bp
0
 
LVL 1

Author Comment

by:Simon336697
ID: 26105932
bill, im not quite sure in what context you mean the calling and called script, and why use the exit command.

If my first script i run is main.cmd, which fires off child.cmd (eg. in main.cmd, there is a line saying:
call child.cmd), is main.cmd the calling script, and child.cmd the "called" script?

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26106128
Let's try a simple example of what I mean.  Attached is a listing of a main.cmd and a child.cmd.  At some point in main.cmd we make a call to the child.cmd file.  Notice in it we can return to main.cmd using the EXIT statement, and add the /B option to also return an errorlevel to the main.cmd script.  Then when control is regained in main.cmd right after the call, we can check the errorlevel returned and take different action depending on the value returned.  Does this make any more sense, if not we'll take another swing at it :-).

~bp
===================== main.cmd
.
.
.
call child.cmd
if errorlevel 1 goto :ErrorsFound
goto :Continue
.
.
.
:Continue 
.
.
.
:ErrorsFound
.
.
.
 
===================== child.cmd
.
.
.
exit /b 1
.
.
.
exit /b 0

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 26106206
Bill, you are brilliant, and that is so well explained.
I totally understand it now thanks to you.

Now, with my ridiculously long script, ill tackle it from your angle.

0
 
LVL 1

Author Comment

by:Simon336697
ID: 26106237
Bill can I ask you in your above example,

If in child.cmd, i had the following:

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
:SCRIPTEVERY1HOUR
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

set /p LastLine=CONTINUING in 1 HR (%sleep_gtr10_systems% sec) FROM NOW!..<nul
echo.
%fol_tools%\sleep %sleep_gtr10_systems% >nul 2>&1
rem sleep 3600 >nul 2>&1
GOTO PRELIMINARY_FOLDERFILEBUILDS

rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
:SCRIPTEVERY6HOURS
rem zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

set /p LastLine=CONTINUING IN 6 HRS (%sleep_lt10_systems% sec) FROM NOW!..<nul
echo.
%fol_tools%\sleep %sleep_lt10_systems% >nul 2>&1
rem sleep 21600 >nul 2>&1
GOTO PRELIMINARY_FOLDERFILEBUILDS




Now, because in the above, the PRELIMINARY_FOLDERFILEBUILDS
is actually a label in main.cmd, and not in child.cmd, this will fail.
So, bill, if i changed the

GOTO PRELIMINARY_FOLDERFILEBUILDS

to

EXIT /B 1000

and in main.cmd

call child.cmd
if errorlevel 1000 goto :PRELIMINARY_FOLDERFILEBUILDS
goto :Continue


:PRELIMINARY_FOLDERFILEBUILDS


should this work, and is this the way you would do it?
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26106396
Yes, that is the right idea.  

Bear in mind that "IF ERRORLEVEL 1000" will be true for all values of ERRORLEVEL greater than or equal to 1000.  In your case it might make more sense to use the following variation, which will check for an exact match to the value 1000.

if %errorlevel% == 1000 goto :PRELIMINARY_FOLDERFILEBUILDS

Can I ask where in main.cmd you will be calling child.cmd from?

~bp
0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 160 total points
ID: 26107421
As long as you do not use setlocal in any "child" batch file, you can set an environment variable for status instead of the somehow tricky errorlevel.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26107507
> Qlemo

> As long as you do not use setlocal in any "child" batch file, you can set an environment variable
> for status instead of the somehow tricky errorlevel.

Yes, but then you need 2 statements instead of 1 to return :-).

(I feel obliged to say that since I often get beat up for erroring on the side of being wordy in my batch scripts for readability rather than terse and compact).

> Simon336697

Attached is an example of what Qlemo is mentioning, it's just a slightly different way to get the same result.  

~bp
===================== main.cmd
.
.
.
call child.cmd
if "%ReturnCode%" == "Errors" goto :ErrorsFound
goto :Continue
.
.
.
:Continue 
.
.
.
:ErrorsFound
.
.
.
 
===================== child.cmd
.
.
.
set ReturnCode=Errors
exit /b
.
.
.
set ReturnCode=Normal
exit /b

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 26107944
Hi guys, you are both champions.

Bill, with regards to where in main.cmd I would be calling child.cmd, think of the following *test*.cmd's as all potential candidates for child.cmd

:LOCATION
call %pth_standard%\4b.loop_test_1.location.cmd
:STATUS
call %pth_standard%\4b.loop_test_2.status.cmd
:AVAILABILITY
call %pth_standard%\4b.loop_test_3.availability.cmd
:IDENTITY
call %pth_standard%\4b.loop_test_4.identity.cmd
:ACCESSIBILITY
call %pth_standard%\4b.loop_test_5.accessibility.cmd

Basically, in main.cmd, main.cmd will take 1 argument, that is a text file of computer names, and loop through each one in that text file, and perform tests against it.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26107980
Okay, sounds like you have some decent options then, let me know if you run into trouble.  Thanks for the points, glad I was able to help.

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26110842
One other option.... which may be appropriate (though I know you have your answer as such) is:

master.cmd:

@echo off
if not "%1"=="" goto %1
child1.cmd
echo After child1.cmd
exit /b

:part2
 echo Now in part2

Then in child1.cmd return to the master with:
master.cmd part2

The above just runs:

Now in child1.cmd
Now in part2

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26110853
lost the last bit of text there.... was saying along the lines of If you don't use call then the calling batch file stops and changes control to the new batch only.  You can then run master.cmd with a command line option of the line label you want it to start again from....

Frankly a combination of these different techniques work in different circumstances.

Steve
0
 
LVL 1

Author Comment

by:Simon336697
ID: 26111751
Thanks dragon-it, really appreciate it.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26111997
Steve,

Interesting approach.  The software engineer in me took a little exception to it at first read, but given the occasional departure of command scripts from a pure programming language here and there that is another useful tool to have.

My initial reaction is that this approach is a bit less like "calling a function" in the way it feels, and I said to myself "oh gosh, I lose all context in the calling (main) script".  But the reality of it is the only real context that's typically useful is in environment variables, and as long as none of the script involved use setlocal then we are good.

Actually, does "setlocal enabledelayedexpansion" count as a setlocal, if so that pops up here and there.

Also, I wonder if either approach works better or worse if we try to spawn the child script from within a "block" in the main code, like a FOR of IF code body?  I'll have to do some experimenting on that.

Anyway, just rambling a bit, but thanks for the "post points" addition, I always like to hear of different approaches and this one (as you say) can have some good merit depending on the situation and nature of the scripts involved.

Hope the family is well (including the little one) and you have a great holiday.

~bill
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26112059
Just waiting for a comment from t0t0 about programming style :-)
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26112099
Heha, I see those bruises are still sensitive...

:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question