How to run batch with task from inside another batch?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SG-1
Posts: 2
Joined: 02 Oct 2013 13:39

How to run batch with task from inside another batch?

#1 Post by SG-1 » 02 Oct 2013 13:49

Hi, new to this site and love all the help given to people here :P

My question is this:

I have two batch files

I have one running at my comps startup and I want it to run my second batch

So was wondering if I can call the second batch from the first

I was also wondering if from the first batch I can run an task that will run the second batch instead of second batch being called by first batch

My concern is that the second batch must run at "highest" parameters which is why I want to use a task to run the second batch

Thanks for any assistance

:mrgreen:

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

Re: How to run batch with task from inside another batch?

#2 Post by penpen » 02 Oct 2013 16:31

SG-1 wrote:So was wondering if I can call the second batch from the first
Yes, you can:

Code: Select all

rem content of first.bat
call second.bat

SG-1 wrote:I was also wondering if from the first batch I can run an task that will run the second batch...
This is possible, too.

Code: Select all

rem content of first.bat
start "" second.bat

SG-1 wrote:My concern is that the second batch must run at "highest" parameters which is why I want to use a task to run the second batch
I'm not sure what you mean, maybe this:

Code: Select all

rem content of first.bat
start "" /REALTIME second.bat

penpen

SG-1
Posts: 2
Joined: 02 Oct 2013 13:39

Re: How to run batch with task from inside another batch?

#3 Post by SG-1 » 02 Oct 2013 20:34

penpen wrote:
SG-1 wrote:So was wondering if I can call the second batch from the first
Yes, you can:

Code: Select all

rem content of first.bat
call second.bat

SG-1 wrote:I was also wondering if from the first batch I can run an task that will run the second batch...
This is possible, too.

Code: Select all

rem content of first.bat
start "" second.bat

SG-1 wrote:My concern is that the second batch must run at "highest" parameters which is why I want to use a task to run the second batch
I'm not sure what you mean, maybe this:

Code: Select all

rem content of first.bat
start "" /REALTIME second.bat

penpen


Thank you penpen

I think call will work fine but will keep start in mind :wink:

My situation is that I have a large batch file that does a backup and restore but only needs the restore if what gets restored is gone, and it requires user interaction if restore is needed so it can't run silent :(

And so I wanted to split the batch file so that the backup part (1.bat) will be run silently with a task that has switches to run first batch file silently like I think this does

Code: Select all

schtasks /create /tn "Task1" /tr "%SystemDrive%\One\One1\1.bat" /sc onstart /ru ""


And if a restore is needed then the Restore part (2.bat) will be called by 1.bat

My question is when my first batch calls my second batch will it will run as administrator or another user or system level?

And I will use this call in 1.bat to call 2.bat

Code: Select all

call 2.bat


Also was wondering how to write this correctly regarding the & goto exit part

Code: Select all

echo if %%errorlevel%%==0 call 2.bat & goto :Exit >>%SystemDrive%\One\One1\1.bat


After I call 2.bat I want 1.bat to close

Thank you

:D

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

Re: How to run batch with task from inside another batch?

#4 Post by penpen » 03 Oct 2013 02:26

SG-1 wrote:And so I wanted to split the batch file so that the backup part (1.bat) will be run silently with a task that has switches to run first batch file silently like I think this does

Code: Select all

schtasks /create /tn "Task1" /tr "%SystemDrive%\One\One1\1.bat" /sc onstart /ru ""


And if a restore is needed then the Restore part (2.bat) will be called by 1.bat

My question is when my first batch calls my second batch will it will run as administrator or another user or system level?
I have no experiences starting batch scripts using schtasks.
But if it's set to silent (i think you mean that the shell is not visible), i'm not sure that call will 'unhide' the shell, so you may be have to use start.
The called batch script should run at the same level as the starting batch script (no matter if you use call or start).
But you should test both first without the full functionality; for example:
- 1.bat just calls 2.bat
- 2.bat writes to a folder that requires specific user rights.

SG-1 wrote:Also was wondering how to write this correctly regarding the & goto exit part

Code: Select all

echo if errorlevel%%==0 call 2.bat & goto :Exit >>%SystemDrive%\One\One1\1.bat

If you just want to echo if ... :Exit redirected to the given 1.bat, then this may be what you want to do:

Code: Select all

echo if errorlevel%%==0 call 2.bat ^& goto :Exit >>%SystemDrive%\One\One1\1.bat

penpen

Post Reply