Monday, 2 February 2009

Reading from a file with a Batch Script

Weird given how easy it is to write to a file from a batch script:

echo "Test" > file.txt

You would be mistaken for thinking it would be that easy to get it back out again into a happy variable.After a bit of searching, this is the best that I have come up with:

for /f %%a in ( file.txt ) DO ( something %%a )

Which really only allows you to use it in that line, which I guess for most cases is okay, and the other cases you can push what you need to do off into another batch file.

So what is this doing ?

Simply this is stepping though each line of the file and performing the action in the DO with the line in question in the variable %%a.

note: the double %% that is when you are writing a script, if you were doing this at the command line you only need one % for variables.

No comments:

Post a Comment