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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Net::Ftp Problem.

Status
Not open for further replies.

randy4126

Programmer
Jun 2, 2001
62
US
Does anyone know how if you are using the Net::FTP module, How I can preserve the the time stamp from the original file? Thanks.

Randy
smiletiniest.gif
 
There doesn't appear to be a 'nice' way to get ftp to keep original file attributes.

One way round this problem is to produce a text file containing the original dates/times and ftp it along with the files. Then, when the files have arrived, use the text file to reset the files' attirbutes.

I acquired this code along the way so can lay no claim to authorship!

Record the original attributes:

Code:
use strict;

open INDEX, ">timestamps";

makeindex(".");

sub makeindex($) { # makes index of a dir
  my (@files, $dir);
  $dir = $_[0];

  return -1 if (!opendir(hDir, $dir));
  @files = readdir(hDir);
  closedir(hDir);

  foreach (@files) {
    if (-d $dir . "/" . $_) {
      makeindex($dir . "/" . $_) if ($_ ne "." && $_ ne "..");
    } else {
      print INDEX "$dir/$_\t" . (stat($dir . "/" . $_))[9] . "\n";
    }
  }
  closedir(hDir);
}

close INDEX;

Restore the attributes after ftp-ing:

Code:
use strict;

my $file, my $date, my $atime, my $mtime;

open INDEX, "timestamps";

while (<INDEX>) {
  chomp;
  ($file, $date) = split /\t/;
  ($atime,$mtime) = (stat($file))[8,9];

  if ($date ne $mtime) {
    print STDERR " -> $file has changed mtime\n";
    utime $atime, $date, $file;
  }
}

close INDEX;

Hope this helps you produce a solution.
 
Thanks for your suggestion. What I did is I got I directory listing from the remote machne by using the $ftp->dir() and then parsed out the time stamp. Then I used the utime() so set the files time stamp. Creating a file and FTPing seemed to me to be a little much. Thanks for you help. What I was missing was the function utime and was about to do it by using the touch command.

Randy
smiletiniest.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top