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?

5
  • Yes, make echo Hola and echo Bonjour conditional, dependant on a certain argument and while calling commands.bat, send in that argument. Commented Sep 15, 2011 at 14:46
  • commandwindows.com/batchfiles-branching.htm Commented Sep 15, 2011 at 14:48
  • Edited my question. Left out an important detail: calling them by line number and not by what their string contains Commented Sep 15, 2011 at 14:54
  • @sehe I'm not sure what you were trying to point me towards even with the original question. Commented Sep 15, 2011 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. Commented Sep 15, 2011 at 15:01

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If you want to just execute the nth line and not n-through-EOF, you might want to take a look at stackoverflow.com/questions/6409869/… and combine it with my answer to get what you want.
I'm gonna use Jeb's answer from your linked post. Setting answer.
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:\> 

Comments

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" 

1 Comment

It's a great idea! I will add another answer which is similar to what you have done.
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.

Comments

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.