473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

deleting files from a folder...

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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rosa | last post by:
Hi, I'm trying to clear the TIF on Windows XP programmatically with the below code. This code works fine on any folder but the TIF. For some reason the atEnd() statements always defaults to true...
6
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application...
3
by: Martin Ho | last post by:
Can someone help me with this please? I wasn't very clear in my old post. I have a program to copy files from one location to another, now I want to copy only those files which were created on...
5
by: Sandeep Singh Sekhon | last post by:
I am Developing a Web Application with ASP.NET 1.1 I have one project Folder which is virtual directory of IIS. In this directory, I have one Folder named Photos in which I used to Store Photos....
3
by: Kimera.Kimera | last post by:
I'm trying to write a program in VB.net 2003 that basically deletes all files, folders, sub-folders and sub-sub folders (etc). The program is simply for deleting the Windows/Temp folder contents,...
6
Zerin
by: Zerin | last post by:
Hi guys, I started "autopurging" discussion many days ago.As I couldn't do it,so I decided to change my database into SQL Server.And so posted this problem here. Plain and simple ------ I have...
5
by: jawloc | last post by:
Hi I have a question that I hope someone can help me with. I have created a web page that allows a user to upload an image with a caption to my web server. Here is what happens when the user...
0
by: rahuldhammy | last post by:
I am implementing a personalization(customizing the contents of the web site according to the user choice)in asp.net web site.For this i have to delete modify some files in my App_Themes folder.I am...
1
by: =?Utf-8?B?TWFyZWs=?= | last post by:
Hi Sorry for the newbie question, but is it possible to stop the publish operation from Visual Studio 2005 deleting files in a specific folder on the web server? I have an uploads folder on the...
9
by: divyakgowda | last post by:
Hello, For my ruby on rails application,i need to delete all the temporary files created in /tmp folder.If those files increases, at some point in time, my application stops working unless i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.