ForEachFile.exe

Usage: ForEachFile <Filespec> [-s] [-n] [-c] [-e] [-p] [-x<Xspec>] <Command>

ForEachFile executes Command once for each file that matches Filespec. Both Filespec and Xspec can be any valid argument to the DIR command, including multiple space-separated file specifications, attribute specifications (/a...), etc. If either Filespec or Xspec contain spaces, they must be enclosed in quotation marks. ForEachFile is a Win32 console app, and so supports long file names, long command lines, UNC names for network servers, and has no practical memory limits. It runs only under Windows NT, Windows95, or a compatible environment.

Why it is Useful

Any time you have to do the same thing to more than one file, ForEachFile is potentially useful. It lets you write more powerful batch files with less effort. And it makes programs you use every day (like notepad) more useful by effectively enhancing their command-line support.

Macros

Given a matching file named D:\USR\DEFAULT\SOMETHNG.TXT and a search starting in D:\USR, macros in Command expand as follows:
      $f  D:\USR\DEFAULT\SOMETHING.TXT
      $n  SOMETHING.TXT
      $b  SOMETHING
      $e  .TXT
      $p  D:\USR\DEFAULT\
      $d  \USR\DEFAULT\
      $r  DEFAULT\  (path relative to current directory)
      $v  D:
	

Options

       -s:  recurse into subdirectories.
       -n:  noshell (suppresses use of shell specified by COMSPEC)
       -c:  chdir to $p before executing Command.
       -e:  echo Command to console before executing it.
       -p:  pause for confirmation before executing Command.
       -8:  use DOS (8.3) filenames in macro expansions.
 -m<char>:  designate a character other than '$' to start macros.
 -x<spec>:  exclude files matching spec from Filespec.
	

Return Codes

ForEachFile returns 2 if no files match FileSpec. Otherwise it returns an accumulation (a bitwise-OR) of the return codes of all the commands it executes. Note that under Win95, the return code is more meaningful if you use the -n switch to suppress use of the COMMAND.COM shell, because COMMAND.COM does not pass through the exit code of the last program it executes; this is not a problem under NT.


Examples


ForEachFile *.c -s fc $n j:$r$n
For each file with a .c extension in the current directory and all its subdirectories, this command runs the Win95 FC.EXE utility to compare the file with a file of the same name and in the same relative location on drive J:. This is handy because, although FC supports wildcard filespecs, it does not recurse into subdirectories.
ForEachFile "*.c *.h *.cpp" comptime -v $f \\SomeServer\sharename\src\$n
This command uses the utility COMPTIME.EXE (included with ForEachFile.exe in PRESENTS.ZIP) to show any timestamp differences between source files in the current directory and same-named sources on a server specified with a UNC name.


ForEachFile "/ar-d *.?" -s fc $f j:$d$f
This command searches for read-only non-directory files with single-character extensions in or under the current directory and uses FC.EXE to compare them with same-named files in a parallel location on drive J:. Note that the "/ar-d" is part of the filespec, not a ForEachFile switch.
ForEachFile *.txt -s start notepad $f
This command loads each .txt file in the current directory or any of its subdirectories into a separate instance of the Win95 notepad applet.


ForEachFile *.txt -s copy $f c:\temp\$b.bak
This command copies all .txt files in or under the current directory to the c:\temp directory and changes their extensions to .bak.


ForEachFile "*.txt *.lst" -s -8 list $r$n
The -s (recurse into subdirectories) and -8 (use 8.3 filenames) arguments in this command make the shareware LIST utility more useful by solving its inability to handle long file and directory names, as well as its lack of support for directory recursion.


ForEachFile "*.tmp *.bak tmp*.*" -s -e move $f c:\temp
This command searches the current directory and all its subdirectories for files ending in .tmp or .bak, and for files beginning with "tmp", and moves them to c:\temp. The -e argument causes it to echo each command as it is executed so you can see what is happening.


ForEachFile "/ad *.?" -s echo $f
This command recurses through all subdirectories of the current directory and displays their names.


Distribution

You can copy ForEachFile freely, and you can use it for any purpose, but you may not modify it.

ForEachFile is updated occasionally in http://www.teleport.com/~brecheen.
Written by Cole Brecheen (brecheen@teleport.com), November 1995.