Delete x days files and folders

555 pts.
Tags:
Batch files
Batch Scripts
I need help with a batch script that will delete 30 days old files from a folder automatically. I have read about forfiles and I am doing something wrong. I don't know where the file goes when I click on forfiles.exe. Anyways, if someone can help me with this, I would greatly appreciated it. Also, I have to setup a separate batch script that would delete folders older than 7 days and their subfolders and files. Thank you!

Answer Wiki

Thanks. We'll let you know when a new response is added.

Have a look at the following pages to see if they might help. The first one uses a BAT file, and the second one uses vb script.

Discuss This Question: 12  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Kwt712
    thanks for the links ... the batch file is couple of pages of code ... i am looking for something that can be put together in few lines and delete the files ... Microsoft have something called ForFiles and if i can only figure it out how that works ... it would save me tons of time ... if you can recommend something else, great, if not, no biggy ... thanks
    555 pointsBadges:
    report
  • carlosdl
    Here is the official documentation for the tool: Forfiles And here's an article with a small example and a short explanation: ForFiles – Delete files older than X amount of Days
    69,920 pointsBadges:
    report
  • Kwt712
    Script I have use the script that you suggested ... getting an error ... attach is the script i ran and the error i get ... i am testing a folder that has 90 files and i want to delete files that are 30 days old Thanks for any of your help with this
    555 pointsBadges:
    report
  • Kwt712
    i think i forgot tot add the link to the error i put together http://www.4shared.com/file/121511873/2a86f139/script.html
    555 pointsBadges:
    report
  • carlosdl
    In the screen shot you sent, I can see that the double quotes are not showing properly, and thus the second /c, which is in fact part of the command you want to execute to the files, is being taken as a second /c option for the forfiles command. Did you copy and paste the script text ? If so, try again, but write the script manually.
    69,920 pointsBadges:
    report
  • Kwt712
    ok this is what i am getting now http://www.4shared.com/file/121538434/de5c4410/test.html
    555 pointsBadges:
    report
  • carlosdl
    I tested your script, and it worked for me. The error message you are getting is not really an error, but a message saying that no files older than 30 days exists in the 'c:test072909' directory. By the way, I would test changing the "cmd /c del @file" command to "cmd /c echo @file", which would list the files being affected. When you have it working, you can change the command to delete the files again.
    69,920 pointsBadges:
    report
  • Kwt712
    is there a reason why this script don't work with XP Professional SP3 pc ... just curious
    555 pointsBadges:
    report
  • Kwt712
    Also, i want to know if there is a way to delete Folder (for example Mon_07-01-2009, Tue_07-02-2009 etc.) and all the subfolder and file that are 7 days old. Right now i login to the remote server and delete the folders manually
    555 pointsBadges:
    report
  • carlosdl
    Forfiles is included in Windows 2003 server and above, and in Vista and above, but I think there is some resource kit you can download to be able to use it on previous versions of Windows. As for the folder deletion, I guess it can be done with something similar to the scripts I suggested in the original answer.
    69,920 pointsBadges:
    report
  • Sawser
    Here's something I used to solve this issue, it might help you:
    SET DAY=30
    SET REMOVALDIR=C:Folder_To_ Look_in
    SET PATTERN=*.*
    ::REMOVE THE /s to prevent looking in subfolders
    
    ECHO Deleting files older than %DAY% days!
    forfiles -p %REMOVALDIR% /d -%DAY% /s /m %PATTERN% /c "cmd /c del @file" 
    
    Thanks!
    10 pointsBadges:
    report
  • JenniC
    biterscripting script to delete files older than 30 days :
    # Script Delete30Days.txt
    var str list, file
    lf -n "*" "C:/Test" ($fmtime < (addtime(diff("-30000000")))) > $list
    while ($list <> "")
    do
        lex "1" $list > $file
        system del ("""+$file+""")
    done
    Read the help page for the lf ( list files) command at http://www.biterscripting.com/helppages/lf.html . You will find more goodies there that can help you customize exactly what files are processed.
    70 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following