jrock has asked for the wisdom of the Perl Monks concerning the following question:

does anyone know how to change to a different directory using Net:FTP? Here's some code I have using it - a client wants the file in a different directory - would you know how to change to a specified directory within this code? Here is some code I have:(and thank you)
sub ftp_data { if (!($ftp = Net::FTP->new("$hostname"))){ printf "error connecting to $hostname\n"; system("echo \"could not connect to host \n$!\n\" | $B +IN/mailx -s\"$SCRIPT\" $prob"); exit; } if (!($ftp->login("$username","$password"))){ printf "error login \n"; $ftp->quit; system("echo \"could not log in: \n$!\n\" | $BIN/mailx + -s\"$SCR IPT\" $prob"); exit; } if (!($ftp->put("$OUTFILE2/IC$CPREVDATE"))){ printf OUTPUT "FAILED $line_counter,FAILED\n"; printf "error transferring file. \n"; system("echo \"could not transfer (PUT) $IN file \n$!\ +n\" | $BI N/mailx -s\"$SCRIPT\" $prob"); exit; } if (!($ftp->quit)){ printf "error while quitting \n"; system("echo \"could not quit ftp\n$!\n\" | $BIN/mailx + -s\"$SCR IPT\" $prob"); } } # End ftp_data

Replies are listed 'Best First'.
Re: FTP issue - feedback appreciated
by Beatnik (Parson) on May 23, 2001 at 21:03 UTC
    Ofcourse I'm just being lazy by pasting Net::FTP's POD but you started it :)
    cwd ( [ DIR ] ) Attempt to change directory to the directory given in $dir. If $dir is "..", the FTP CDUP command is used to attempt to move up one directory. If no directory is given then an attempt is made to change the directory to the root directory.
    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: FTP issue - feedback appreciated
by blue_cowdawg (Monsignor) on May 23, 2001 at 21:28 UTC

    Have you tried:

    $ftp->cwd($mydirectory)
    or am I misunderstanding the question?

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    Peter L. Berghold --- Peter@Berghold.Net
    "Those who fail to learn from history are condemned to repeat it."
    
Re: FTP issue - feedback appreciated
by monk2b (Pilgrim) on May 23, 2001 at 22:21 UTC
    I use a very simple version of what you are doing.
    It is easier for me to understand
    #!/usr/local/bin/perl -w use Net::FTP; $hostname = 'hostname'; # ie laserlight.net $username = 'whatever'; # ie login name $password = 'whatever'; # ie adb87nhe $ftp = Net::FTP->new($hostname); $ftp->login($username, $passwd); $ftp->cwd("/home/kevin"); # this is where you change directories $ftp->put("$OUTFILE2/IC$CPREVDATE"); $ftp->quit;

    This code sample may very well be too simple for you to use
    it helps me understand the process better.

    made the corrections that beatnik suggested

    learning monk2b
Re: FTP issue - feedback appreciated
by kschwab (Vicar) on May 24, 2001 at 05:02 UTC
    I'm posting this because I didn't see it mentioned elsewhere. Aside from cwd(), you do have another option...use full pathnames with ftp->put().

    $ftp->put("/source/file","/dest/dir/file");
Re: FTP issue - feedback appreciated
by LordAvatar (Acolyte) on May 24, 2001 at 01:17 UTC

    Use the cwd method in Net::FTP. i.e:

    use Net::FTP; $ftp = Net::FTP->new("somehost"); $ftp ->login("anonymous",'me@me.com'); $ftp->cwd("/mydir"); $ftp->get("thatFile.txt"); $ftp->quit;

    -LordAvatar "A simple truth is but a complicated lie..." -Nietzche