Home > Mobile >  Save multiple HDF files from an ftp list in R, giving a different name to each according to the name
Save multiple HDF files from an ftp list in R, giving a different name to each according to the name

Time:11-24

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!

CodePudding user response:

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)
   }
  • Related