how can i delete folders older then x days?

Discussion in 'Programming' started by haliloo22, Aug 11, 2013.

  1. #1
    hello,

    can anyone please help me. i have my own vps and whm/cpanel etc. on my site i want automatically that folders older then 50 days gets deleted. how can i set this? script/code? can anyone please help. i want the entire folder gets deleted with the files in it, automatically if a folder is older then 50 days.
     
    haliloo22, Aug 11, 2013 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    I did not test it but should work just fine. and will delete sub folders of current dir alternatively you can supply dir path as first argument to glob function call.

        $dirs = glob('*', GLOB_ONLYDIR);
       
        $fifty_days = (24*3600 /*1 day */)  * 50;
       
       
        foreach ($dirs as $dir)
        {
            if (filectime($dir) <= time() - $fifty_days)
                `rm -rf $dir`;
        }
    Code (markup):
    stay well
     
    Vooler, Aug 11, 2013 IP
    hassanahmad2 likes this.
  3. haliloo22

    haliloo22 Member

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    can you please tell me exactly how and where i need to put this code? i am a noob at coding, please help me
     
    haliloo22, Aug 11, 2013 IP
  4. matt_62

    matt_62 Prominent Member

    Messages:
    1,827
    Likes Received:
    515
    Best Answers:
    14
    Trophy Points:
    350
    #4
    you put this in a file such as script.php you run the script by using a cron job. google cron jobs on how to do this.

    I havent tested the above code, so no comments on if it will work, however as you say you are a noob, you need to know that you will need to put at the start of the file
    <?php
    and at the end of the file
    ?>
     
    matt_62, Aug 11, 2013 IP
  5. haliloo22

    haliloo22 Member

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    ok, i have a folder called 'week2'

    i want to let this delete after x days

    so in this folder i put a php file with the above code in it. and this code i recall it every day with a cronjob? and this code look if the folder is older then x days, it will remove?

    folder is called week2, so how to put week2 in this code? where?
     
    haliloo22, Aug 11, 2013 IP
  6. hassanahmad2

    hassanahmad2 Active Member

    Messages:
    243
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #6
    You've to put the script in the parent directory of week2. And you don't have to call it 'every day', it will run automatically once you set up a cron job.

    No need to put week2 anywhere in the code.
     
    hassanahmad2, Aug 13, 2013 IP