+ 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
    11,840
    Blog Entries
    6
    Thank Post
    592
    Thanked 1,043 Times in 920 Posts
    Rep Power
    224

    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. IDG Tech News

  3. #2


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    3,743
    Thank Post
    661
    Thanked 639 Times in 493 Posts
    Rep Power
    154
    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.

  4. Thanks to tom_newton from:

    FN-GM (7th August 2011)

  5. #3

    plexer's Avatar
    Join Date
    Dec 2005
    Location
    Norfolk
    Posts
    9,564
    Thank Post
    306
    Thanked 884 Times in 794 Posts
    Rep Power
    211
    folders in a bath, don't they just get wet and soggy?

    Ben

  6. #4

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    11,840
    Blog Entries
    6
    Thank Post
    592
    Thanked 1,043 Times in 920 Posts
    Rep Power
    224
    Quote Originally Posted by plexer View Post
    folders in a bath, don't they just get wet and soggy?

    Ben
    Opps typo corrected

  7. #5
    morganw's Avatar
    Join Date
    Apr 2009
    Location
    Cambridge
    Posts
    679
    Thank Post
    45
    Thanked 92 Times in 87 Posts
    Rep Power
    28
    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.

  8. #6


    Join Date
    Feb 2007
    Location
    51.405546, -0.510212
    Posts
    3,845
    Thank Post
    149
    Thanked 1,228 Times in 924 Posts
    Rep Power
    305
    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?

  9. #7
    morganw's Avatar
    Join Date
    Apr 2009
    Location
    Cambridge
    Posts
    679
    Thank Post
    45
    Thanked 92 Times in 87 Posts
    Rep Power
    28
    Do mentions generate email notifications?
    If so @pat might be disappointed when he gets here.

  10. #8

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    11,840
    Blog Entries
    6
    Thank Post
    592
    Thanked 1,043 Times in 920 Posts
    Rep Power
    224
    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

  11. #9


    Join Date
    Feb 2007
    Location
    51.405546, -0.510212
    Posts
    3,845
    Thank Post
    149
    Thanked 1,228 Times in 924 Posts
    Rep Power
    305
    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!


  12. #10


    Join Date
    Feb 2007
    Location
    51.405546, -0.510212
    Posts
    3,845
    Thank Post
    149
    Thanked 1,228 Times in 924 Posts
    Rep Power
    305
    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.

  13. #11

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    11,840
    Blog Entries
    6
    Thank Post
    592
    Thanked 1,043 Times in 920 Posts
    Rep Power
    224
    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?

  14. #12


    Join Date
    Feb 2007
    Location
    51.405546, -0.510212
    Posts
    3,845
    Thank Post
    149
    Thanked 1,228 Times in 924 Posts
    Rep Power
    305
    ^ Only files.

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

  15. #13

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    11,840
    Blog Entries
    6
    Thank Post
    592
    Thanked 1,043 Times in 920 Posts
    Rep Power
    224
    Quote Originally Posted by Arthur View Post
    ^ Just files.
    Ah doesnt matter, thanks anyway

  16. #14
    morganw's Avatar
    Join Date
    Apr 2009
    Location
    Cambridge
    Posts
    679
    Thank Post
    45
    Thanked 92 Times in 87 Posts
    Rep Power
    28
    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.

  17. #15

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    11,840
    Blog Entries
    6
    Thank Post
    592
    Thanked 1,043 Times in 920 Posts
    Rep Power
    224
    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, 11: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, 03:51 PM
  4. To VOIP or not to VOIP, that is the question
    By localzuk in forum Networks
    Replies: 10
    Last Post: 11th December 2006, 11: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
  •