in reply to Re^18: transfer a file via SFTP
in thread transfer a file via SFTP

You could add

die unless(-e $file);

There are many file tests available. You can read about them in -X.

Replies are listed 'Best First'.
Re^20: transfer a file via SFTP
by cc (Beadle) on Aug 06, 2009 at 14:47 UTC
    sorry to disturb u again, but I have 2 problems:

    1.) I don't know howto move setup of the sftp connection outside the loop.

    2.) to die if the *.txt file is not there, I tried to add these lines:
    chdir "/srv" or die "/srv: $!\n"; -f "/srv/*.txt" or die "NO FILE NO TRANSFER !\n";
    but it doesn't work.

    greeings cc

      "it doesn't work" is a bit too vague for me to be able to help you. What happens? Are any messages produced?

      You need to learn some basic programming and debugging skills. For the former I suggest How to Design Programs or, if that doesn't suite you, find something in Where and how to start learning Perl that does or enroll in an introductory programming course. For the latter, you might find the advice in brian's Guide to Solving Any Perl Problem helpful.

      The -f test doesn't do glob matching on the file name. Your test would only succeed if there were a file named, literally, "/srv/*.txt". Try something like the following:

      my $dir = '/srv'; chdir $dir or die "$dir: $!"; my $sftp=Net::SFTP->new($server, %args) or die "could not open connection to $server\n"; my @files = glob('*.txt'); die 'No files to transfer' unless(@files); foreach my $file (@files) { $sftp->put($file, "/HOME/$file") or die; sleep(5); }