FTP ファイル操作の実行
この例では、FTP オブジェクトを使用して FTP サーバーに接続し、リモート ファイル操作を実行する方法を説明します。FTP サーバーで任意のファイル操作を行うには、以下の手順に従います。
関数
ftp
を使用してサーバーに接続します。関数
cd
、関数dir
、および関数mget
など、適切な MATLAB® FTP 関数を使用して操作を実行します。すべての操作について FTP オブジェクトを指定します。サーバーでの作業が終わったら、関数
close
を使用して接続を閉じます。
National Centers for Environmental Information (NCEI、国立環境情報センター) は、地球物理データへのパブリック アクセスを提供する匿名 FTP サービスを維持管理しています。その FTP サーバーにアクセスして、コンテンツの一覧表示、ファイルのダウンロード、およびサブフォルダーのコンテンツの一覧表示を行います。
まず、接続を開きます。
ftpobj = ftp('ftp.ngdc.noaa.gov')
ftpobj = FTP Object host: ftp.ngdc.noaa.gov user: anonymous dir: / mode: binary
FTP サーバーの最上位フォルダーのコンテンツを一覧表示します。
dir(ftpobj)
DMSP Solid_Earth google12c4c939d7b90761.html mgg INDEX.txt coastwatch hazards pub README.txt dmsp4alan index.html tmp STP ftp.html international wdc Snow_Ice geomag ionosonde
関数 mget
を使用して INDEX.txt
という名前のファイルをダウンロードします。mget
はローカル マシン上の現在の MATLAB フォルダーにファイルをコピーします。ファイルのコピーのコンテンツを表示するには、関数 type
を使用します。
mget(ftpobj,'INDEX.txt'); type INDEX.txt
National Centers for Environmental Information (NCEI), formerly the National Geophysical Data Center (NGDC) INDEX of anonymous ftp area ftp.ngdc.noaa.gov DIRECTORY/FILE DESCRIPTION OF CONTENTS -------------- ----------------------------------------------------------------- pub/ Public access area DMSP/ Defense Meteorological Satellite Data Archive geomag/ Geomagnetism and geomagnetics models hazards/ Natural Hazards data, volcanoes, tsunamis, earthquakes international/ International program information on IAGA/Oersted/wdc ionosonde/ Ionosonde data mgg/ Limited Marine Geology and Geophysics (most data in http area) OD/ Office of the Director Snow_Ice/ Snow and Ice Data Center Solid_Earth/ Historic Solid Earth Geophysics STP/ Solar-Terrestrial Physics tmp/ Pickup area for temporary outgoing data wdc/ World Data Service for Geophysics, formerly World Data Centers -------------- ----------------------------------------------------------------- Please see file README.txt in this directory for more information and how to contact NCEI. Direct E-mail inquiries to ncei.info@noaa.gov Also see our web site: http://www.ngdc.noaa.gov/ NCEI is part of the: U.S. Department of Commerce, National Oceanic and Atmospheric Administration (NOAA), National Environmental Satellite, Data and Information Service (NESDIS)
FTP サーバー上の pub
という名前のサブフォルダーに移動します。
cd(ftpobj,'pub')
ans = '/pub'
コンテンツを一覧表示します。pub
がこの時点で FTP サーバー上の現在のフォルダーとなっています。ただし、ローカル マシン上の現在の MATLAB フォルダーは変わっていないことに注意してください。cd
および dir
などの関数を使用して FTP オブジェクトを指定したとき、操作はローカル マシン上ではなく、FTP サーバー上で実行されます。
dir(ftpobj)
WebCD coast glac_lib krm outgoing results rgon
FTP サーバーへの接続を閉じます。
close(ftpobj)
FTP サービスは NCEI により提供されています。NCEI の利用規約は、NCEI Privacy Policy, Disclaimer, and Copyright を参照してください。