Copy a file to FTP server

jarett

Board Regular
Joined
Apr 12, 2021
Messages
165
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am trying to take a file from one of my network drives (O:) and copy it to a ftp server. I set the ftp up on my pc as an available network, I can browse through the folders which means I am connected, I think. I tried doing a "xcopy" & "copy" bat file to copy the file, but it would just create a new folder on my C drive.
VBA Code:
xcopy "C:\Users\jarett.AS\Documents\Book2.csv" "###.###.##.###/Action/"
pause

From what I've read I have to take another step and create a file to log on to the ftp? It seemed like I could just point the file to the location I want it to copy too. Any suggestions on easy to understand links on this situation?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
So I need to creat a txt file first? What is the "size" for?
VBA Code:
[SIZE=2]open ftp://user:password@ipaddress:port/ -passive=off
cd /Action
binary
synchronize remote "O:\Purchase Orders\z-POs\Automated_PO\Lapco_PO" /Action
bye
[/SIZE]
Code:

Then I create the bat? I copied the code from the link and tried changing what I thought would pertain to my situation but might of butchered it up.
Code:
Const cFTPServer As String = "ftp://ipaddress/"     'CHANGE THIS.
Const cFTPPort = 21
Const cFTPCommandsFile As String = "script.txt"


Public Sub Ftp_Upload_File()

    Dim inputValue As Variant
    Dim FTPusername As String, FTPpassword As String
    Dim filenum As Integer
    Dim FTPcommand As String
    Dim wsh As Object
    
    inputValue = InputBox("user", cFTPServer)
    If inputValue = False Or inputValue = "" Then Exit Sub
    FTPusername = CStr(inputValue)
    
    inputValue = InputBox("password" & user, cFTPServer)
    If inputValue = False Or inputValue = "" Then Exit Sub
    FTPpassword = CStr(inputValue)
    
    'Create file containing ftp commands.  The file has to contain the username and password
    'to connect to the ftp server.  Creating the file and deleting it immediately after use gives some degree of
    'security in preference to having a static file containing the username and password
    
    filenum = FreeFile
    Open cFTPCommandsFile For Output As #filenum
    Print #filenum, "!REM upload .csv file"             'Use !REM for comments with a Windows ftp server
    'Print #filenum, "!# upload .csv file"              'Use '# for comments with a Unix ftp server
    Print #filenum, "open " & cFTPServer & " " & cFTPPort
    Print #filenum, "user " & FTPusername & " " & FTPpassword
    Print #filenum, "cd /Action"
    Print #filenum, "binary"
    Print #filenum, "synchronize" & QQ(ThisWorkbook.Path & "O:\Purchase Orders\z-POs\Automated_PO\Lapco_PO\PO_*")
    
    'The next line is temporarily commented out to omit the bye command from the ftp commands script.
    'This keeps the command window open to show whether the ftp commands worked successfully or not.
    'If the bye command is omitted you must type bye in the command window to exit ftp and end this procedure
    
    'Print #filenum, "bye"
    Close #filenum
    
    'Construct ftp command line, specifying the file containing FTP commands.  The -n parameter suppresses auto-login
    'upon initial connection because we want to use the username and password specified in the command file
    
    FTPcommand = "ftp -i -n -s:" & QQ(cFTPCommandsFile)
    
    'Run ftp command synchronously, waiting for the command to return
    'WindowStyle 1 displays the command window. Use this, together with omitting the bye command, to verify
    'whether or not the ftp commands worked successfully.
    'WindowStyle 0 hides the command window.
    'See http://msdn.microsoft.com/en-us/library/d5fk67ky%28VS.85%29.aspx
    
    CreateObject("WScript.Shell").Run Command:=FTPcommand, WindowStyle:=1, waitonreturn:=True
        
    'Delete the ftp commands file so that the username and password are not left lying around
    
    Kill cFTPCommandsFile
    
    MsgBox "Finished"
            
End Sub

Private Function QQ(text As String) As String
    QQ = Chr(34) & text & Chr(34)
End Function
Code:
 
