Reputation: 69
not sure what ive done here, i did get it working but something i have done has stuffed it up. I am FTPing from UNIX to Windows server. I want to retrieve only files from within this Windows folder (i dont want to try and process any directories). Im doing the following to check if its a file, it exists and it is readable.
if (-e $_ && -f _ && -r _)
This is always false, it is never true, even though in the Win folder there are a few files. If i take out this IF it will retrieve the files (but i want to check the file first to be safe)
Most of my code is:
$newerr=0;
# Open the connection to the host
$ftp = Net::FTP->new($hostname); # Construct object
$ftp->login($username, $password);# Log in
$ftp->cwd($home); # Change directory
#Get List of what is in current directory - this may contain other directories
@files=$ftp->dir or $newerr=1;
push @ERRORS, "Can't get file list on $_ $!\n" if $newerr;
$ftp->quit if $newerr;
next if $newerr;
print "newerr : $newerr\n";
foreach my $l (@files)
{
$_=substr($l,39);
print "file $_\n";
if (-e $_ && -f _ && -r _) {
$ftp->get("${_}") or $newerr=1; # Get file from /temp folder
push @ERRORS, "Couldn't get $_ $!\n" if $newerr;
print "Retrieved $_\n";
} else {
print "Unable to retrieve $_\n";
}
}
$ftp->quit;
print @ERRORS;
exit 0;
The print shows:
newerr : 0
file test01.hl7
Unable to retrieve test01.hl7
file test02.hl7
Unable to retrieve test02.hl7
file test03.hl7
Unable to retrieve test03.hl7
file test04.txt
Unable to retrieve test04.txt
Any help is much appreciated
Upvotes: 1
Views: 51
Reputation: 123300
if (-e $_ && -f _ && -r _) {
These are operations on locally accessible files. They cannot be applied to file names which are only on a remote server.
FTP by itself has no direct way to determine if an entry from a returned directory listing is a file or a directory. One can try to list the content of the supposed directory or try to change into it (cwd) to see if it is an actual directory. Or one can try to just retrieve the file to see if the server treats it as a real file.
Upvotes: 3