Incorporate a 2nd Batch into Main Batch Script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Incorporate a 2nd Batch into Main Batch Script

#1 Post by booga73 » 29 Dec 2013 12:09

Is there a way to incorporate the batch file, ShutDwnCnt1.bat, into my main batch file without having a 2nd batch file?

How could a batch file be included into a batch script that already has a main subroutine? Is there a limit to how many subroutines there can be?

this command calls the ShutDwnCnt1.bat and passes 2 variables to the ShutDwnCnt1.bat batch script.

start C:\Users\%username%\Documents\MyScripts\ShutDwnCnt1.bat %cpuname% %var1%



ShutDwnCnt1.bat file consists of the following code:

Code: Select all


@ECHO OFF
title Shutdown %cpuname% --^> %var1% Timer!!

@ECHO OFF & setlocal enabledelayedexpansion
set "StatusBar=* 4 * * * * * * * * * * * * 3 * * * * * * * * * * * * * * * 2 * * * * * * * * * * * * * * 1 * * * * * * Rebooting...0"
set /a count=0
:cycle
cls
set /A count+=1
SET "progressbar=!StatusBar:~0,%count%!"
set /a myCount=118-%count%
:: echo %count% %progressbar%
echo %mycount% %progressbar%
ping -n 3 127.0.0.1 > nul 2>&1
if %count% NEQ 118 goto :cycle

exit





Main Program Consists of the following:

Code: Select all


:01BeginRDP
Echo Press Any Key to Begin RDP to Machines Online.
echo.
pause
for /f "tokens=1-4 delims=. " %%a in (C:\Users\%UserName%\Documents\MyScripts\AliveIP.txt) do (
set var1=%%a.%%b.%%c.%%d
echo Calling Client Station: %var1%
ping -n 1 %var1% | find "Reply from" & call :1Routine %1
)


echo Sys Admin RDP Session Completed.
pause
exit

:1Routine %1

.  . .. instruction1

.  . .. instruction2

.  . .. instruction3

. . .etc

shutdown /r /m \\%var1% /t 240 /f /c "Sys Admin to connect Remotely. Computer needs Software Maintenance. Save your work. Sys Admin Thanks You!" > C:\Users\%UserName%\Documents\MyScripts\shtdwnErrorSession.txt 2>&1 & start C:\Users\%username%\Documents\MyScripts\ShutDwnCnt1.bat %cpuname% %var1%


.  . .. more instruction. . .

exit /b


Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Incorporate a 2nd Batch into Main Batch Script

#2 Post by Squashman » 29 Dec 2013 13:42

Is is no different then the code you are using to call a label in your existing batch file. Just copy the code into you main batch file and put a label before it.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Incorporate a 2nd Batch into Main Batch Script

#3 Post by penpen » 29 Dec 2013 13:58

booga73 wrote:Is there a way to incorporate the batch file, ShutDwnCnt1.bat, into my main batch file without having a 2nd batch file?
I'm not sure if i understand it right (i hope you don't want to let the programs run in parallel affecting each other):
Just compose the two batch files to one,
adding encapsulating label, goto :eof,
replacing all calling to sub.bat, and
rename labels used in both batch files:

Code: Select all

:main
:: copy pasted code of main.bat part 1
:: replaced "call sub.bat param1 param2 ..."
call :sub param1 param2 ...
:: replaced label used in sub.bat, too: ":label".
:mLabel
:: copy pasted code of main.bat part 2
goto :eof

:sub
:: copy pasted code of sub.bat part 1
:: replaced label used in main.bat, too: ":label".
:sLabel
:: copy pasted code of sub.bat part 2
goto :eof
(I hope i haven't missed something.)


booga73 wrote:How could a batch file be included into a batch script that already has a main subroutine?
Just rename the name of labels to unique ones.

booga73 wrote:Is there a limit to how many subroutines there can be?
As every subroutine needs at least one unique label to be callable,
this limit equals to the number of different labels, that can be created within a batch script.
I'm not sure if this limitation hasn't exceeded, but if not, then:
-Only the first 8 chars (after the colon) of a label are used, all further characters are ignored to define a label.
-There is no differentiation between uppercase and lowercase letters.
- Allowed letters are alphanumerical and "_".
So there is an upper limit of |{0:9,A:Z,_}|^8 = 37^8 = 3.512.479.453.921 subroutines in one batch files
(i think this should suffice for most purposes :D ).

penpen

Edit: Squashman was faster.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Incorporate a 2nd Batch into Main Batch Script

#4 Post by booga73 » 29 Dec 2013 21:19

Pretty much, the ShutDwnCnt1.bat routine would be called and continued processing of the main script would be immediately picked up.

I'll look into it, thank you for all the feed back; I'll let you folks know my progress! :)

