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"
forfiles– is the command to iterate over files
/s– to search recursively in subdirectories
/p .– to search in current directory
/M *.syslog– find all syslog files
/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):
||File name without extension|
||File name extension|
||Full path of the file|
||Relative path of the file|
||Evaluates to TRUE if a file type is a directory. Otherwise, this variable evaluates to FALSE|
||File size, in bytes|
||Last modified date stamp on the file|
||Last modified time stamp on the file|
Or one could do this entirely with powershell:
Get-ChildItem -Path . -Filter New*.txt -Recurse | Remove-Item
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.