Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Generating one batch file from another  (Read 2415 times)

0 Members and 1 Guest are viewing this topic.

stdoubt

  • Guest
Generating one batch file from another
« on: September 21, 2009, 04:58:36 PM »
Folks,

Sorry for asking the question, as I am sure the answer is out there somewhere - I just don't know the right way to phrase this in a search:

I would like to run a batch file from a scheduled task to create a new batch file in the Startup folder and to reboot. The purpose of this is to force an interactive login and action some commands, in a completely hands off fashion.

Because their are several minor variations to the batch files I want to create, I currently have several similar files that I copy to Startup at different times. But this feels untidy. I would rather have the contents of the new files that I want to create stored "in stream" in the main file.

I think that this is possible in Linux Bash script using a "<<<" to an end point, but again, I can't find the details.

Again, sorry for the vague question - flame away  ;)

billrich

  • Guest
Re: Generating one batch file from another
« Reply #1 on: September 21, 2009, 05:32:33 PM »
one step at a time:

1)  create a batch that calls another batch. Get that working and then schedule with the at command.

2) Call all batch files from a main batch or call the the next batch from the last batch.  I believe a main batch offers better control and flow.

C:\>exit /?
Quits the CMD.EXE program (command interpreter) or the current batch
script.

EXIT [/B] [exitCode]

  /B          specifies to exit the current batch script instead of
              CMD.EXE.  If executed from outside a batch script, it
              will quit CMD.EXE

  exitCode    specifies a numeric number.  if /B is specified, sets
              ERRORLEVEL that number.  If quitting CMD.EXE, sets the process
              exit code with that number.

C:\>
« Last Edit: September 22, 2009, 01:56:53 PM by billrich »

billrich

  • Guest
Re: Generating one batch file from another
« Reply #2 on: September 21, 2009, 06:09:17 PM »

C:\>type main.bat
Code: [Select]
@echo off
call wait1.bat
echo this line will execute when wait1.bat is finished
call wait2.bat
echo this line will execute when wait2.bat is finished

Output:

C:\>main.bat
waitbat1 will sleep 1
this line will execute when wait1.bat is finished
waitbat2 will sleep 2
this line will execute when wait2.bat is finished

C:\>type  wait1.bat
Code: [Select]
echo waitbat1 will sleep 1

C:\batextra\sleep 1

exit /b

C:\>

billrich

  • Guest
Re: Generating one batch file from another
« Reply #3 on: September 22, 2009, 09:58:48 AM »

C:\>type generatebat.bat
Code: [Select]
@echo off

Rem We may generate a new batch file
Rem with redirect (>) and append (>>)


echo echo Hello >  newbat.bat
echo  date /T >> newbat.bat
echo  c:\batextra\sleep  3 >> newbat.bat
echo exit /b  >> newbat.bat


C:\>
C:\>type newbat.bat
Code: [Select]
echo Hello
 date /T
 c:\batextra\sleep  3
exit /b

Output:

C:\>newbat.bat

C:\>echo Hello
Hello

C:\>date /T
Tue 09/22/2009

C:\>c:\batextra\sleep  3

C:\>exit /b

C:\>