deleting files from a folder...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • georgios zakitraxis

    deleting files from a folder...

    Hi all,

    I have a folder with many pictures in it.
    All have the name like: HHMMDDMMYYYY.jp g

    H-hour
    M-minute
    D-day
    M-month
    Y-year

    Now I want to delete all pictures which are older than now()-7days.
    How Can I do it? In SQL would be no prob but with file deleting is problem.

    Thank you sharing your experience with me!
  • Daniel Tryba

    #2
    Re: deleting files from a folder...

    georgios zakitraxis <efzoni@yahoo.d e> wrote:[color=blue]
    > I have a folder with many pictures in it.
    > All have the name like: HHMMDDMMYYYY.jp g
    >
    > Now I want to delete all pictures which are older than now()-7days.
    > How Can I do it? In SQL would be no prob but with file deleting is problem.[/color]

    That's what you get for using dates in such a horrible way... if the
    format was YYYYMMDDHHMM you could do a simple strcmp.

    Maybe you could use filemtime of filectime if these dates correspond
    with the name...

    --

    Daniel Tryba

    Comment

    • Pedro Graca

      #3
      Re: deleting files from a folder...

      georgios zakitraxis wrote:[color=blue]
      > I have a folder with many pictures in it.
      > All have the name like: HHMMDDMMYYYY.jp g
      >
      > Now I want to delete all pictures which are older than now()-7days.
      > How Can I do it? In SQL would be no prob but with file deleting is problem.[/color]

      1. Get the file names into an array;
      $filenames[0] = '121212122003.j pg'
      $filenames[1] = '030303032004.j pg'
      ...

      2. Transform all HHMMDDMMYYYY to YYYYMMDDHHMM and put in a new array;
      $newfilenames[0] = '200312121212'
      $newfilenames[1] = '200403030303'
      ...

      3. With the new format verify which files are older than now()-7days
      and delete them
      loop() {
      if ($newfilenames[$i] < $threshold) unlink($filenam es[$i]);
      }

      Pretty simple, uh?

      Ah! Don't forget to error-check!

      --
      USENET would be a better place if everybody read: | to email me: use |
      http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
      http://www.netmeister.org/news/learn2quote2.html | header, textonly |
      http://www.expita.com/nomime.html | no attachments. |

      Comment

      • Savut

        #4
        Re: deleting files from a folder...

        for ($d=1;$d<=7;$d+ +) {
        for ($h=0;$h<24;$h+ +) {
        for ($i=0;$i<60;$i+ +) {
        $strNow = date("hisdmY",
        mktime($h,$i,0, date("m"),date( "d")-$d,date("Y")));
        $imgFile = "somedir/$strNow.jpg";
        if (file_exists($i mgFile)) {
        unlink($imgFile );
        }
        }
        }
        }

        I use mktime() as it handle automatically month and year move.

        If only the file where in this format YYMMDDHHMM.jpg, it would be as easy as
        3 lines of codes

        Savut


        "georgios zakitraxis" <efzoni@yahoo.d e> wrote in message
        news:8c590c7e.0 407080932.dcefe 21@posting.goog le.com...[color=blue]
        > Hi all,
        >
        > I have a folder with many pictures in it.
        > All have the name like: HHMMDDMMYYYY.jp g
        >
        > H-hour
        > M-minute
        > D-day
        > M-month
        > Y-year
        >
        > Now I want to delete all pictures which are older than now()-7days.
        > How Can I do it? In SQL would be no prob but with file deleting is
        > problem.
        >
        > Thank you sharing your experience with me![/color]

        Comment

        • Michael Austin

          #5
          Re: deleting files from a folder...

          georgios zakitraxis wrote:
          [color=blue]
          > Hi all,
          >
          > I have a folder with many pictures in it.
          > All have the name like: HHMMDDMMYYYY.jp g
          >
          > H-hour
          > M-minute
          > D-day
          > M-month
          > Y-year
          >
          > Now I want to delete all pictures which are older than now()-7days.
          > How Can I do it? In SQL would be no prob but with file deleting is problem.
          >
          > Thank you sharing your experience with me![/color]

          on my OpenVMS box it is very easy... if the files were actually created
          on the date of the filename, then it is pretty easy to
          delete/before="today-7-" *.jpg; (Yes, I use PHP on OpenVMS - and Rdb and
          Oracle and MySQL and Perl and Apache -- it is far more secure than the
          equivalent in Unix...)

          but in your case, it is just a bit more difficult...

          break the date into it's pieces-parts, rearrange them, see if it is >
          than 7 days ago and then delete the file. There is a calc-elapsed-time
          in the docs that could be "borrowed" as a starting point. <<
          http://us3.php.net/manual/en/function.time.php >>

          Michael Austin.

          Comment

          • Chung Leong

            #6
            Re: deleting files from a folder...


            "Savut" <webki@hotmail. com> wrote in message
            news:v1gHc.3437 1$WM5.1610102@n ews20.bellgloba l.com...[color=blue]
            > for ($d=1;$d<=7;$d+ +) {
            > for ($h=0;$h<24;$h+ +) {
            > for ($i=0;$i<60;$i+ +) {
            > $strNow = date("hisdmY",
            > mktime($h,$i,0, date("m"),date( "d")-$d,date("Y")));
            > $imgFile = "somedir/$strNow.jpg";
            > if (file_exists($i mgFile)) {
            > unlink($imgFile );
            > }
            > }
            > }
            > }
            >
            > I use mktime() as it handle automatically month and year move.
            >
            > If only the file where in this format YYMMDDHHMM.jpg, it would be as easy[/color]
            as[color=blue]
            > 3 lines of codes
            >
            > Savut
            > http://www.savut.com[/color]

            I don't know... Calling file_exists() ten thousand times doesn't seem
            terribly efficient to me. Here's my take in 5 lines:

            $cutoff = time() - 60 * 60 * 24 * 7;
            $files = glob("$folder/*.jpg");
            foreach(array_m ap('basename', $files) as $file)
            if(sscanf($file , "%2d%2d%2d%2d%4 d.jpg", &$H, &$M, &$D, &$F, &$Y) == 5)
            if(mktime($H, $M, 0, $F, $D, $Y) < $cutoff) unlink($file);


            --
            Obey the Clown - http://www.conradish.net/bobo/


            Comment

            • georgios zakitraxis

              #7
              Re: deleting files from a folder...

              Daniel Tryba <news_comp.lang .php@canopus.nl > wrote in message news:<cck1nc$6f j$1@news.tue.nl >...[color=blue]
              > georgios zakitraxis <efzoni@yahoo.d e> wrote:[color=green]
              > > I have a folder with many pictures in it.
              > > All have the name like: HHMMDDMMYYYY.jp g
              > >
              > > Now I want to delete all pictures which are older than now()-7days.
              > > How Can I do it? In SQL would be no prob but with file deleting is problem.[/color]
              >
              > That's what you get for using dates in such a horrible way... if the
              > format was YYYYMMDDHHMM you could do a simple strcmp.
              >
              > Maybe you could use filemtime of filectime if these dates correspond
              > with the name...[/color]

              OK, ok, I know I did a mistake!
              :o)
              I changed the date to YYYYMMDDHHMM to make it easier for you and for
              me!
              ;-)

              Anyway thanks for your help - To make the code less big and very fast
              is also in my interest.

              The folder contains pictures from every _minute_ so the total pics for
              7 days (13 hours a day) are: 1*60*13*7 = That means there are 5460
              pics in the folder.
              So if you have a clue or idea to make the code more flexible and
              faster, please let me know and post it here!

              Thanks

              Comment

              • Daniel Tryba

                #8
                Re: deleting files from a folder...

                georgios zakitraxis <efzoni@yahoo.d e> wrote:[color=blue][color=green][color=darkred]
                >> > All have the name like: HHMMDDMMYYYY.jp g[/color]
                >> That's what you get for using dates in such a horrible way... if the
                >> format was YYYYMMDDHHMM you could do a simple strcmp.
                >>
                >> Maybe you could use filemtime of filectime if these dates correspond
                >> with the name...[/color]
                >
                > OK, ok, I know I did a mistake!
                > :o)
                > I changed the date to YYYYMMDDHHMM to make it easier for you and for
                > me!
                > ;-)[/color]
                [color=blue]
                > Anyway thanks for your help - To make the code less big and very fast
                > is also in my interest.
                >
                > The folder contains pictures from every _minute_ so the total pics for
                > 7 days (13 hours a day) are: 1*60*13*7 = That means there are 5460
                > pics in the folder.
                > So if you have a clue or idea to make the code more flexible and
                > faster, please let me know and post it here![/color]

                Reading files from a folder is actually quite fast (depending on
                filesystems restrains ofcourse).

                Sounds like a camera taking automated pictures every minute... How much
                control do you have over the process? Splitting up the file in
                directories per day would be a good start.

                But if the pictures are taken every minute... does the creation time
                reflect that?

                If so:

                $maxage=time()-7*24*60*60;
                if($fd=opendir( "."))
                {
                while(($file=re addir($fd))!==f alse)
                {
                if(is_file($fil e) && filemtime($file )<$maxage)
                {
                unlink($file);
                }
                }

                closedir($fd);
                }

                --

                Daniel Tryba

                Comment

                Working...