## Batch file to read a text file and perform actions

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.

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

## 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 

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

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~1\Applic~1\2Brigh~1\SyncBack\Logs\*.html 10 c:\progra~1\logmein\lmi*.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:\sysinternals\contig -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:

 SET dest=\\path\to\save\location C:\wget\wget.exe -N -e robots=off -r %1 -P%dest% 

### 3 Responses to “Batch file to read a text file and perform actions”

1. Good article. Try biterscripting.com. It is way more flexible and powerful than batch. I recommend it over batch these days for automating stuff.

2. Thanks!
I’d never heard of biterscripting – I’ll take close look!

Update: I edited the comment from “SenHu” about biterScripting and removed the live link. It might be legitimate software but there are a few things that bother me about the site:

• There’s no contact info, “about us”, or privacy policy
• The whois lookup I did had bogus contact info
• You can’t see the “Agreement” you have to accept to use the software until after you’ve downloaded & started the installation

It’s up to you to decide, but I won’t even consider downloading and installing this…

3. I have been using biterscripting for a while. I have not had any issue of that nature.

It has one quirk though. It requires double quotes in its syntax. But that’s more of the syntax of that scripting language.

Sen