Batch commands from text file

March 5, 2013 at 20:55:27
Specs: Windows 7 Home Premium?
hey guys, i've been playing around with batch files and i want to know if it's possible to load commands out of text files, for example:

text file:
echo blah,blah,blah
timeout>nul 2 /nobreak
set /p input=
if /i "%input%"=="hi there" (
goto infinite_loop
) ELSE (
goto the_other_infinite_loop
)

:infinite_loop
echo hi
pause
goto infinite_loop

:the_other_infinite_loop
echo this is the other loop!
pause
goto the_other_infinite_loop

Apologies for the lack of info...


See More: Batch commands from text file

Report •


#1
March 5, 2013 at 21:23:15
I think you've answered your own question.
"load commands out of text files"
is pretty much the definintion of a script, except substitute "run" for "load".

Report •

#2
March 5, 2013 at 21:43:25
sorry, i didnt explain properly,
what i meant was, i have a batch file with a few commands already in it, but i also have a few other text files with other commands.
is there a way for the user to select a text file and whatever code is in that text file will be run?

Report •

#3
March 5, 2013 at 21:59:57
Save the text file as a .bat and use call from within the first batch.

As in

Call c:\mybatchname.bat

:: mike


Report •

Related Solutions

#4
March 5, 2013 at 22:07:43
do i have to change the text file to a batch file?

Report •

#5
March 6, 2013 at 01:48:38
Not nessicarily however it might be easier

:: mike


Report •

#6
March 6, 2013 at 21:32:42
I've managed to find some script that allows me to make the batch file execute the text in the .txt file, but for some reason as soon as it reaches a "goto" command the batch file says:
"the system cannot find the batch label specified - main"
"invalid attempt to call batch label outside of batch script"

this is the batch file:

@echo off
for /f "tokens=*" %%x in (%cd%\test.txt) do (
call %%x
)

and this is the text file:

echo hi
set /p input=
if /i "%cmd%"=="hi" (
goto hi
) ELSE (
goto bye
)

:hi
echo hi
exit
:bye
echo bye
exit

any idea's on whats going wrong?


Report •

#7
March 7, 2013 at 06:51:01
The label bye and label hi are not in the batch that you initialized. it is reading line by line the text file and the command to goto effects the original batch, not the text file.

:: mike


Report •

#8
March 8, 2013 at 06:44:22
You could cheat and use a temporary file if you wanted to.

type test.txt > tmp.bat

then call the tmp.bat

and then delete the tmp.bat

this would leave the text file intact, and if you are using multiple text files it would work for any of them.

:: mike


Report •

#9
March 13, 2013 at 22:04:31
Cool, thanks for all the help guys, it really helped!

Report •


Ask Question