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

trying FTP in Perl 2

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
US
My code is :

Code:
#!/usr/bin/perl -w
use Net::FTP;

$vPath1 = 'D:\\Appdata\\Invoice';
$vName1 = 'D:\\Inetpub\\ftproot\\Invoice\\ghxInv.bak';

if (-e $vName1)
{
    $rc=system("del /Q $file1");
    $rc=system("cd $vPath1");

    $host="edmapp";
    $username="abc";
    $password="1234";
    $directory="\olc_test_upload_v0j5";

    $ftp=Net::FTP->new($host);
    $ftp->login($username, $password);
    $ftp->cwd($directory);
    $ftp->binary();
    foreach $file (@files)
    {
       $ftp->put($file);
    }
    $ftp->quit();
}

I changed the user name and password. The error I get is 'The syntax of the command is incorrect.'

For the directory, I have tried "\olc_test_upload_v0j5", "olc_test_upload_v0j5", and "/olc_test_upload_v0j5". None seemed to change anything, same error.

The file (ghxInv.bak) does get deleted.

Does anybody have an idea of what I am doing wrong?
 
The syntax of which command is incorrect? Have you tried putting debugging messages around each step to identify which one is at fault?

Annihilannic.
 
I fail to see where you are populating the array @files.
 
You've really gotta start putting
use strict
in all your scripts, it will find 99% of the errors for you.

When you first start using strict you'll hate it, but then it'll kinda grow on you.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I didn't think you needed to escape the \'s if you used single ticks
Code:
$vPath1 = 'D:\\Appdata\\Invoice';
$vName1 = 'D:\\Inetpub\\ftproot\\Invoice\\ghxInv.bak';
Perl has unlink and I don't see where doing a system call to CD is getting you anything
Code:
$rc=system("del /Q $file1");    
$rc=system("cd $vPath1");
You should use single quotes for all of this
Code:
$host="edmapp";    $username="abc";    $password="1234";    $directory="\olc_test_upload_v0j5";
idk if you stripped these for the web or if you just don't have them in the script
Code:
$ftp->login($username, $password);
where does @files come from, you might want to look at opendir, readdir in perl to build out @files. I belive you can use the format put(local/path/$file, remote/path/file) so you don't have to worry about doing all the cd's
Code:
foreach $file (@files)    {       $ftp->put($file);    }    $ftp->quit();}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Travis, I made the changes and tested. I am working on a Windows Server.

Code:
use Net::FTP;

$vName1 = 'D:\Inetpub\ftproot\Invoice\ghxInv.bak';

if (-e $vName1)
{
##    $rc=system("del /Q $vName1");

    @files=<D:\Appdata\Invoice\Invoice*>;
    $host='edmapp';
    $username='plededmftp@ochsner.org';
    $password='EDB#h83rg28d1guv126e56312397SGI1GSU^#!^%!';

    $ftp=Net::FTP->new($host, Debug => 0) or die "No connection";
    $ftp->login($username, $password) or die "cannot login", $ftp->message;
    $ftp->cwd(olc_upload_v0j5) or die "can't cwd ", $ftp->message;
    $ftp->binary();

    foreach $file (@files)
    {
       $ftp->put($file) or die "put failed ", $ftp->message;
    }
    $ftp->quit;
}

{/Code]

If I remove the foreach loop and hard code the $file to a single file name then this works - for the one file.

That means that I am not building my @files correctly. That is how I did on the UNIX system, is it different for the Windows server?

If anybody else knows the answer, please let me know.
 
I tried using the suggestion of opendir and readdr.

Code:
use Net::FTP;

$vName1 = 'D:\Inetpub\ftproot\Invoice\ghxInv.bak';

if (-e $vName1)
{
##    $rc=system("del /Q $vName1");

    $dir='D:\Appdata\Invoice\Invoice';
    $host='edmapp';
    $username='plededmftp@ochsner.org';
    $password='EDB#h83rg28d1guv126e56312397SGI1GSU^#!^%!';

    $ftp=Net::FTP->new($host, Debug => 0) or die "No connection";
    $ftp->login($username, $password) or die "cannot login", $ftp->message;
    $ftp->cwd(olc_upload_v0j5) or die "can't cwd ", $ftp->message;
    $ftp->binary();
    opedir(BIN, $dir) or die "can't open $dir: $!";
    while( $file = readdir BIN)
    {
       $ftp->put($file) or die "put failed ", $ftp->message;
    }
    closedir(BIN);
    $ftp->quit;
}