I wanted to find out where to place shutdwncnt1.bat in the script; I'll follow PenPen's illustration.

v/r Booga73

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Incorporate a 2nd Batch into Main Batch Script

#5 Post by Squashman » 30 Dec 2013 10:39

booga73 wrote:Pretty much, the ShutDwnCnt1.bat routine would be called and continued processing of the main script would be immediately picked up.

I'll look into it, thank you for all the feed back; I'll let you folks know my progress! :)

I wanted to find out where to place shutdwncnt1.bat in the script; I'll follow PenPen's illustration.

v/r Booga73


My logic is to put all my functions that I call at the end of the batch file. Just like Penpen has illustrated.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Incorporate a 2nd Batch into Main Batch Script

#6 Post by booga73 » 30 Dec 2013 12:36

here are my results.. ..

I believe I'm better off to keep a 2nd batch file for this purpose too. . .

but. . .

I did figure out where to put my 2nd batch subroutine and how to call it. See below for basic illustration of the script.

But, since the subroutine is called and needs to do a timer count down of 4 minutes; when instructed to, the function is called but gets hooked into doing the count down then finally returns back to main script. I would prefer to call the function, then function launches into another command windows at the same time the main script continues; I think that's too much of a burden for batch script - a limitation. :shock:

thank you though for the illustrations.

I tried using google foo to search for an option to do so but stood up to said enough is enough. . . :roll:

v/r Booga73

Code: Select all


:01BeginRDP
Echo Press Any Key to Begin RDP to Machines Online.
echo.
pause
for /f "tokens=1-4 delims=. " %%a in (C:\Users\%UserName%\Documents\MyScripts\AliveIP.txt) do (
set var1=%%a.%%b.%%c.%%d
echo Calling Client Station: %var1%
ping -n 1 %var1% | find "Reply from" & call :1Routine %1
)


echo Sys Admin RDP Session Completed.
pause
exit

:1Routine %1

.  . .. instruction1

.  . .. instruction2

.  . .. instruction3

. . .etc


shutdown /r /m \\%var1% /t 240 /f /c "Sys Admin to connect Remotely. Computer needs Software Maintenance. Save your work. Sys Admin Thanks You!" > C:\Users\%UserName%\Documents\MyScripts\shtdwnErrorSession.txt 2>&1 & Call :ShtDnCn1 %cpuname% %var1%


.  . .. more instruction. . .

exit /b

:ShtDnCn1

title Shutdown %cpuname% --^> %var1% Timer!!

@ECHO OFF & setlocal enabledelayedexpansion
set "StatusBar=* 4 * * * * * * * * * * * * 3 * * * * * * * * * * * * * * * 2 * * * * * * * * * * * * * * 1 * * * * * * Rebooting...0"
set /a count=0
:cycle
cls
set /A count+=1
SET "progressbar=!StatusBar:~0,%count%!"
set /a myCount=118-%count%
:: echo %count% %progressbar%
echo %mycount% %progressbar%
ping -n 3 127.0.0.1 > nul 2>&1
if %count% NEQ 118 goto :cycle

exit /b


Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Incorporate a 2nd Batch into Main Batch Script

#7 Post by Squashman » 30 Dec 2013 12:52

Then you would need to use the START command to keep launching another cmd shell to run your shutdown batch file. If you had 100 computers on your network you would end up with 100 CMD shells opened up on your computer.

I think a better option for you would be to use PSSHUTDOWN. That way you could specify the wild card to shutdown all computers within the domain or you can create a list of computers in a text file and use that list with psshutdown.
http://technet.microsoft.com/en-us/sysi ... 97541.aspx

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Incorporate a 2nd Batch into Main Batch Script

#8 Post by booga73 » 30 Dec 2013 14:19

thank you all for your help in guiding me to the right direction with the setup, I didn't know where to put my routine, but with the illustration, good to go.

I don't want to reboot all workstations on the network; this has to be done 1 at a time. Even with that, give the tech an option to stop the shutdown of remote computer.

