Reading of files in a Batch Script is done via using the FOR loop command to go through each line which is defined in the file that needs to be read. Since there is a no direct command to read text from a file into a variable, the ‘for’ loop needs to be used to serve this purpose.
Let’s look at an example on how this can be achieved.
@echo off FOR /F "tokens=* delims=" %%x in (new.txt) DO echo %%x
The delims parameter is used to break up the text in the file into different tokens or words. Each word or token is then stored in the variable x. For each word which is read from the file, an echo is done to print the word to the console output.
If you consider the new.txt file which has been considered in previous examples, you might get the following output when the above program is run.
"This is the directory listing of C:\ Drive" Volume in drive C is Windows8_OS Volume Serial Number is E41C-6F43 Directory of C:\ 12/22/2015 09:02 PM <DIR> 01 - Music 06/14/2015 10:31 AM <DIR> 02 - Videos 09/12/2015 06:23 AM <DIR> 03 - Pictures 12/17/2015 12:19 AM <DIR> 04 - Software 12/15/2015 11:06 PM <DIR> 05 - Studies 12/20/2014 09:09 AM <DIR> 06 - Future 12/20/2014 09:07 AM <DIR> 07 - Fitness 09/19/2015 09:56 AM <DIR> 08 - Tracking 10/19/2015 10:28 PM <DIR> 09 – Misc