Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2004
    Location
    London
    Posts
    76

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

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

  3. #3
    Join Date
    Feb 2004
    Location
    London
    Posts
    76
    Hi,

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

    Dal

  4. #4
    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++
    }

  5. #5
    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]";

  6. #6
    Join Date
    Feb 2004
    Location
    London
    Posts
    76
    Fantastic!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •