-2

I am trying to connect to a FTP server using perl. I am using the below code and when I run the code in CMD I get nothing in return therefore I think it has made a connection.

I am wondering if there is a way copy a CSV file from my desktop to the FTP sever. However the CSV filename is different every time for example,

File_2017_10_23 - 10.29.20
File_2017_10_23 - 13.40.20

As you can see the CSV file grabs the current date and time every time the file is generated.

I am using WINSCP to connect to my FTP server.

The perl script:

 use strict;           # Don't forget !
use Net::FTP;

my $host = "your.favorite.server";
my $user = "user";
my $password = "password";
my $put_file = 'filename.csv';
my $dir = 'C:\Users\Rich\Desktop';
my $f = Net::FTP->new($host) or die "Can't open $host\n";
$f->login($user, $password) or die "Can't log $user in\n";
$f->cwd($dir) or die "Unable to cwd to $dir\n";
$f->put($put_file) or die "Unable to put $put_file\n";

Could someone show me how this is done please.

Rich
  • 177
  • 2
  • 3
  • 13
  • "I get nothing in return therefore I think it has made a connection" — Well, yes. Have you read the documentation for the module to see how to transfer files after you've made a connection? – Quentin Oct 23 '17 at 10:23
  • no i have not, this is all new to me therefore im confused. you able to show me or put me in the right direction? – Rich Oct 23 '17 at 10:25
  • 2
    https://duckduckgo.com/?q=documentation+for+Net%3A%3AFTP+perl+module – Quentin Oct 23 '17 at 10:25
  • I don't understand what you're asking. You seem to have written some Perl and you say it appears to work. What more do you want to know? – Borodin Oct 23 '17 at 13:26
  • i am trying to copy a csv file from desktop to the FTP sever which is not working. I said the connecting to the FTP sever is working. so the issue is the CSV file not being able to copy to FTP – Rich Oct 23 '17 at 13:27
  • Your question does not contain any useful error description, i.e. the only information one can extract is that something is not working as you expected. It might be useful to show more clear what you expect your code to do and what it does instead. To debug Net::FTP you can use `Net::FTP->new($host, Debug =>1)` and include the output you get into your question. – Steffen Ullrich Oct 24 '17 at 04:22

1 Answers1

0

This is fairly simple. You need to cwd to where you would like to place the file and then put it there. This is however not moving the file, just copying it. If you want to move it, rather copy and delete locally.

use strict;           # Don't forget !
use Net::FTP;

my $host = "your.favorite.server";
my $user = "user";
my $password = "password";
my $put_file = 'filename.csv';
my $dir = '/dir/goes/here';
my $f = Net::FTP->new($host) or die "Can't open $host\n";
$f->login($user, $password) or die "Can't log $user in\n";
$f->cwd($dir) or die "Unable to cwd to $dir\n";
$f->put($put_file) or die "Unable to put $put_file\n";
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • am i putting the dir where i want the file to go e.g the ftp? also my file name is always changing for example - the file name right now is - File_2017_10_23 - 11.07.51 (as you can see it garbs the current date and time). how can i make the perl script to understand my file name? – Rich Oct 23 '17 at 11:01
  • ok, that is not part of the original question. edit your question and add the full question explaining how the files gets created, is this the only file or what? – Gerhard Oct 23 '17 at 11:51
  • _am getting error of 'unable to put filename.csv_ because you need to give it the correct name, filename.csv is just what I used as a demo – Gerhard Oct 23 '17 at 11:55
  • i have updated my post and i renamed the csv to be filename and try your code but it still gave me the error message – Rich Oct 23 '17 at 11:56
  • @Rich is your script in the same directory as the CSV files? – Gerhard Oct 23 '17 at 11:56
  • @Rich please add your actual code in your question edit so I can see, not the code I gave you so I can see where the problem is. You are welcome to change your username, password and hostname though for security reasons. – Gerhard Oct 23 '17 at 11:59
  • my code was in the original post where i was asking on how to move/copy a file. then i used your code and now i am getting errors on 'unable to put filename.csv – Rich Oct 23 '17 at 12:49
  • that is the dir i am using (please see updated post) – Rich Oct 23 '17 at 13:08
  • 1
    that is not going to work. We are doing the cwd on the FTP server. so if you want to place it in the default dir, then remove the entire `$f->cwd($dir) or die "Unable to cwd to $dir\n";` line from the script – Gerhard Oct 23 '17 at 13:18
  • what if i want to copy the file which is on desktop to the FTP sever do i put my $dir = '/root'; ? – Rich Oct 23 '17 at 13:25
  • yes, that is however if your user has rights to write to root. – Gerhard Oct 24 '17 at 05:54