Upvote 0
No, the macro creates a temporary file from your inputs.

What size? It's not in the linked code.
The code in the link you referred to had "[size=2] then [/size]" at the end of the code. Oh, dam I am way off, so I am creating a macro in the actual csv file. Ok before I try to figure this out, if I am running a batch script to rename a file (same file every time ex. master.csv) to po_%timestamp%.csv, does the macro stay with the master.csv file through the rename?
 
Upvote 0
No, the link goes to my post, not the OP's.

Try this macro, changing the 'CHANGE THIS' lines as required.
VBA Code:
Const cFTPServer As String = "ftp.mysite.com"     'CHANGE THIS.
Const cFTPPort = 21
Const cFTPCommandsFile As String = "FTP_commands.txt"


Public Sub Ftp_Upload_File()

    Dim inputValue As Variant
    Dim FTPusername As String, FTPpassword As String
    Dim filenum As Integer
    Dim FTPcommand As String
    Dim wsh As Object
   
    inputValue = InputBox("Enter username", cFTPServer)
    If inputValue = False Or inputValue = "" Then Exit Sub
    FTPusername = CStr(inputValue)
   
    inputValue = InputBox("Enter password for username " & FTPusername, cFTPServer)
    If inputValue = False Or inputValue = "" Then Exit Sub
    FTPpassword = CStr(inputValue)
   
    'Create file containing ftp commands.  The file has to contain the username and password
    'to connect to the ftp server.  Creating the file and deleting it immediately after use gives some degree of
    'security in preference to having a static file containing the username and password
   
    filenum = FreeFile
    Open cFTPCommandsFile For Output As #filenum
    Print #filenum, "!REM upload .csv file"             'Use !REM for comments with a Windows ftp server
    'Print #filenum, "!# upload .csv file"              'Use '# for comments with a Unix ftp server
    Print #filenum, "open " & cFTPServer & " " & cFTPPort
    Print #filenum, "user " & FTPusername & " " & FTPpassword
    Print #filenum, "cd /remoteFolder"  'CHANGE THIS
    'Print #filenum, "binary"  'binary file
    Print #filenum, "ascii"  'text file
    Print #filenum, "put " & QQ("C:\path\to\local csv file.csv") 'CHANGE THIS
   
    'The next line is temporarily commented out to omit the bye command from the ftp commands script.
    'This keeps the command window open to show whether the ftp commands worked successfully or not.
    'If the bye command is omitted you must type bye in the command window to exit ftp and end this procedure
   
    'Print #filenum, "bye"
    Close #filenum
   
    'Construct ftp command line, specifying the file containing FTP commands.  The -n parameter suppresses auto-login
    'upon initial connection because we want to use the username and password specified in the command file
   
    FTPcommand = "ftp -i -n -s:" & QQ(cFTPCommandsFile)
   
    'Run ftp command synchronously, waiting for the command to return
    'WindowStyle 1 displays the command window. Use this, together with omitting the bye command, to verify
    'whether or not the ftp commands worked successfully.
    'WindowStyle 0 hides the command window.
    'See http://msdn.microsoft.com/en-us/library/d5fk67ky%28VS.85%29.aspx
   
    CreateObject("WScript.Shell").Run Command:=FTPcommand, WindowStyle:=1, waitonreturn:=True
       
    'Delete the ftp commands file so that the username and password are not left lying around
   
    Kill cFTPCommandsFile
   
    MsgBox "Finished"
           
End Sub

Private Function QQ(text As String) As String
    QQ = Chr(34) & text & Chr(34)
End Function
If that works the macro can be updated to rename your csv file with the timestamp.
 
Upvote 0
No, the link goes to my post, not the OP's.

Try this macro, changing the 'CHANGE THIS' lines as required.
VBA Code:
Const cFTPServer As String = "ftp.mysite.com"     'CHANGE THIS.
Const cFTPPort = 21
Const cFTPCommandsFile As String = "FTP_commands.txt"


Public Sub Ftp_Upload_File()

    Dim inputValue As Variant
    Dim FTPusername As String, FTPpassword As String
    Dim filenum As Integer
    Dim FTPcommand As String
    Dim wsh As Object
  
    inputValue = InputBox("Enter username", cFTPServer)
    If inputValue = False Or inputValue = "" Then Exit Sub
    FTPusername = CStr(inputValue)
  
    inputValue = InputBox("Enter password for username " & FTPusername, cFTPServer)
    If inputValue = False Or inputValue = "" Then Exit Sub
    FTPpassword = CStr(inputValue)
  
    'Create file containing ftp commands.  The file has to contain the username and password
    'to connect to the ftp server.  Creating the file and deleting it immediately after use gives some degree of
    'security in preference to having a static file containing the username and password
  
    filenum = FreeFile
    Open cFTPCommandsFile For Output As #filenum
    Print #filenum, "!REM upload .csv file"             'Use !REM for comments with a Windows ftp server
    'Print #filenum, "!# upload .csv file"              'Use '# for comments with a Unix ftp server
    Print #filenum, "open " & cFTPServer & " " & cFTPPort
    Print #filenum, "user " & FTPusername & " " & FTPpassword
    Print #filenum, "cd /remoteFolder"  'CHANGE THIS
    'Print #filenum, "binary"  'binary file
    Print #filenum, "ascii"  'text file
    Print #filenum, "put " & QQ("C:\path\to\local csv file.csv") 'CHANGE THIS
  
    'The next line is temporarily commented out to omit the bye command from the ftp commands script.
    'This keeps the command window open to show whether the ftp commands worked successfully or not.
    'If the bye command is omitted you must type bye in the command window to exit ftp and end this procedure
  
    'Print #filenum, "bye"
    Close #filenum
  
    'Construct ftp command line, specifying the file containing FTP commands.  The -n parameter suppresses auto-login
    'upon initial connection because we want to use the username and password specified in the command file
  
    FTPcommand = "ftp -i -n -s:" & QQ(cFTPCommandsFile)
  
    'Run ftp command synchronously, waiting for the command to return
    'WindowStyle 1 displays the command window. Use this, together with omitting the bye command, to verify
    'whether or not the ftp commands worked successfully.
    'WindowStyle 0 hides the command window.
    'See http://msdn.microsoft.com/en-us/library/d5fk67ky%28VS.85%29.aspx
  
    CreateObject("WScript.Shell").Run Command:=FTPcommand, WindowStyle:=1, waitonreturn:=True
      
    'Delete the ftp commands file so that the username and password are not left lying around
  
    Kill cFTPCommandsFile
  
    MsgBox "Finished"
          
End Sub

Private Function QQ(text As String) As String
    QQ = Chr(34) & text & Chr(34)
End Function
If that works the macro can be updated to rename your csv file with the timestamp.
Does it matter that the csv is created from a query export from Access?
 
Upvote 0
I've been looking at the WINSCP options and put this code together to run a bat script (seemed like an easier route maybe?) but think I'm still missing a key part because of the errors I'm getting. Should I start a new thread for this or is it ok to post the code on this thread?
 
Upvote 0
Does it matter that the csv is created from a query export from Access?
No, as long as the file exists before running the macro.

I've been looking at the WINSCP options and put this code together to run a bat script (seemed like an easier route maybe?) but think I'm still missing a key part because of the errors I'm getting. Should I start a new thread for this or is it ok to post the code on this thread?
Yes, please start a new thread.
 
Upvote 0
Just got something to work, so I am posting my solution on this thread. This video helped me,
Text file
VBA Code:
option batch abort
option confirm off
open ftp://user:password@ipaddress/ -passive=off
synchronize remote "O:\Purchase Orders\z-POs\Automated_PO\Lapco_PO" /Action
exit

Batch script
Code:
"C:\Program Files (x86)\WinSCP\WinSCP.com" /script=C:\Users\jarett.AS\Documents\lapcosync.txt
 
Upvote 0

Forum statistics

Threads
1,214,999
Messages
6,122,645
Members
449,093
Latest member
Ahmad123098

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top