Batch that creates one inside another

May 13, 2010 at 09:00:49
Specs: Windows 7 Ultimate, 4GB DDR2
I know this may seem very stupid, but I haven't found the answer anywhere googling nor in this site so here it goes: Is it possible to create a batch that creates another that creates another batch? For occasions of only in a certain time?


ECHO @ECHO OFF > batch1.bat
ECHO COMMAND >> batch1.bat
ECHO IF %time% EQU 10:00 goto create
ECHO exit >> batch1.bat
ECHO :create >> batch1.bat
ECHO @ECHO OFF >> batch1.bat > batch2.bat
ECHO COMMAND >> batch1.bat >> batch2.bat
ECHO del /Q %0 >> batch1.bat >> batch2.bat
ECHO exit >> batch1.bat

del /Q %0

::If you haven't noticed I'm trying to do this because original batch is deleted.

See More: Batch that creates one inside another

Report •

May 13, 2010 at 09:30:18
I dunno win7 but in w2k %time% is 23:27:44.60 so it's unlikely to ever equHH:MM.

This bit will probably open a can of worms:

del /Q %0

Helping others achieve escape felicity


Report •

May 13, 2010 at 09:33:27

ECHO IF %time% EQU 10:00 goto create

doesn't work, to see why type at prompt


the if must compare substrings

ECHO IF "%time:~0,5%"=="10:00" goto create

More it is malpractice to delete the batch currently in execution. To know more on substring at prompt type set /?

Report •

May 13, 2010 at 17:44:20
Hello again and oh well this was supposed to be about the title so let's just forget about %time%.
And Mechanix2Go, what do you mean by "open a can of worms"??

So could someone tell me if something like this possible [in any other syntax] to do with a batch file??


ECHO @ECHO OFF > batch1.bat
ECHO COMMAND > batch2.bat >> batch1.bat
ECHO exit >> batch1.bat

call batch1.bat ::learned my lesson ;o

Well I could do this, but the command wouldn't work =[.
ECHO "COMMAND > batch2.bat" >> batch1.bat

Report •

Related Solutions

May 14, 2010 at 01:40:08
The statement I suggested (with double %) does the right job

ECHO IF "%%time:~0,5%%"=="10:00" goto create

while your

ECHO COMMAND > batch2.bat >> batch1.bat

is not allowed as you can't redirect stdout twice to different destinations with just ONE line.

The problem is you are trying too hard compared your scripting knowledge. To generate a script by another script is not a trivial job and needs deep scripting language experience including tricks and borderline behaviors.

I am Italian, but if I would drive a Ferrari I would kill someone and myself.

Report •

May 14, 2010 at 16:53:20
Hmm nice I'll try to use that time code later =].

I didn't say it had to be in ONE line, but yeah I thought it was a simple thing to do D=

If you know "deep scripting language" could you give me an example?

And wth was that Ferrari thing supposed to mean??

Report •

May 15, 2010 at 02:02:06
Just trivial if ONE line has to be directed twice

echo STATEMENT > batch2.bat
echo STATEMENT >> batch1.bat

You may be disappointed by the above code, but that is the easiest way to solve the problem.

If you however have to redirect twice a block of statements, more sophisticated coding is required to be planned according to the specific issue.

About the If "%time..." to bind a task to time-planned execution the standard way is to enable the system scheduler using the Windows GUI/Control Panel or the command line AT command.

Be aware you can get a quick though not friendly help by typing the command name followed by /? (e.g. AT /?). I strongly suggest you go on this facility even for commands you believe to know quite fine (as IF or SET). Surprise may be awesome.

Last but not least the "Ferrari" connection... I mean anybody must evaluate his/her own capabilities to improve them gradually. Human life is not (usually) ruled by laws of Quantum Physics, in other words jumps are not allowed, step by step is the rule.

Report •

May 15, 2010 at 13:23:12
Well yea I'm disappointed but I guess I'll have to go with that one for now.

Hmm interesting command [AT] and yeah cmd explained it simply enough.

I would never have guessed that, I thought you hated Ferraris at the first place [yea I suck at reading comprehension], but now I get it and I'll try to follow your advice, but hey:

I'm a truck driver and I would try to pilot an airplane if it wasn't so complicated.

If Bill Gates thought that way we wouldn't be here =P

Report •

May 16, 2010 at 02:25:37
The can of worms gets opened when attempting self-modifying code.

IVO covered you questions.

I wonder what COMMAND is trying to do in the bat that gets created.

Helping others achieve escape felicity


Report •

Ask Question