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:

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

Now I could have done this on windows as:

1
dir /s /b *.syslog | xargs rm

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

1
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):

VariableDescription
@FILEFile name
@FNAMEFile name without extension
@EXTFile name extension
@PATHFull path of the file
@RELPATHRelative path of the file
@ISDIREvaluates to TRUE if a file type is a directory. Otherwise, this variable evaluates to FALSE
@FSIZEFile size, in bytes
@FDATELast modified date stamp on the file
@FTIMELast modified time stamp on the file

Or one could do this entirely with powershell:

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

or:

1
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.