3

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 within 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?

Anthony Miller
  • 15,101
  • 28
  • 69
  • 98
  • Yes, make `echo Hola` and `echo Bonjour` conditional, dependant on a certain argument and while calling `commands.bat`, send in that argument. – adarshr Sep 15 '11 at 14:46
  • http://commandwindows.com/batchfiles-branching.htm – sehe Sep 15 '11 at 14:48
  • Edited my question. Left out an important detail: calling them by line number and not by what their string contains – Anthony Miller Sep 15 '11 at 14:54
  • @sehe I'm not sure what you were trying to point me towards even with the original question. – Anthony Miller Sep 15 '11 at 14:55
  • @Mechaflash You can do one thing. Have a master `commands.bat` which will only contain the list of `echo` statements. Depending on the line number (or range) you may receive, copy the contents from that line number till the end of file into a separate file, `temp.bat` and execute it. – adarshr Sep 15 '11 at 15:01

5 Answers5

2

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.

adarshr
  • 61,315
  • 23
  • 138
  • 167
  • 1
    If you want to just execute the nth line and not n-through-EOF, you might want to take a look at http://stackoverflow.com/questions/6409869/echo-the-nth-line-from-a-text-file-where-n-is-a-command-line-argument and combine it with my answer to get what you want. – adarshr Sep 15 '11 at 15:37
  • I'm gonna use Jeb's answer from your linked post. Setting answer. – Anthony Miller Sep 15 '11 at 16:28
1

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:\>
sehe
  • 374,641
  • 47
  • 450
  • 633
1

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"
Anthony Miller
  • 15,101
  • 28
  • 69
  • 98
1

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.

Anthony Miller
  • 15,101
  • 28
  • 69
  • 98
0

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
Community
  • 1
  • 1
adarshr
  • 61,315
  • 23
  • 138
  • 167