0

trying to download multiple files from a FTP server. Currently I can download one file from the FTP server but I struggle to modify the code to my needs.

I have the following code:

 Option Explicit
 
 Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
 ByVal szURL As String, _
 ByVal szFileName As String, _
 ByVal dwReserved As Long, _
 ByVal lpfnCB As Long) As Long
 
 Private Declare PtrSafe Function DoFileDownload Lib "shdocvw.dll" (ByVal lpszFile As String) As Long
 
 Public Sub test1a() 'ohne Dialog
 
 Call URLDownloadToFile(0, "ftp://UserName:[email protected]/Week_202251/1_REPORT.ABC_20.12.22.07.32_202251.csv", _
 "C:/Users/xyz/Downloads/1_REPORT.ABC_20.12.22.07.32_202251.csv", 0, 0)
 
 End Sub

The issue, I am facing is that the filename changes every week since it has a timestamp. The filename structure is:

  • 1_REPORT.ABC_TimeStamp
  • 2_REPORT.ABC_TimeStamp
  • 3_REPORT.ABC_TimeStamp
  • 4_REPORT.ABC_TimeStamp
  • 5_REPORT.ABC_TimeStamp

Also, the folder where the files are located changes on a weekly basis: Week_202250, Week_202251, and so on. How can I change the code to get all five files with from the changing folder name?

I already tried to use:

Call URLDownloadToFile(0, "ftp://UserName:[email protected]/Week_202251/1_REPORT.ABC*.csv", .........

This did not work.

I am looking for a solution where the folder name is calculated by the current week number and the macro downloads all files from this folder.

Thanks for your help!

7
  • Welcome to SO. Make a loop that changes the filenames in every loop and download. Commented Dec 29, 2022 at 10:10
  • Thanks! How can I get the files when I only know a part of the string? Commented Dec 29, 2022 at 10:25
  • Loop trough that directory or ask for the names with an inputbox or code them into an array. You can search for examples here on SO. Commented Dec 29, 2022 at 11:45
  • Searched on SO, but did not find anything to loop through ftp directory and get filenames. Inputbox is not an option since it should be fully automated. Do you have an idea how to get that done? Commented Dec 29, 2022 at 12:58
  • Did you check this or this or this or this or this?? Try all of them coding something and adapting the co to your needs. Commented Dec 29, 2022 at 13:17

0

Browse other questions tagged or ask your own question.