Thursday 3 May 2007

Do recursive things from the command line

Ever wanted to recursively delete all files of a particular type from the command line or a script? Linux gives you lots of options for this and it turns out so does the cmd prompt. Try this for removing html files from a folder tree on Windows:
C:\>for /d /r "C:\My Clutter\And Chaos" %v IN (*) DO del "%v\*.html"
You've probably noticed that you can amend this line to run any command your mischievous mind can conjure.
  • The for obviously gives you your recursion
  • The /d gives you a directory listing
  • The "C:\My Clutter\And Chaos" is your working directory
  • %v is the variable that will capture the output of your directory listing
  • Everything after DO is the command that gets run for each variant of %v
Enjoy your new power...

No comments: