Forum Moderators: coopster & phranque

Message Too Old, No Replies

perl script upload all files from a given directory

I'm getting frustrated, any ideas?

         

jeremy goodrich

11:45 pm on Dec 28, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is what I get for being clever, and trying to make the script use strict :)

Here is the file, which isn't working right now:

[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;
#my $filename;
#my $mkd;

$login = "";
$password = "";
$server = "";
$basepath = "/path/to/site";
$datadir = "practice";
$initial = "testing";
$f = "";
$filename = "";
$mkd = "";

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

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

($ftp, $server, $login, $path, $initial, $f, @site, $filename, $mkd) = (" ", "", "", "",
"testing", "", "@site", "", "")

if ( my($ftp) = Net::FTP->new("$server") ) {

print "Connected to $server\n";
if ($ftp->login("$login","$password")) {
print "Logged into $server\n";

$ftp->cwd("$path/");
$ftp->mkdir("$initial");
$ftp->cwd("$initial");

foreach $f (@site) {
#chomp($f);
#print "@site\n";
print "$f\n\n";
$filename = $f;
$f =~ s/\.html//g;
print "dir will be $f";
$mkd = $ftp->mkdir("$f");
if ($mkd) {
print "ok\n";
print "dir is $f";
}
else {
print "failed\n $f\n";
print "exiting...\n";
exit(0); }
$ftp->cwd("$f");
$ftp->put("practice/$filename");
$ftp->cwd("$initial");

}
}

else {
print "Cannot connect to $server!\n";
$ftp->quit();
return 0;
}
print "Done...\n";
}

1;
[/perl]

Any help greatly appreciated...

jeremy goodrich

1:21 am on Dec 29, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



well, looks like I got it working :) and before I got a response he he, thanks to everybody that has helped me here (and that's quite a list)

if this software will do you any good, here's a working version:

what it does: reads the names of all files in a directory, and then uploads them to a server, within a directory that is the same as the file name, for example it will connect to a server, make a directory called 'pagename' and then put 'pagename.html' in that directory.

However, that's about all the explanation I'm going to give. Turns out, the script does work with strict after all :)

here goes:

[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;
#my $filename;
#my $mkd;

$login = "";
$password = "";
$server = "";
$basepath = "";
$datadir = "practice";
$initial = "testing";
$f = "";
$filename = "";
$mkd = "";

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

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

if (my($ftp) = Net::FTP->new("$server")) {

print "Connected to $server\n";
if ($ftp->login("$login","$password")) {
print "Logged into $server\n";

$ftp->cwd("$path/");
$ftp->mkdir("$initial");
$ftp->cwd("$initial");

foreach $f (@site) {
#print "$f\n\n";
$filename = $f;
$f =~ s/\.html//g;
print "dir will be $f\n";
$mkd = $ftp->mkdir("$f");
print "ok\n";
print "dir is $f\n";
$ftp->cwd("$f");
$ftp->put("practice/$filename");
#$ftp->cwd("$initial");
$ftp->cwd("..");
sleep(2);
}
}

else {
print "Cannot connect to $server!\n";
$ftp->quit();
return 0;
}
print "Done...\n";
}

1;
[/perl]