How can I check if a file exists on a remote server using Perl?
Can I do this while using the Perl module Net::FTP
?
CHECK TO SEE IF FILE EXISTS
if (-e $file_check) {
print "File Exists!\n";
}
else {
print "File Doesn't Exist!\n";
}
Here are a few options: test -e to see if any file exists (directory or regular file), test -f to see if it exists and is a regular file, or test -d to see if it exists and is a directory. This seems to break for paths including $HOME/test or ~/test .
You might be best served by using SSH to do this:
#!/usr/bin/perl
use strict;
use warnings;
my $ssh = "/usr/bin/ssh";
my $host = "localhost";
my $test = "/usr/bin/test";
my $file = shift;
system $ssh, $host, $test, "-e", $file;
my $rc = $? >> 8;
if ($rc) {
print "file $file doesn't exist on $host\n";
} else {
print "file $file exists on $host\n";
}
You could use a command such as:
use Net::FTP;
$ftp->new(url);
$ftp->login(usr,pass);
$directoryToCheck = "foo";
unless ($ftp->cwd($directoryToCheck))
{
print "Directory doesn't exist
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With