mget

Download files from FTP server

Description

example

mget(ftpobj,contents) retrieves the files or folders specified by contents from the FTP server associated with ftpobj into the MATLAB® current folder.

example

mget(ftpobj,contents,target) retrieves the files or folders into the local folder specified by the absolute or relative path in target. If the local folder does not exist, mget creates it.

downloadPaths = mget(___) also returns the paths to the downloaded files and folders as a cell array of character vectors. You can use the input arguments from either of the previous syntaxes.

Examples

collapse all

Download a text file from an FTP server and display its contents.

First, connect to the National Centers for Environmental Information (NCEI) FTP server.

ftpobj = ftp('ftp.ngdc.noaa.gov')
ftpobj = 

  FTP Object
     host: ftp.ngdc.noaa.gov
     user: anonymous
      dir: /
     mode: binary

Download a text file. The mget function downloads the file to the current folder on your machine.

mget(ftpobj,'README.txt');

Display the beginning of README.txt. To read the copy of README.txt downloaded to your computer, use the fileread function.

readme = fileread('README.txt');
readme(1:95)
ans = 
    '                 Welcome to the 
         NOAA/National Centers for Environmental Information (NCEI)'

FTP service courtesy of the National Centers for Environmental Information (NCEI). See the NCEI Privacy Policy, Disclaimer, and Copyright for NCEI terms of service.

Download a text file from an FTP server to a specified folder on your local machine.

First, connect to the National Centers for Environmental Information (NCEI) FTP server.

ftpobj = ftp('ftp.ngdc.noaa.gov')
ftpobj = 

  FTP Object
     host: ftp.ngdc.noaa.gov
     user: anonymous
      dir: /
     mode: binary

Download a text file to a folder named myLocalFolder. If this folder does not exist, then the mget function creates it on your local machine.

mget(ftpobj,'README.txt','myLocalFolder');

Read the beginning of README.txt using the fileread function.

readme = fileread('myLocalFolder/README.txt');
readme(1:95)
ans = 
    '                 Welcome to the 
         NOAA/National Centers for Environmental Information (NCEI)'

FTP service courtesy of the National Centers for Environmental Information (NCEI). See the NCEI Privacy Policy, Disclaimer, and Copyright for NCEI terms of service.

Input Arguments

collapse all

Connection to an FTP server, specified as an FTP object.

Remote files or folders, specified as a character vector or string scalar.

To match multiple files or folders on the FTP server, you can include a wildcard character (*) in contents. For example, if you specify contents as *.docx, then mget downloads all files whose names end with .docx.

Local folder, specified as a character vector or string scalar. target can specify a relative or absolute path.

See Also

| |

Introduced before R2006a