I got no error messages and no data was transferred.

Any ideas??
 
first I fixed the typo's

Code:
use Net::FTP;

$vName1 = 'D:\Inetpub\ftproot\Invoice\ghxInv.bak';

if (-e $vName1)
{
    $rc=system("del /Q $vName1");

    $dir='D:\Appdata\Invoice';
    $rc=system("cd $dir");
    print "Files in $dir are: \n";
    $host='edmapp';
    $username='name';
    $password='password';

    $ftp=Net::FTP->new($host, Debug => 0) or die "No connection";
    $ftp->login($username, $password) or die "cannot login", $ftp->message;
    $ftp->cwd(olc_upload_v0j5) or die "can't cwd ", $ftp->message;
    $ftp->binary();
    opendir(BIN, $dir) or die "can't open $dir ";
    while( $file = readdir BIN)
    {
       $ftp->put($file) or die "put failed ", $ftp->message;
       print "$file\n";
    }
    closedir(BIN);
    $ftp->quit;
}


The output I get is

Files in D:\Appdata\Invoice are:
Cannot open Local file .: Permission denied
at File2Found_sh line 41
put failed Type set to I.

This does not make sense to me.

 
You keep making changes in your code that I don't understand. Start off simple. Get this working, then integrate it into a simple Net::FTP script;
Code:
my $some_dir = 'c:\windows';

opendir(my $dh, $some_dir) || die "can't opendir $some_dir: $!";
my @files = readdir($dh);
closedir $dh;

for my $file (@files) {
	print "$file\n";
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I tested this code

Code:
use Net::FTP;

$vName1   = 'D:\Inetpub\ftproot\Invoice\ghxInv.bak';
$dir      = 'D:\Appdata\Invoice';
$host     = 'xyz';
$username = '1234';
$password = 'abcd';

if (-e $vName1)
{
    $rc=system("del /Q $vName1");
    $rc=system("cd $dir");

    opendir($dh, $dir) or die "can't opendir $dir: $!";
    @files = readdir($dh);
    closedir $dh;

    for $file (@files) {
        print "$file\n";}

    $ftp=Net::FTP->new($host, Debug => 0) or die "No connection";
    $ftp->login($username, $password) or die "cannot login", $ftp->message;
    $ftp->cwd(olc_upload_v0j5) or die "can't cwd ", $ftp->message;
    $ftp->binary();

    for $file (@files)
    {
       print "$file\n";
       $ftp->put($file) or die "put failed ", $ftp->message;
    }
    $ftp->quit;
}

and this was the result
a list of all the files
.
Cannot open Local file .: Permission denied
at File2Found_sh line 48
put failed Type set to I.

It seems to me that if the first for loop can print the list of files, then the second for loop should be able to also print the list of files, but the return is a '.' (dot).

It is like after it starts the $ftp process it dumps the contents of the array '@files'.

Is this possible? If so, how do I send everything that is in a directory?

Code:
use Net::FTP;

    $file='D:\Appdata\PO\Invoice20100115-12345.pdf';
    $host='host';
    $username='user';
    $password='password';


    $ftp=Net::FTP->new($host, Debug => 0) or die "No connection";
    $ftp->login($username, $password) or die "cannot login", $ftp->message;
    $ftp->cwd(olc_upload_v0j5) or die "can't cwd ", $ftp->message;
    $ftp->binary();
    $ftp->put($file) or die "put failed ", $ftp->message;
    $ftp->quit;

this code works great for the one file.
 
It looks like it's dieing at the put
change this $ftp->put($file) or die "put failed ", $ftp->message;

to (please verify the directories)
$ftp->put("$dir/$file","olc_upload_v0j5/$file") or die "put failed ", $ftp->message;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I reduced the list to 12 files, removed the 'put' and just used a print statement. The first 2 lines of the 14 printed where . and the ..

Is there an easy way to delete the first 2 entries in the array?

 
next if $file =~ /^\d{1,2}/;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
oops
next if $file =~ /^\.{1,2}/;

Gets rid of . and ..

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
lets see if I can get all this again.. :)

next if $file =~ /\^.{1,2}$/;

if you find this isn't matching make sure you chomp @files.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I tried

next if $file =~ /\^.{1,2}$/;

but it did not delete or skip the records. Also, I got the same results with and without 'chomp @files;'.

Any more ideas to remove the first 2 entries in the @files array???
 
sorry.. I gotta pay attention to what I'm typing

next if $file =~ /^\.{1,2}$/;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top