+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
Scripts Thread, Delete folder that is X days old in Coding and Web Development; Hi, Is there a way i can use forfiles to delete folders in a batch file please? If not can ...
  1. #1

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,092
    Thank Post
    891
    Thanked 1,742 Times in 1,502 Posts
    Blog Entries
    12
    Rep Power
    456

    Delete folder that is X days old

    Hi,

    Is there a way i can use forfiles to delete folders in a batch file please? If not can you please point me in the right direction to something that can.

    Thanks
    Last edited by FN-GM; 7th August 2011 at 08:04 PM.

  2. #2


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,479
    Thank Post
    867
    Thanked 851 Times in 673 Posts
    Rep Power
    197
    On linux, you'd use find, which can find files according to rules like age, modification time, name, size etc. and then either list them, or execute a command (such as rm, or whatever you delete files with in windows-land) - recent versions of find actually include an option to delete the file, so you don't have to fork a copy of rm.

    Luckily, find is available on windows too.

  3. Thanks to tom_newton from:

    FN-GM (7th August 2011)

  4. #3

    plexer's Avatar
    Join Date
    Dec 2005
    Location
    Norfolk
    Posts
    13,317
    Thank Post
    681
    Thanked 1,650 Times in 1,471 Posts
    Rep Power
    425
    folders in a bath, don't they just get wet and soggy?

    Ben

  5. #4

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,092
    Thank Post
    891
    Thanked 1,742 Times in 1,502 Posts
    Blog Entries
    12
    Rep Power
    456
    Quote Originally Posted by plexer View Post
    folders in a bath, don't they just get wet and soggy?

    Ben
    Opps typo corrected

  6. #5
    morganw's Avatar
    Join Date
    Apr 2009
    Location
    Cambridge
    Posts
    816
    Thank Post
    46
    Thanked 132 Times in 126 Posts
    Rep Power
    40
    Not exactly what you want but this does files based on their age:

    Code:
    FORFILES /P "C:\Folder" /S /D -1 /C "cmd /c del @path"
    /D -1 is files that are over one day old


    edit: obviously this shouldn't be mention but I can't figure out how to turn it off, [at]path" is what it should say.
    Last edited by morganw; 7th August 2011 at 10:33 PM.

  7. #6


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,150
    Thank Post
    234
    Thanked 2,739 Times in 2,019 Posts
    Rep Power
    800
    Quote Originally Posted by morganw View Post
    obviously this shouldn't be mention but I can't figure out how to turn it off, [at]path" is what is should say.
    I think it's a bug in vBulletin as I often have the same issue with @echo off in batch files. Perhaps it might be worth mentioning it to @ZeroHour?

  8. #7
    morganw's Avatar
    Join Date
    Apr 2009
    Location
    Cambridge
    Posts
    816
    Thank Post
    46
    Thanked 132 Times in 126 Posts
    Rep Power
    40
    Do mentions generate email notifications?
    If so @pat might be disappointed when he gets here.

  9. #8

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,092
    Thank Post
    891
    Thanked 1,742 Times in 1,502 Posts
    Blog Entries
    12
    Rep Power
    456
    Quote Originally Posted by morganw View Post
    Not exactly what you want but this does files based on their age:

    Code:
    FORFILES /P "C:\Folder" /S /D -1 /C "cmd /c del @path"
    /D -1 is files that are over one day old


    edit: obviously this shouldn't be mention but I can't figure out how to turn it off, [at]path" is what it should say.
    Got files copied thanks

    Quote Originally Posted by Arthur View Post
    I think it's a bug in vBulletin as I often have the same issue with @echo off in batch files. Perhaps it might be worth mentioning it to @ZeroHour?
    I had it as well, reported it here: Code Wrapper and mentions

  10. #9


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,150
    Thank Post
    234
    Thanked 2,739 Times in 2,019 Posts
    Rep Power
    800
    Quote Originally Posted by morganw View Post
    If so @pat might be disappointed when he gets here.
    Especially if she/he is going to be deleted!


  11. #10


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,150
    Thank Post
    234
    Thanked 2,739 Times in 2,019 Posts
    Rep Power
    800
    Here's a PowerShell script which does the same thing (should you need it)....

    Code:
    $date = (Get-Date).AddDays(-10)
    $files = Get-ChildItem "C:\Temp" | Where { !$_.PSIsContainer }
    foreach($file in $files) {                                 
        if ($file.LastAccessTime -lt $date) {
            Remove-Item -Path $file.FullName -WhatIf
        }
    }
    Remove the -WhatIf on the fifth line to actually delete the files.

  12. #11

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,092
    Thank Post
    891
    Thanked 1,742 Times in 1,502 Posts
    Blog Entries
    12
    Rep Power
    456
    Quote Originally Posted by Arthur View Post
    Here's a PowerShell script which does the same thing (should you need it)....

    Code:
    $date = (Get-Date).AddDays(-10)
    $files = Get-ChildItem "C:\Temp" | Where { !$_.PSIsContainer }
    foreach($file in $files) {                                 
        if ($file.LastAccessTime -lt $date) {
            Remove-Item -Path $file.FullName -WhatIf
        }
    }
    Remove the -WhatIf on the fifth line to actually delete the files.
    Would this delete files or folders please?

  13. #12


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,150
    Thank Post
    234
    Thanked 2,739 Times in 2,019 Posts
    Rep Power
    800
    ^ Only files.

    Edit. Just noticed you said folders, not files in your post.
    Last edited by Arthur; 7th August 2011 at 11:51 PM.

  14. #13

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,092
    Thank Post
    891
    Thanked 1,742 Times in 1,502 Posts
    Blog Entries
    12
    Rep Power
    456
    Quote Originally Posted by Arthur View Post
    ^ Just files.
    Ah doesnt matter, thanks anyway

  15. #14
    morganw's Avatar
    Join Date
    Apr 2009
    Location
    Cambridge
    Posts
    816
    Thank Post
    46
    Thanked 132 Times in 126 Posts
    Rep Power
    40
    If the files are already gone from the folder, perhaps by using the powershell example for FORFILES, you could use:
    Code:
    RD C:\Folder
    ...which would only delete it if it is empty.
    Last edited by morganw; 8th August 2011 at 09:46 AM.

  16. #15

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,092
    Thank Post
    891
    Thanked 1,742 Times in 1,502 Posts
    Blog Entries
    12
    Rep Power
    456
    so nobody has one. I will see if i can find something that makes the initial backup that will put it into a zip and use the above to get rid of the old backups.

    Thanks

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 4
    Last Post: 25th November 2007, 10:44 PM
  2. That is the right password!
    By Ric_ in forum *nix
    Replies: 9
    Last Post: 27th July 2007, 09:23 AM
  3. Replies: 1
    Last Post: 21st January 2007, 02:51 PM
  4. To VOIP or not to VOIP, that is the question
    By localzuk in forum Wireless Networks
    Replies: 10
    Last Post: 11th December 2006, 10:16 AM
  5. Replies: 15
    Last Post: 15th September 2006, 09:01 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •