Announcement

Collapse
No announcement yet.

Learn how to use a FTP batch file.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Learn how to use a FTP batch file.

    Hi everyone,


    I have a folder on my C Drive called "Items" and I would like to upload all the contents in that folder to another folder in the ftp server called "Upload".

    So I've found this script on this forum.

    http://forums.petri.com/showthread.p...&highlight=ftp

    Code:
    use Net::FTP;
    my $file_to_get = "file.txt";
    $ftp = Net::FTP->new("127.0.0.1", Debug => 1) or die "Cannot connect to localhost: [email protected]";
    $ftp->login("administrator",'password') or die "Cannot login ", $ftp->message;
    $ftp->cwd("somedir");
    $ftp->get($file_to_get)  or die "get failed ", $ftp->message;
    $ftp->quit;
    This is my first time using a FTP script so I'm to understand each line in the script where to put my criterias. Example;

    Code:
    my $file_to_get = "Items";
    $ftp = Net::FTP->new("68.xx.xxx.x", Debug => 1) or die "Cannot connect to localhost: [email protected]";
    $ftp->login("administrator",'password') or die "Cannot login ", $ftp->message;
    $ftp->cwd("somedir"); <<<<What is this?
    $ftp->get($Upload)  or die "get failed ", $ftp->message;
    $ftp->quit
    is this correct?

  • #2
    Re: Learn how to use a FTP batch file.

    Originally posted by Shazam View Post

    Code:
    my $file_to_get = "Items";
    $ftp = Net::FTP->new("68.xx.xxx.x", Debug => 1) or die "Cannot connect to localhost: [email protected]";
    $ftp->login("administrator",'password') or die "Cannot login ", $ftp->message;
    $ftp->cwd("somedir"); <<<<What is this?
    $ftp->get($Upload)  or die "get failed ", $ftp->message;
    $ftp->quit
    is this correct?
    not really correct. Items is your folder correct? (and by the way, this is Perl, not batch so if you want to use the code, at least read up on Perl and how to use it. Type perldoc Net::FTP to see more information. cwd() means change working directory. So it should be cwd("Upload"). To upload a file, use put()...

    Code:
    chdir ("c:\items");
    $ftp = Net::FTP->new("68.xx.xxx.x", Debug => 1) or die "Cannot connect to localhost: [email protected]";
    $ftp->login("administrator",'password') or die "Cannot login ", $ftp->message;
    $ftp->cwd("Upload"); 
    while (<*>) {
        $file=$_;
        $ftp->put($file) or die "upload failed ", $ftp->message;    
    }
    $ftp->quit
    please check the documentation of Net::FTP for more info.
    Last edited by ghostdog74; 11th June 2009, 03:38.

    Comment


    • #3
      Re: Learn how to use a FTP batch file.

      Originally posted by ghostdog74 View Post
      Code:
      chdir ("c:\items");
      $ftp = Net::FTP->new("68.xx.xxx.x", Debug => 1) or die "Cannot connect to localhost: [email protected]";
      $ftp->login("administrator",'password') or die "Cannot login ", $ftp->message;
      $ftp->cwd("Upload"); 
      while (<*>) {
          $file=$_;
          $ftp->put($file) or die "upload failed ", $ftp->message;    
      }
      $ftp->quit
      please check the documentation of Net::FTP for more info.

      Hi ghostdog74,

      thank you so much for responding. Can we use cmd lines instead. so if I use this;

      Code:
      open 68.xx.xxx.x
      administrator
      xxxxxxxxxxxx
      !:--- FTP commands below here ---
      lcd c:\Items
      cd  public_html/Upload <<<< this "upload folder" is located on the ftp server E:\Test\upload
      binary
      mput "*.*"
      disconnect
      bye"
      Is this correct? Because this is not working.
      Last edited by Shazam; 11th June 2009, 19:00.

      Comment


      • #4
        Re: Learn how to use a FTP batch file.

        Originally posted by Shazam View Post
        Hi ghostdog74,

        thank you so much for responding. Can we use cmd lines instead. so if I use this;

        Code:
        open 68.xx.xxx.x
        administrator
        xxxxxxxxxxxx
        !:--- FTP commands below here ---
        lcd c:\Items
        cd  public_html/Upload <<<< this "upload folder" is located on the ftp server E:\Test\upload
        binary
        mput "*.*"
        disconnect
        bye"
        Is this correct? Because this is not working.

        why don't you try it out? do the fTP manually, then if every thing works, copy those commands you tried that works into the batch file.

        Comment


        • #5
          Re: Learn how to use a FTP batch file.

          ghostdog's script looks remarkabl similar to the way ours works.. we've got a file called something.ftp
          then we call ftp < something.ftp
          Please do show your appreciation to those who assist you by leaving Rep Point https://www.petri.com/forums/core/im.../icon_beer.gif

          Comment


          • #6
            Re: Learn how to use a FTP batch file.

            Ok...

            I finally got this batch file to work. The only changes you have to make in the script below are the ones that are highlighted in red.

            Code:
            @echo off
            setlocal
            
            
            REM Filename
            set FILENAME="Document.txt"
            
            REM Folder name
            set PLATFORM=Test
            
            REM The file is located in this folder
            cd \%PLATFORM%
            
            
            ::Variables
            set f=%temp%\ftpc.txt
            
            ::Compose ftp commands file 
            echo open ftp.companyname.com>>%f%
            echo user username password>>%f%
            echo binary>>%f%
            
            REM This is the name of the folder on the FTP server you want to upload to
            echo cd /upload>>%f%
            
            REM This is the file you want to upload
            echo put %FILENAME%>>%f%
            echo bye>>%f%
            
            ::Execute ftp command
            ::Use "-d" key for verbose output
            ftp -n -d -s:%f%
            
            ::Cleanup
            if EXIST %f% del /f /q %f%
            if EXIST %FILENAME% del %FILENAME%
            cd \%PLATFORM%
            
            endlocal
            
            :END
            Last edited by Shazam; 25th June 2009, 15:09.

            Comment

            Working...
            X