0

I have a text list of HDF files I need to download from an ftp server.

This is the (example) structure of the list:

ftp://username:password@ftppath/File#1_hh_mm_ss.HDF
ftp://username:password@ftppath/File#2_hh_mm_ss.HDF
ftp://username:password@ftppath/File#3_hh_mm_ss.HDF
...

I tried to download a single file with this basic script:

url = "ftp://username:password@ftppath/File#3_hh_mm_ss.HDF"
       download.file(url, destfile = "Test1.HDF")

What I would like to do is to download multiple files at once (i.e., the ones in the list above), and save them automatically, giving at each file the name of the file as it is in the ftp link (i.e., File#1_hh_mm_ss.HDF, File#2_hh_mm_ss.HDF, File#3_hh_mm_ss.HDF)

Is anyone able to help?

Thanks!

EDIT:

I noticed that the list of files that I need to download also includes different FTP URLs (i.e.:

          'ftp://username1:password1@ftppath/File#1_hh_mm_ss.HDF',
          'ftp://username1:password1@ftppath/File#2_hh_mm_ss.HDF',
          'ftp://username2:password2@ftppath/File#3_hh_mm_ss.HDF',
          'ftp://username2:password2@ftppath/File#4_hh_mm_ss.HDF',
          'ftp://username3:password3@ftppath/File#5_hh_mm_ss.HDF',
          'ftp://username3:password3@ftppath/File#6_hh_mm_ss.HDF',
          ...

This makes everything more complicated.

Would it be possible, instead, to download all of the files per each ftp URL?

For example (simplified):

ftp://username1:password1@ftppath/File#1, File#2, File#3, File#4, ... .HDF    #(DOWNLOAD ALL .HDF FILES in the ftp folder)
ftp://username2:password2@ftppath/File#1, File#2, File#3, File#4, ... .HDF    #(DOWNLOAD ALL .HDF FILES in the ftp folder )
ftp://username3:password3@ftppath/File#1, File#2, File#3, File#4, ... .HDF    #(DOWNLOAD ALL .HDF FILES in the ftp folder )
...

Thanks a lot for your help!

1 Answer 1

0

you can use for loop, try this :

list_files= list(
"ftp://username:password@ftppath/File#1_hh_mm_ss.HDF",
"ftp://username:password2@ftppath/File#2_hh_mm_ss.HDF",
"ftp://username:password3@ftppath/File#3_hh_mm_ss.HDF",
"...")

for (i in 1:length(list_files))
  {   
   file_name=basename(list_files[[i]])
   #url=paste0(base_url,file_name)
   destfile = paste0("path/to/created_folder/",file_name)
   download.file(list_files[[i]], destfile = destfile)
   }
6
  • Thanks for your help. I tried to run it, but I get the following error: Error in download.file(url, destfile = file_name) : scheme not supported in URL 'username:password@ftppath/File#1_hh_mm_ss.HDF' . Please note, I changed "username:password@ftppath/File#1_hh_mm_ss.HDF" with the actual ftp url.
    – S_Av
    Commented Nov 23, 2022 at 9:49
  • it looks that is a problem in the url , try to add http:// or https:// to the beginning of your url?. like this stackoverflow.com/questions/44763726/…
    – islem
    Commented Nov 23, 2022 at 10:44
  • That works great! However, I have tried to change the download path but it doesn't work. Can you please help me to download it to a designated folder? I have tried the following: for (i in 1:length(list_files)) { file_name=basename(list_files[[i]]) url=paste0(base_url,file_name) download.file(url, destfile = "C:/Users/simon/Desktop/FY_3D/file_name") } What is wrong with this path? Thanks a lot!!
    – S_Av
    Commented Nov 23, 2022 at 10:59
  • try with the edited code if it work fine? . you can add dir.create("directory_name") if the dir is not yet created!
    – islem
    Commented Nov 23, 2022 at 11:06
  • 1
    Thanks islem, really appreciate your help! This works great, however, I noticed that the list of files that I need to download, includes different FTP URLs. Please, see my last EDIT.
    – S_Av
    Commented Nov 23, 2022 at 14:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.