Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move file out of directory after 7 days 2

Status
Not open for further replies.

florida41

Technical User
May 13, 2004
95
US
I have a directory where I want to move files that are over 7 days past their creation date to a new directory on my Windows 2000 Server. Please advise how I can do this?

This isnt working:

Code:
foreach $file (C:/directory/location/)  #My foreach needs help
{
     if(stat[10] > 7)  #stat[10} is creation file date?
     {
          system.move $file /newdirectory;  #using DOS move command
     }
}
 
assuming you're running from the root of C
Code:
while (</directory/location/*>) {
   $file = $_;
   @filedetails=stat $file;
   if ($filedetails[10] > 86400*7) {
     system ("move  $file /newdirectory/*.*");
   }
}

I haven't tested this, and I'm basing it on a post from this forum, particularly </directory/location/*>, if that doesn't work look at the opendir, readdir, and closedir functions, or file::find.

The creation date (assuming its supported, and it should be) is returned in the number of seconds since the epoch , jan 1 1970, so 86400 is 24 hours, by 7 is a week

HTH
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
In Paul's example, the validation that is being run against the create date of the file is only looking to see if the create date is greater than 7 days since epoch (86400 * 7). Try this code to find out if the file was created more than 7 days ago.

Code:
while (</directory/location/*>) {
   $file = $_;
   @filedetails=stat $file;
   if ($filedetails[10] < (time-(86400*7))) {
     system ("move  $file /newdirectory/*.*");
   }
}

- Rieekan
 
valid point Rieekan,

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Every once in a while I know what I'm talking about.

- Rieekan
 
don't write code, when you **NEED** coffee to stay awake, you should see the sh!t I made of the other stuff I was working on last night.

Anyway tonite's beer nite ...
--Paul


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
In looking through the forums about moving files i came across this post. I cant seem to get the script to work. My problem is that the system command does not recognize the $file. Any suggestions?
 
I have really been working with what was given here originally. My main issue is that the system function does not recognize the $file.

my $dir = "D://Program Files/B&L Associates/Vertices/Lists/";
opendir(DIR, "$dir");
closedir(DIR);


while ($dir) {
$file = $_;
@filedetails=stat $file;
if ($filedetails[10] < (time-(86400*7))) {
system ("move $file /oldlists/*.*");
}
}
 
I think your problem may be something to do with your / and \.
I may be wrong though on all of this, since it's not tested, but it's a guess.

Windows uses \ not /.
Also, perl may have a problem with this, since \ is the escape character.

Try:

my $dir="D:\\Program Files\\B\&L Associates\Verticies\List";

Note the \ before the &- & is a special character, and May need changing.
Also do the same on move $file /oldists/.

Maybe this will work.
 
I gave that a shot but I get an error saying the:

"the system cannot find the specified file."


I believe the system command does not recoginze the $file variable.

Any other suggestions?
 
Hang on, what are you trying to do?

As far as i can tell, all you are saying with
while ($dir)
you are just saying, all the time that $dir is true (which it always it), loop.
But maybe that's what you want.

I would try something like this:

my $dir = "D://Program Files/B&L Associates/Vertices/Lists/";

opendir(DIR, "$dir");
while ($file= readdir DIR) {
@filedetails=stat $file;
if ($filedetails[10] < (time-(86400*7))) {
system ("move $file /oldlists/*.*");
}
}
close DIR;
 
OK, my suggestion does not use perl, but this should work fine:

find /your/directory -ctime +7 -exec mv {} /your/new/dir \;

This will do it, no loops, no BS.

If you need to do it with perl, use backticks.

 
FWIW Perl recognises forward slashes as directory seperators on windows.

The suggestion by mbaranski would require cygwin to be installed, but not a suggestion I would follow for reasons of portability, just my opinion
Code:
my $dir = "D://Program Files/B&L Associates/Vertices/Lists/";
opendir(DIR, "$dir");
[b][COLOR=red]@files=readdir DIR;[/color][/b]
closedir(DIR);
[COLOR=red]foreach (@files) {[/color]
   $file = $_;
   @filedetails=stat [COLOR=red][b]"$dir$file";[/b][/color]
   if ($filedetails[10] < (time-(86400*7))) {
     [COLOR=green]system ("move  $file /oldlists/*.*");[/color]
     #I'd llok at the perl rename command for this 
   }
}

HTH
--Paul

cigless ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top