bibo
bibo

Reputation: 13

Reading commands from text file for batch

I want to use a .bat file to read lines from .txt file then use them as commands. The text file is updated everyday so the size or the number of lines is unknown. I seem to be stuck on it. I'm completely new to batch scripting. So any kind of help is appreciated.

Upvotes: 1

Views: 1436

Answers (2)

Aacini
Aacini

Reputation: 67206

The simplest way is this:

cmd < foo.txt

This method also allows you to include lines of input data for commands in the lines following the commands. For example, try previous line with this foo.txt file:

echo Read the value given in next line
set /P var=
This is the value
echo The value read is: %var%

This feature may ve very useful in certain cases.

Upvotes: 2

Joey
Joey

Reputation: 354356

You can iterate over the lines of a file with

for /f "delims=" %%L in (foo.txt) do ...

To use whatever is in those lines as commands, just execute it:

for /f "delims=" %%L in (foo.txt) do %%L

A simpler way might be to just rename the file to a batch file and run it:

ren foo.txt foo.cmd
call foo.cmd

Upvotes: 3

Related Questions