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.

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.

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.

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.