Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you loop through each line in a text file using a windows batch file?

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.

like image 456
Mr. Kraus Avatar asked Oct 01 '08 02:10

Mr. Kraus


People also ask

How do you sequentially execute commands in batch file?

Try using the conditional execution & or the && between each command either with a copy and paste into the cmd.exe window or in a batch file. Additionally, you can use the double pipe || symbols instead to only run the next command if the previous command failed.

How does for loop work in batch?

For loop (default) of Batch language is used to iterate over a list of files. Example: copy some files into a directory (Note: the files to be copied into the target directory need to be in the same disk drive).


1 Answers

I needed to process the entire line as a whole. 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.

For Command on TechNet


If there are spaces in your file path, you need to use usebackq. For example.

for /F "usebackq tokens=*" %%A in ("my file.txt") do [process] %%A 
like image 131
Mr. Kraus Avatar answered Oct 18 '22 16:10

Mr. Kraus