deleting all matching files recursively

I am a big fan of linux command line utils and work with many of those on windows as well. One of the commands I have started to like is xargs. I Like the way you could compose commands with pipes. Have tried using xargs on windows without much success.

One of the things I often use this is to clean temp directories of certain log files. My linux commands would have been:

find . -name "*.syslog" | xargs rm

Now I could have done this on windows as:

dir /s /b *.syslog | xargs rm

but xargs dies with environment being too large, instead had to switch to a less elegant solution:

forfiles /s /p . /M *.syslog /c "cmd /c rm @path"

What this boils down to is:

  1. forfiles – is the command to iterate over files
  2. /s – to search recursively in subdirectories
  3. /p . – to search in current directory
  4. /M *.syslog – find all syslog files
  5. /c – to execute specified command in this case I want to remove files

Following variables are available for the command with /c option (case doesn’t matter):

Variable Description
@FILE File name
@FNAME File name without extension
@EXT File name extension
@PATH Full path of the file
@RELPATH Relative path of the file
@ISDIR Evaluates to TRUE if a file type is a directory. Otherwise, this variable evaluates to FALSE
@FSIZE File size, in bytes
@FDATE Last modified date stamp on the file
@FTIME Last modified time stamp on the file

Or one could do this entirely with powershell:

Get-ChildItem -Path . -Filter New*.txt -Recurse | Remove-Item

or:

gci . New*.txt -r | ri

For now I prefer forfiles, it is easy and handy to use. Powershell looks promising, but is verbose and don’t want to switch between two shell commands.