bat script for uploading file to ftp



  • 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



  • @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.



  • @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.



  • 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



  • @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



  • @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
    


  • @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..


Log in to reply