Batch Read Line From File Into Variable

Batch file to read a text file and perform actions

To create a simple variable and assign it to a value or string use the SET. I've been reading through the posts looking for a way/solution to store each line into its own variable, but all that is happening is that the first SET /P reads/puts the complete file content into%LOCALVER%:S. Batch-file documentation: Setting variables from an input. Using the /p switch with the SET command you can define variables from an Input. This input can be a user Input (keyboard). Aug 16, 2008 The FOR loop will read one line, assign data to variables, then you can process the variables. The FOR loop will then read the next line, and repeat the process. The variables will remain defined with the data from the last line of the config.txt after the batch file ends.

I do a lot of repetitive things on Windows pcs and servers. Often, I’ll need to do the same thing on 10 or more machines, like delete old log files or database backups older than a certain age. In the past, I created a complicated batch file to do a bunch of tasks, but it was annoying to customize it for each location. I tried adding a “profile” for each machine that had path info for common programs and file locations, but that quickly got out of hand too.

I could have installed any one of a number of scripting “languages” to make it easier, but that would have just added another layer of complexity. I just needed a simple way to read a text file and pipe each line to a command. Then I re-discovered FOR…

FOR runs a command for each file in a set of files. You can specify the command and file set on the commandline itself, or read them from other files.

Batch Read Line From File Into Variable Excel

Batch Read Line From File Into Variable

Here’s a couple of references to check out:
Microsoft
ss64

Batch Read Line From File Into Variable Function

Read lines in a file & echo them

Updated 06/30/2012 with the most basic usage.

This does nothing except read a file line by line and echo it. “%%f” is the parameter (variable name) for the line being read and “echo %%f” is the action that DO performs. Use this as a starting point to create your own batch file.

echo off
FOR /F 'delims=|' %%f in (your_file.txt) DO echo %%f

read_do.bat

Here’s my simple batch file that reads a text file, line by line, and passes each line to another batch file to perform an action. The second variable, “%%g”, is there because DelOld expects 2 parameters and FOR interprets a space in a line as a delimiter. There is a way to avoid the space as a delimiter, but this was easier and the other programs don’t seem to care. You could also pass more variables by using “%%h”, etc.


CLS
@echo off
::#########################################
ECHO #########################################
ECHO Read file and perform action
ECHO Usage: read_do.bat (filename.txt) (action.bat)
ECHO.
ECHO Text files:
dir /B /w *.txt
ECHO.
ECHO Action (.bat) files:
dir /w /B *.bat
ECHO.
ECHO #########################################
ECHO.
REM sanity check
ECHO Text file contents:
FOR /F 'delims=|' %%f in (%1) DO echo %%f %%g
PAUSE
FOR /F 'delims=|' %%f in (%1) DO CALL %2 %%f %%g

The examples below should be self-explanatory.

Example 1 – Delete a variety of old, mostly .log, files from known locations

Batch Read Line From File Into Variable

I use the VB version of DelOld (Michna.com) to get rid of old log files, and in some cases backups, after X number of days. DelOld expects a complete path to the file or directory to delete and a value in days for the minimum age of the files.

This is a list of the commandlines I want to send to DelOld. I’ve had mixed results with quoted long paths, but 8.3 style seems to work every time.

delold.txt:

c:docume~1%USERNAME%LocalS~1Applic~12Brigh~1SyncBackLogs*.html 10
c:progra~1logmeinlmi*.log 10
c:backup*rsync*.log 10

delold.bat:

REM path to delold.exe plus filename to delete (%1) & how many days old (%2)
c:delold %1 %2

Example 2 – make several file types contiguous

Use Microsoft (Sysinternals) Contig (TechNet) to make a list of files or file types contiguous.

contigs.txt:

c:*.log
e:*.log
c:*.dat
e:*.dat

contig.bat:

REM path to contig.exe plus file(s) to contig (%1)
c:sysinternalscontig -s %1

Example 3 – copy/update a list of websites for offline browsing or backup

Use wget for Windows (SourceForge) to download a list of sites, each in its own directory and fix the links for local browsing. There’s a bewildering array of options for wget that might suit your needs better than this.

sitelist.txt:

http://computingconsultants.com
http://ohio-funeral-homes.com
http://spywarekillers.info
http://betta-fish-care.info

wget_site.bat:

Batch Read First Line From File Into Variable


SET dest=pathtosavelocation
C:wgetwget.exe -N -e robots=off -r %1 -P%dest%