I believe I figured this out; :P :P here's my solution. The idea was to take my wanted batch script and set it as a routine within my script. But the trouble was that when I call this batch file, the batch script is a count down timer which needs to work with the shutdown command of 4 minute count down. For example, once the shutdown initiates, the count down timer needs to start, both virtually at the same time.


I figured I could do the standard call procedure to call for the subroutine and still pass my needed 2 variables to the title section of my script.

ie: echo title Shutdown Timer: %cpuname% %var1% ^!^! > C:\Users\%username%\Documents\MyScripts\tmp\test1.bat

Esscentially, I took my subroutine and going to parse it out to a new batch file in the tmp sub folder, named test1.bat. Then

I had to watch out for reserved characters %, >, < ! and use the ^ carrot symbol to help parse and for the presence of %, use double %%.

This can be done; I will use it; as long as the subroutine called is NOT too long but keep it like less than 12 to 15 lines, running the script won't be a problem.

Subroutine to be called:

Code: Select all


:ShtDnCn1
echo title Shutdown Timer: %cpuname% %var1% ^!^! > C:\Users\%username%\Documents\MyScripts\tmp\test1.bat

echo ^@ECHO OFF ^& setlocal enabledelayedexpansion >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo set "StatusBar=* 4 * * * * * * * * * * * * 3 * * * * * * * * * * * * * * * 2 * * * * * * * * * * * * * * 1 * * * * * * Rebooting...0" >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo set /a count=0 >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo :cycle >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo cls >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo set /A count+=1 >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo SET "progressbar=^!StatusBar:~0,%%count%%^!" >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo set /a myCount=118-%%count%% >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo echo %%mycount%% %%progressbar%% >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo ping -n 3 127.0.0.1 ^> nul 2^>^&1 >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo if %%count%% NEQ 118 goto :cycle >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat

start C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
exit /b



Code: Select all


:01BeginRDP
Echo Press Any Key to Begin RDP to Machines Online.
echo.
pause
for /f "tokens=1-4 delims=. " %%a in (C:\Users\%UserName%\Documents\MyScripts\AliveIP.txt) do (
set var1=%%a.%%b.%%c.%%d
echo Calling Client Station: %var1%
ping -n 1 %var1% | find "Reply from" & call :1Routine %1
)


echo Sys Admin RDP Session Completed.
pause
exit

:1Routine %1

.  . .. instruction1

.  . .. instruction2

.  . .. instruction3

. . .etc


shutdown /r /m \\%var1% /t 240 /f /c "Sys Admin to connect Remotely. Computer needs Software Maintenance. Save your work. Sys Admin Thanks You!" > C:\Users\%UserName%\Documents\MyScripts\shtdwnErrorSession.txt 2>&1 & Call :ShtDnCn1 %cpuname% %var1%


.  . .. more instruction. . .

exit /b
:ShtDnCn1
echo title Shutdown Timer: %cpuname% %var1% ^!^! > C:\Users\%username%\Documents\MyScripts\tmp\test1.bat

echo ^@ECHO OFF ^& setlocal enabledelayedexpansion >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo set "StatusBar=* 4 * * * * * * * * * * * * 3 * * * * * * * * * * * * * * * 2 * * * * * * * * * * * * * * 1 * * * * * * Rebooting...0" >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo set /a count=0 >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo :cycle >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo cls >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo set /A count+=1 >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo SET "progressbar=^!StatusBar:~0,%%count%%^!" >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo set /a myCount=118-%%count%% >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo echo %%mycount%% %%progressbar%% >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo ping -n 3 127.0.0.1 ^> nul 2^>^&1 >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo if %%count%% NEQ 118 goto :cycle >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat

start C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
exit /b


Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Incorporate a 2nd Batch into Main Batch Script

#9 Post by Squashman » 30 Dec 2013 14:26

This code should never work as it needs Delayed Expansion.

Code: Select all

for /f "tokens=1-4 delims=. " %%a in (C:\Users\%UserName%\Documents\MyScripts\AliveIP.txt) do (
set var1=%%a.%%b.%%c.%%d
echo Calling Client Station: %var1%
ping -n 1 %var1% | find "Reply from" & call :1Routine %1
)


Not sure why you are passing %1 to your subroutine either and then specifying it with your label for the sub routine.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Incorporate a 2nd Batch into Main Batch Script

