How To loop through each line in a text file using a Batch Dos?

SCENARIO (From Stackoverflow.com)

I would like to know how to loop through each line in a text file using a Windows batch file and process each line of text in succession.

SOLUTION

Here is what I found to work.

for /F "tokens=*" %%A in (myfile.txt) do [process] %%A

The tokens keyword with an asterisk (*) will pull all text for the entire line. If you don't put in the asterisk it will only pull the first word on the line. I assume it has to do with spaces.

NOTE: This command will select the whole line, without the need of specifying delimiters.

EXAMPLE

REM ****************************************************************** 

REM Runs all *.sql scripts sorted by filename in the current folder. 
REM To use integrated auth change -U <user> -P <password> to -E
REM ******************************************************************
dir /B /O:n *.sql > RunSqlScripts.tmp for /F %%A in (RunSqlScripts.tmp) do osql -S (local) -d DEFAULT_DATABASE_NAME -U USERNAME_GOES_HERE -P PASSWORD_GOES_HERE -i %%A del RunSqlScripts.tmp
SOURCE

LINK

LANGUAGE
ENGLISH

1 thought on “How To loop through each line in a text file using a Batch Dos?”

Comments are closed.