ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    bat script for uploading file to ftp

    IT Discussion
    3
    7
    3.4k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      smartkid808
      last edited by smartkid808

      creating bat file for uploading csv file to an ftp site.. But confused as to where I need to add the file that needs to go up.. I am trying to use the script here.

      Where do I add/or what command do I use to add the file "\\server\folder\test.csv"

      or is there a better script to use

      thanks

      RomoR 1 Reply Last reply Reply Quote 0
      • RomoR
        Romo @smartkid808
        last edited by

        @smartkid808 Edit the fields bellow Connection information:

        @ECHO OFF
        ECHO Upload to FTP
        ECHO Written by: Jason Faulkner
        ECHO SysadminGeek.com
        ECHO.
        ECHO.
        
        REM Usage:
        REM UploadToFTP [/L] FileToUpload
        REM
        REM Required Parameters:
        REM  FileToUpload
        REM      The file or file containing the list of files to be uploaded.
        REM
        REM Optional Parameters:
        REM  /L  When supplied, the FileToUpload is read as a list of files to be uploaded.
        REM      A list of files should be a plain text file which has a single file on each line.
        REM      Files listed in this file must specify the full path and be quoted where appropriate.
        
        SETLOCAL EnableExtensions
        
        REM *** Connection information: EDIT THE FOLLOWING FIELDS*****
        SET Server=
        SET UserName=
        SET Password=
        
        REM***********************************************************
        
        REM ---- Do not modify anything below this line ----
        
        SET Commands="%TEMP%SendToFTP_commands.txt"
        
        REM FTP user name and password. No spaces after either.
        ECHO %UserName%> %Commands%
        ECHO %Password%>> %Commands%
        
        REM FTP transfer settings.
        ECHO binary >> %Commands%
        
        IF /I {%1}=={/L} (
           REM Add file(s) to the list to be FTP'ed.
           FOR /F "usebackq tokens=*" %%I IN ("%~dpnx2") DO ECHO put %%I >> %Commands%
        ) ELSE (
           ECHO put "%~dpnx1" >> %Commands%
        )
        
        REM Close the FTP connection.
        ECHO close  >> %Commands%
        ECHO bye    >> %Commands%
        
        REM Perform the FTP.
        FTP -d -i -s:%Commands% %Server%
        
        ECHO.
        ECHO.
        
        REM Clean up.
        IF EXIST %Commands% DEL %Commands%
        
        ENDLOCAL
        
        

        Server – The FTP Server you are uploading to. You can either enter the DNS name (ftp.myserver.com) or IP address (1.2.3.4).
        UserName – Your user name for connecting to FTP server.
        Password – Your password for connecting to the FTP server.

        1 Reply Last reply Reply Quote 0
        • S
          smartkid808
          last edited by

          @romo said in bat script for uploading file to ftp:

          Server – The FTP Server you are uploading to. You can either enter the DNS name (ftp.myserver.com) or IP address (1.2.3.4).
          UserName – Your user name for connecting to FTP server.
          Password – Your password for connecting to the FTP server.

          Got that already It connects, but I need it to 'put' the file

          This is where I am confused.

          REM Usage:
          REM UploadToFTP [/L] FileToUpload
          REM
          REM Required Parameters:
          REM FileToUpload
          REM The file or file containing the list of files to be uploaded.
          REM
          REM Optional Parameters:
          REM /L When supplied, the FileToUpload is read as a list of files to be uploaded.
          REM A list of files should be a plain text file which has a single file on each line.
          REM Files listed in this file must specify the full path and be quoted where appropriate.

          F RomoR 2 Replies Last reply Reply Quote 0
          • F
            flaxking
            last edited by

            How about Powershell?

            $uri = "ftp://uploadpath.com/in/fstest.txt" 
            $file = "c:\localpath\fstest.txt" 
            $user = "username" 
            $pass = "password"
            
            # create the FtpWebRequest and configure it 
            $ftp = [System.Net.FtpWebRequest]::Create($uri) 
            $ftp = [System.Net.FtpWebRequest]$ftp 
            $ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile 
            $ftp.Credentials = new-object System.Net.NetworkCredential($user,$pass) 
            $ftp.UseBinary = $true 
            $ftp.UsePassive = $true 
            $ftp.EnableSsl = $true 
            # read in the file to upload as a byte array 
            $content = [System.IO.File]::ReadAllBytes($file) 
            $ftp.ContentLength = $content.Length 
            # get the request stream, and write the bytes into it 
            $rs = $ftp.GetRequestStream() 
            $rs.Write($content, 0, $content.Length) 
            # be sure to clean up after ourselves 
            $rs.Close() 
            $rs.Dispose()
            

            Might have to remove options like $ftp.EnableSsl = $true and $ftp.UsePassive = $true

            1 Reply Last reply Reply Quote 1
            • F
              flaxking @smartkid808
              last edited by

              @smartkid808 said in bat script for uploading file to ftp:

              @romo said in bat script for uploading file to ftp:

              Server – The FTP Server you are uploading to. You can either enter the DNS name (ftp.myserver.com) or IP address (1.2.3.4).
              UserName – Your user name for connecting to FTP server.
              Password – Your password for connecting to the FTP server.

              Got that already It connects, but I need it to 'put' the file

              This is where I am confused.

              REM Usage:
              REM UploadToFTP [/L] FileToUpload
              REM
              REM Required Parameters:
              REM FileToUpload
              REM The file or file containing the list of files to be uploaded.
              REM
              REM Optional Parameters:
              REM /L When supplied, the FileToUpload is read as a list of files to be uploaded.
              REM A list of files should be a plain text file which has a single file on each line.
              REM Files listed in this file must specify the full path and be quoted where appropriate.

              Looks like you pass it in as an argument when you call the script

              1 Reply Last reply Reply Quote 0
              • RomoR
                Romo @smartkid808
                last edited by Romo

                @smartkid808 Sorry my bad, run the bat from either powershell or cmd and pass the parameter /I for a single File or /L for a list of files written on a file one per line.

                UploadToFTP.bat /I thisIsMyFile.txt
                
                UploadToFTP.bat /L fileContainingListOfFilesInside.txt
                
                S 1 Reply Last reply Reply Quote 1
                • S
                  smartkid808 @Romo
                  last edited by

                  @romo Thanks.. That helped.. might need help later.. Wanting to move and rename it. I need to wait for some info before figuring the rest out..

                  1 Reply Last reply Reply Quote 1
                  • 1 / 1
                  • First post
                    Last post