#10 Post by booga73 » 30 Dec 2013 14:49

Squashman,

Honest, I for all intentions, I thought I wrote the command:

ping -n 1 %var1% | find "Reply from" & call :1Routine %1

which would carry the value of %var1% into my routine through %1. Such as, %var1% value is now held in %1, and inside the :1Routine, the value %1 is used to reference my value of %var1%. That's how I understood the use of what was written.

When you go through the logical, sequentcial, interpretation, as a more valued look, can I get your professional insight on what it means? Would I need to use %1? :?:

v/r Booga73

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Incorporate a 2nd Batch into Main Batch Script

#11 Post by Squashman » 30 Dec 2013 15:01

%1 is a value that is passed to a batch file from the command line or to subroutine. But since you are referencing %var1% in your subroutine you don't even need to pass it because the subroutine inherits all your variables. I would also assume you would only want to call the subroutine if the previous command completed successfully which means you would want to use a double ampersand.

Your code should look more like this.

Code: Select all

@echo off & setlocal enabledelayedexpansion
:01BeginRDP
Echo Press Any Key to Begin RDP to Machines Online.
echo.
pause
for /f "tokens=1-4 delims=. " %%a in (C:\Users\%UserName%\Documents\MyScripts\AliveIP.txt) do (
set var1=%%a.%%b.%%c.%%d
echo Calling Client Station: !var1!
ping -n 1 !var1! | find "Reply from" && call :1Routine
)


echo Sys Admin RDP Session Completed.
pause
exit

:1Routine

.  . .. instruction1

.  . .. instruction2

.  . .. instruction3

. . .etc


shutdown /r /m \\%var1% /t 240 /f /c "Sys Admin to connect Remotely. Computer needs Software Maintenance. Save your work. Sys Admin Thanks You!" > C:\Users\%UserName%\Documents\MyScripts\shtdwnErrorSession.txt 2>&1 & Call :ShtDnCn1 %cpuname% %var1%


.  . .. more instruction. . .

exit /b
:ShtDnCn1
echo title Shutdown Timer: %cpuname% %var1% ^!^! > C:\Users\%username%\Documents\MyScripts\tmp\test1.bat

echo ^@ECHO OFF ^& setlocal enabledelayedexpansion >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo set "StatusBar=* 4 * * * * * * * * * * * * 3 * * * * * * * * * * * * * * * 2 * * * * * * * * * * * * * * 1 * * * * * * Rebooting...0" >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo set /a count=0 >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo :cycle >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo cls >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo set /A count+=1 >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo SET "progressbar=^!StatusBar:~0,%%count%%^!" >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo set /a myCount=118-%%count%% >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo echo %%mycount%% %%progressbar%% >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo ping -n 3 127.0.0.1 ^> nul 2^>^&1 >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
echo if %%count%% NEQ 118 goto :cycle >> C:\Users\%username%\Documents\MyScripts\tmp\test1.bat

start C:\Users\%username%\Documents\MyScripts\tmp\test1.bat
exit /b

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Incorporate a 2nd Batch into Main Batch Script

#12 Post by booga73 » 30 Dec 2013 15:25

That's awesome to know.

I did this code snippet to know which workstations I couldn't provide software remediation because I believe the dead IP list consists of laptops or desktops that are not powered on or are off the network.

Here's the code that I use to actually find a good or bad IP and sift through to make a good LIVE IP list to RDP to.

Code: Select all


for /f %%i in (C:\Users\%UserName%\Documents\MyScripts\IPList.txt) do call :FindAliveIP
echo.
Echo AliveIP ^& DeadIPs' Identified. Now onto RDP Software Remediation.
goto 01BeginRDP
 
:FindAliveIP
set state=alive
ping -n 1 %1 | find /i "bytes=" || set state=dead
If "%state%"=="alive" echo %1 >> C:\Users\%UserName%\Documents\MyScripts\AliveIP.txt
If "%state%"=="dead" echo %1 >> C:\Users\%username%\Documents\MyScripts\DeadIP.txt
exit /b



All this work is tying into a 41k batch script that I will eventually post. I had some great favorable output from testing my script the other evening in its entirety. I work remotley during the evenings on small projects which I at times need to connect remotely to a workstation while a customer is not there and provide software fix or so; that's the only time that is alloted in the evenings to do this work.

Thank you for all your help and insight.

v/r Booga73

Post Reply