If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > Finding the last modified file in a directory

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-09-06, 11:19
dsehmby dsehmby is offline
Registered User
 
Join Date: Feb 2004
Location: London
Posts: 76
Finding the last modified file in a directory

Hi,

I am trying to find the last modified file in a directory and do something with it like move or copy.

I know how to read the directory, move, copy and get the last modified date of a file but I need the script to find the ONE file which is the very recent file in a windows folder.

I have being trying various methods, like extract the modified date and compare it to the current date, but cannot convert the dates into a format where I can extract them from each other to find the lowest value so I can extract that file!

I would be grateful is anyone can advise or point me in the right direction to accomplish what I am trying to do, either by using a module or command to extract the most recent file in a directory or calculate dates as mentioned above.

Any help would be appreciated.

Thanks...
Reply With Quote
  #2 (permalink)  
Old 05-09-06, 13:25
KevinADC KevinADC is offline
Registered User
 
Join Date: Feb 2006
Posts: 56
it would have been nice if you posted your code or the relevant part of your code. You use stat() or a file test operator, I believe it's -C (uppercase C) to find the most recent file by date. You should just be using time to compare the file creation date to the current date.
Reply With Quote
  #3 (permalink)  
Old 05-10-06, 04:54
dsehmby dsehmby is offline
Registered User
 
Join Date: Feb 2004
Location: London
Posts: 76
Hi,

Thats great...got it working...thanks very much!!

Dal
Reply With Quote
  #4 (permalink)  
Old 05-10-06, 06:04
dsehmby dsehmby is offline
Registered User
 
Join Date: Feb 2004
Location: London
Posts: 76
Hi,

Not quite getting it too work, thought I had! anyway below is the code I am using but not getting the most recent file in directory back, in fact I get nothing back! I would appreciate any help...thanks.

my $directory = "\\\\TESTINGSQL\\memorandum_export\\log\\";

opendir(DIR2, "$directory");
@files = readdir(DIR2);
$arrlength = @files;
closedir(DIR2);


my $xmlcount = 0;

for ($x = 0; $x <= $arrlength; $x++)

{

$filename = "$directory@files[$x]";
if (@files[$x] =~ m/.txt/ )
{
if (-C @files[$x] )
{
print "@files[$x]\n";
}
}
$xmlcount++
}
Reply With Quote
  #5 (permalink)  
Old 05-10-06, 15:56
KevinADC KevinADC is offline
Registered User
 
Join Date: Feb 2006
Posts: 56
First use the grep() function to get the .txt files:

Code:
opendir(DIR2, $directory) or die "Can't open $directory: $!";
@files =  grep {/\.txt$/i} readdir(DIR2);
closedir(DIR2);
Actually -C was wrong, it is -M for the modification date. You have to use the sort{} function to sort the file to know which is the most recently modified:

Code:
my $directory = '//TESTINGSQL/memorandum_export/log/';
opendir(DIR2, $directory) or die "Can't open $directory: $!";
@files = grep {/\.txt$/i} readdir(DIR2); #get txt files only
closedir(DIR2);
@sorted = sort {-M "$directory$a" <=> -M "$directory$b"} @files; #sort them by modification date most recent to least recent
print "The most recent modified file is: $sorted[0]";
Reply With Quote
  #6 (permalink)  
Old 05-11-06, 03:11
dsehmby dsehmby is offline
Registered User
 
Join Date: Feb 2004
Location: London
Posts: 76
Fantastic!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On