By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,746 Members | 1,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,746 IT Pros & Developers. It's quick & easy.

deleting files from a folder...

P: n/a
Hi all,

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

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!
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
georgios zakitraxis <ef****@yahoo.de> wrote:
I have a folder with many pictures in it.
All have the name like: HHMMDDMMYYYY.jpg

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.


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

Jul 17 '05 #2

P: n/a
georgios zakitraxis wrote:
I have a folder with many pictures in it.
All have the name like: HHMMDDMMYYYY.jpg

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.


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

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($filenames[$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. |
Jul 17 '05 #3

P: n/a
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($imgFile)) {
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
http://www.savut.com

"georgios zakitraxis" <ef****@yahoo.de> wrote in message
news:8c*************************@posting.google.co m...
Hi all,

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

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!


Jul 17 '05 #4

P: n/a
georgios zakitraxis wrote:
Hi all,

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

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!


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.
Jul 17 '05 #5

P: n/a

"Savut" <we***@hotmail.com> wrote in message
news:v1*********************@news20.bellglobal.com ...
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($imgFile)) {
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
http://www.savut.com


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_map('basename', $files) as $file)
if(sscanf($file, "%2d%2d%2d%2d%4d.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/
Jul 17 '05 #6

P: n/a
Daniel Tryba <ne****************@canopus.nl> wrote in message news:<cc**********@news.tue.nl>...
georgios zakitraxis <ef****@yahoo.de> wrote:
I have a folder with many pictures in it.
All have the name like: HHMMDDMMYYYY.jpg

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.


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...


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
Jul 17 '05 #7

P: n/a
georgios zakitraxis <ef****@yahoo.de> wrote:
> All have the name like: HHMMDDMMYYYY.jpg 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...


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!


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=readdir($fd))!==false)
{
if(is_file($file) && filemtime($file)<$maxage)
{
unlink($file);
}
}

closedir($fd);
}

--

Daniel Tryba

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.