Batch Script - CALL another batch script to call line to perform certain command
Is it possible to create a batch file with a bunch of commands (commands.bat)
ECHO HELLO
ECHO HOLA
ECHO KONICHIWA
ECHO ANYONGHASEYO
ECHO BONJOUR
, then with开发者_JAVA百科in a different batch file, CALL commands.bat
and only perform the command on line 2 or line 4 without knowing what is on those lines?
Here's what I meant in my comment.
master.bat
echo abcd
echo hello
echo notepad
echo public
echo wind
echo balance
command.bat
@echo off
more +3 master.bat > temp.bat & temp.bat
Prints the below for me.
public
wind
balance
To start off from the first line, use +0
.
If you want this number to be sent via command line, here is a slightly modified version:
command.bat
@echo off
more +%1 master.bat > temp.bat & temp.bat
You can run the above with commands such as command.bat 0
or command.bat 3
.
A neat little trick I used to know (back when batchfiles were in vogue)
SET JUMPTO=HOLA
goto BRANCH_%JUMPTO%
:BRANCH_HELLO
echo HELLO
GOTO :QUIT
:BRANCH_HOLA
echo HOLA
GOTO :QUIT
:BRANCH_KONICHIWA
echo KONICHIWA
GOTO :QUIT
:BRANCH_ANYONGHASEYO
echo ANYONGHASEYO
GOTO :QUIT
:BRANCH_BONJOUR
echo BONJOUR
GOTO :QUIT
:BRANCH_
echo Illegal branch?!
:QUIT
It becomes more intesting when you replace the first line with e.g. SET JUMPTO=%1
Some test output:
E:>.\test.cmd HELLO
E:\>SET JUMPTO=HELLO E:\>goto BRANCH_HELLO E:\>echo HELLO HELLO E:\>GOTO :QUIT
E:>.\test.cmd
E:\>SET JUMPTO= E:\>goto BRANCH_ E:\>echo Illegal branch?! Illegal branch?! E:\>
Give me your input on this as a solution. It works, but I know some people don't like piping the FIND command to anything =/
REM Contents of COMMANDS.BAT
ECHO HELLO & ::1
ECHO HOLA & ::2
ECHO KONICHIWA & ::3
ECHO ANYONGHASEYO & ::4
ECHO BONJOUR & ::5
REM Command to perform ECHO KONICHIWA out of COMMANDS.BAT
CALL C:\COMMANDS.BAT | FIND "3"
Better yet, I discovered this playing around with it yesterday:
REM Contents of COMMANDS.BAT
ECHO HELLO & ::1
ECHO HOLA & ::2
ECHO KONICHIWA & ::3
ECHO ANYONGHASEYO & ::4
ECHO BONJOUR & ::5
-
REM Command to perform ECHO KONICHIWA out of COMMANDS.BAT
FINDSTR ::3 COMMANDS.BAT | START /B
That way I don't have to output the line to another bat file, it just runs the command instantly.
This is similar to Mechaflash's answer but makes use of findstr
instead of find
.
master.bat
echo abcd &rem line1
echo hello &rem line2
echo notepad &rem line3
echo public &rem line4
echo wind &rem line5
echo balance &rem line5
command.bat
@echo off
findstr line%1 master.bat > temp.bat & temp.bat
精彩评论