Sampath
Sampath

Reputation: 21

Run commands in a batch file which is inside another batch file

How to run commands in a batch file which is inside another batch file......

I am trying to run commands in different console other than command prompt in a batch file but not able to do so.I am able to start the other console in batch file but not able to pass commands on to it.

Upvotes: 2

Views: 10852

Answers (3)

dbenham
dbenham

Reputation: 130819

My first interpretation of the question led me to believe that Sampath wanted one batch script that has two sets of commands. Calling it would run the 1st set of commands in the parent window, and a second window would open that would run the same script with thd 2nd set of commands.

"%~f0" will give the full path to the currently executing batch script. A simple command line argument serves as a switch to determine which code to run.

@echo off
if "%~1"==":PART2" goto %~1

::use this line if 2nd window is to remain open upon completion
::start "%~f0" :PART2

::use this line if 2nd window is to close upon completion
start cmd /c "%~f0" :PART2

echo Test parent output
pause
exit /b

:PART2
echo Test child output
pause
exit /b

Andriy M suggests Sampath wants to be able to dynamically send commands to the 2nd window. This can be done with 2 scripts that I will call master.bat and slave.bat.

The slave.bat simply reads commands from stdin and executes them. The master.bat launches the slave with input redirected to a command file and then appends commands to the command file.

Here is an example of master.bat that demonstrates dymamically sending commands to the slave. Note that the master prompts for a command, but the slave window will have the focus. Make sure you click on the master so you can enter the command of your choice.

@echo off

:: create an empty command file
type nul >cmds.txt

:: start the slave with input redirected to the command file
start slave.bat ^<cmds.txt

:: issue some commands by appending them to the command file
>>cmds.txt echo echo command 1
>>cmds.txt echo echo command 2
>>cmds.txt echo echo(
>>cmds.txt echo rem /?

:: ask for a command to send to the slave
set /p "cmd=Enter a command to be sent to the slave: "

:: send the command
>>cmds.txt echo %cmd%

::pause so we can see the results in the slave window
for /l %%n in (1 1 1000000) do rem

::tell the slave to exit
>>cmds.txt echo exit

And here is the slave.bat

@echo off
:top
set "cmd="
set /p "cmd="
%cmd%
goto :top

Upvotes: 4

Anthony Miller
Anthony Miller

Reputation: 15921

It almost sounds like what you want is a file that holds commands that you want to run, and to use a batch script to call on those commands when you want?

I've implemented this by creating a batch file that holds all the commands (code snippets) that I find useful, and then using my other batch scripts to call on that "master" file for my snippets.

For example, in my MASTER_BAT.BAT file, an example of a snippet to create dates in different format for usage look like this:

GOTO:%~1

:GET_CURRENT_DATE
:: Created: 1/19/2012
:: Creates variables for the date format in different forms.
:: No additional arguments required
SET DISABLED=0
IF [%DISABLED%] == [1] GOTO:EOF
:: Created: 11/30/11
:: Creates date formats.
Set mdy=%date:~4,2%-%date:~7,2%-%date:~12,4%
Set mdY=%date:~4,2%-%date:~7,2%-%date:~10,4%
Set Dmdy=%date:~0,4%%date:~4,2%-%date:~7,2%-%date:~12,4%
Set DmdY=%date:~0,4%%date:~4,2%-%date:~7,2%-%date:~10,4%
Set ymd=%date:~12,4%-%date:~4,2%-%date:~7,2%
Set ymd=%date:~10,4%-%date:~4,2%-%date:~7,2%

GOTO:EOF

And in my CHILD_BAT.BAT, I want to use that snippet to create the date formats... lets say I want to make it so that I can call the date by the current date in mm/dd/yy format:

CALL MASTER_BAT.BAT "GET_CURRENT_DATE"
ECHO %mdy%
PAUSE

Your output for CHILD_BAT.BAT would be:

1-23-12
Press any key to continue...

Also, any variables created in your CHILD_BAT.BAT prior to the CALL command will be passed to the MASTER_BAT.BAT script as well. However, for loop interation that includes a CALL will not pass the for loop temporary variable.

Hope this is helpful.

EDIT: Note that my snippet is usable for the U.S. date format.

Upvotes: 0

CR0SS0V3R
CR0SS0V3R

Reputation: 336

You could try a call statement:

call batchname.bat

this will run the specified batch file in the current open prompt

Upvotes: 0

Related Questions