windows 7 - Using FORFILES to delete Folders older than 2 days

07
2014-07
  • user54512

    here is what i'm trying to do:

    Use the forfiles Command in a .cmd script to delete folders from a specified path that are older than e. g. 2 days on a win7 machine.

    Edit: The Situation is the following under the path X:\backups are folders which contain daily backups e. g. 25.06.2014 next folder 24.06.2014 and so on.

    My questions regarding this are now:

    • /d in the help it always stresses --files--. So I'm not sure if this works for folders too.
    • especially i'm not sure what it would do in combination with /s. So it would just run through the folders and delete the files - but what if already the parentfolder was too old? On a sidenote the whole concept of recursive is not clear to me and what it means i was not able to find out. It just means going through the folders right?
    • If i would go with an IF-Statement then i would have to use 2 Variable @ISDIR and @FDATE. With this solution i don't know how to combine 2 If-Statements in the forfiles command and additionally i'm not sure if @FDATE is again only for files and not folders.

    So how to do it right?

    Reference of forfiles: http://technet.microsoft.com/de-de/library/cc753551%28v=ws.10%29.aspx

    Greetings!

  • Answers
  • TheUser1024

    Here is a one-line PowerShell approach:

    get-childitem "x:\backups" |? {$_.psiscontainer -and $_.lastwritetime -le (get-date).adddays(-2)} |% {remove-item $_ -force -whatif}
    

    You can adjust the age threshold through the adddays' parameter. This is still in "WhatIf" mode, so you can test it first and see what would happen. Remove the -whatif to make it actually delete the folders.

    Source: TechNet Forum


  • Related Question

    windows 7 - How to convert an IIS folder to an application?
  • Nam G VU

    When I want to convert a folder to an application, I proceed as below screenshot.

    I'm looking for script to do this from a batch-command/.bat-file. Is it possible?

    If it is, please share. Thank you!

    enter image description here


  • Related Answers
  • paradroid

    You can use the ConvertTo-WebApplication PowerShell cmdlet. The graphic interface executes the same command.