Forum Moderators: coopster & phranque

Message Too Old, No Replies

net::ftp question how do I check to see if a file is on the server

if the directory is there, look for the file, if file is there, goto next

         

jeremy goodrich

2:30 am on Jan 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm having a little trouble with my ftp script, I posted about before. Using the perl module Net::FTP to upload all the local files to the remote server works great, but after adding the permission changing lines, and uploading to a different server, I got an error and my script died.

So, I'm trying to check if the directory is not there, if it is, check for the file, if the file is there too, do the next one...but right now, something funky is happening, it's only evaluating for truth on the dir/file for existence, and the 2nd and successive times through, it's skipping that part of the loop. Any ideas on where the goof up is ??? Perl script follows
[perl]
#!/usr/bin/perl

#use strict;

use Net::FTP;

#declare global login etc variables
my $login;
my $password;
my $basepath;
my $file;
my $datadir;
my $dot1;
my $dot2;
my $server;
my $path;
my $mkd;
my $f;
my $ftp;
my $filename;
my @site;
my $fileinsite;
my $initial;

$login = "";
$password = "";
$server = "";
$basepath = "home/www";
$datadir = "";
$f = "";
$filename = "";
$mkd = "";

opendir(THEDIR, "$datadir") ¦¦ die ("Could not open the datadir: $datadir");
@site = readdir(THEDIR);
closedir(THEDIR);

$dot1 = shift(@site);
$dot2 = shift(@site);

my $ftp = Net::FTP->new("$server") or warn $ftp->message;

if ($ftp->login("$login","$password")) {
print "Logged into $server\n"; }
else { print "can\'t login ";
$ftp->quit();
return 0; }

$ftp->cwd("$path/");
$ftp->cwd("./www");
$ftp->put("practice/spider.txt");
$ftp->put("practice/.htaccess");

foreach $f (@site) {

$filename = $f;
if ($f =~ /\.html/) {
$ftp->put("practice/1/$f");
$ftp->quot('site chmod 0755',$f); }

else {
print "dir will be $f\n"; }

if ($ftp->cwd("$f")) {
print "$f already there, checking file $f\n";
@isthere = $ftp->ls($f);

if (@isthere =~ /index/) {
print "$f slash index already exists\n";
}
else {

$mkd = $ftp->mkdir("$f") or warn $ftp->message;
print "ok\n";
print "dir is $f\n";
$ftp->cwd("$f");
$ftp->put("practice/1/$f/index.html") or warn $ftp->message;
$index = "index.html";
$ftp->quot('site chmod 0755',$index);

$ftp->cwd("..");
sleep(2);
}
}
$ftp->quit();
print "Done...\n";

}
1;
[/perl]

Key_Master

2:50 am on Jan 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe because of a misplaced parenthese. Have you tried placing it after @isthere = $ftp->ls($f); instead of before $ftp->quit();

jeremy goodrich

3:11 am on Jan 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nope, I actually created another problem a minute ago, and I'm going to test it in a second...

I'll give that one a try if what I'm about to do doesn't work out...I figured out that the script didn't move back out of the directory it was checking when searching to see if the @isthere was true or false...

After I fix that, I'll check out your idea :) thanks.