FJH Consulting Community: Downloads: f_4each 

f_4each is a 32 bit command line utility that outputs a formatted string for each line that is sent to it. This may seem a little confusing, but a simple example may clarify just what it does.

Example: Lets say you wanted to find and delete all backup files under your document directory tree. You could user the command "del *.bak" but that would only delete the backup files in the current directory. If you had a lot of sub directories you would have to execute this command in every directory. You can use the "dir /b /s *.bak" command to find and list all of the files but this does not delete them. This is where f_4each comes in. You could take the output of your search and pipe it to f_4each and have it turn the list into a set of delete commands. f_4each does not actually execute the commands, so I usually output the results to a batch file and then execute the batch file.

OK, still a little fuzzy? Here is a real example of how I use it every day. As a programmer, I often work on large projects that have many modules and parts that are organized in a series of subdirectories on my system. When compiling and debugging these projects, many temporary files are created. Some of these files are fairly large and are not needed once the application is built, so I created this simple batch file to clean up the temporary files.

dir *.~* /b /s|f_4each del {}>temp.bat
dir *.$$$ /b /s|f_4each del {}>>temp.bat
dir p*.lck /b /s|f_4each del {}>>temp.bat
dir Del1.MB /b /s|f_4each del {}>>temp.bat
dir dbdwork.ini /b /s|f_4each del {}>>temp.bat
call temp.bat
del temp.bat

Here's how it works: First I use the "dir" command to list all of the files. The "/b" option creates a "brief" listing containing only the file name and extension, without all of the file size and date junk. The "/s" option tells the dir command to search all subdirectories. Finally the pipe"|" or vertical bar, causes the output to be sent to the f_4each utility. The out put would look something like this:


With me so far? The f_4each utility takes the command line template I provided "del {}", and replaces every occurrence of "{}" with the file name that was passed to f_4each. The output now looks like this:

del apple.~pas
del orange.~pas
del grapes.~pas

OK, almost there. The ">" on the first line creates a new "temp.bat" file and puts all of the output into the file. The subsequent lines use ">>" to append to the newly created temp.bat file. It us important to use the single ">" on the first line, and ">>" on the subsequent lines. If you use the ">>" on the first line and there is a left over temp.bat file from a previous run, you will be executing the old commands as well as the new ones, which is probably not what you want. If you use ">" on every line, then the second line will delete the work done by the first, the third will delete the second, and so on, so that only the output from the last f_4each will be executed.

The final phase is to execute the temp.bat file, then delete the temp.bat file.

The input to f_4each does not have to be the output of a dir command, it can be the output from just about any command. In one case I have a file that is a list of files I want to copy to a floppy. I use the "type" command to send the file to f_4each, and f_4each creates the necessary statements to copy each file to the floppy.

type floppy.lst|f_4each copy {} a:\>temp.bat
call temp.bat
del temp.bat

These are only a few examples of what can be done. I have some scripts that make backup copies of specific files, or rename files. You are only limited by your creativity. See what you can come up with? If you find an interesting use I like to hear about it.

If you have read, and agree with my simple license agreement you may click here to download f_4each.exe (42 KB).

Trivia for the day:

Humphrey Bogart was related to Princess Diana. They were 7th cousins.

Copyright © 1982-2017 FJH Consulting, All Rights Reserved.