FTPダウンロード(コマンドライン/バッチ)
2011年01月28日
Windows標準のFTP.EXEコマンドを使用してファイルをダウンロードする方法を紹介します。FTP.EXEはコマンドプロンプトで使用するツールなのでバッチファイルでの使用も可能です。FTP.EXEコマンドの特にオススメの使い方はデバッグモードです。デバッグモードではFTPサーバとの間で送受信されるコマンドがすべて表示されます。FTPプロトコルを勉強する時に重宝するはずです。
FTP関係のその他のツールやサンプルはこちら「FTP関係」です。
コマンドプロンプトでFTPダウンロードする例:
C:\>ftp localhost
Connected to localhost.
220 Microsoft FTP Service
User (localhost:(none)): [user]
331 Password required for [user].
Password: [pass]
230 User [user] logged in.
ftp> get test.dat
200 PORT command successful.
150 Opening ASCII mode data connection for test.dat(123 bytes).
226 Transfer complete.
ftp: 123 bytes received in 0.13Seconds 5.87Kbytes/sec.
ftp> quit
221
Connected to localhost.
220 Microsoft FTP Service
User (localhost:(none)): [user]
331 Password required for [user].
Password: [pass]
230 User [user] logged in.
ftp> get test.dat
200 PORT command successful.
150 Opening ASCII mode data connection for test.dat(123 bytes).
226 Transfer complete.
ftp: 123 bytes received in 0.13Seconds 5.87Kbytes/sec.
ftp> quit
221
バッチでFTPダウンロードする例:
まず次のようなファイル(test.ftp)を作成します。
open localhost
[user]
[pass]
get test.dat
quit
[user]
[pass]
get test.dat
quit
次にバッチファイルで"ftp -i -s:test.ftp"を実行します。
C:\>ftp -i -s:test.ftp
ftp> open localhost
Connected to localhost.
220 Microsoft FTP Service
User (localhost:(none)):
331 Password required for [user].
230 User [user] logged in.
ftp> get test.dat
200 PORT command successful.
150 Opening ASCII mode data connection for test.dat(123 bytes).
226 Transfer complete.
ftp: 123 bytes received in 0.14Seconds 5.21Kbytes/sec.
ftp> quit
221
ftp> open localhost
Connected to localhost.
220 Microsoft FTP Service
User (localhost:(none)):
331 Password required for [user].
230 User [user] logged in.
ftp> get test.dat
200 PORT command successful.
150 Opening ASCII mode data connection for test.dat(123 bytes).
226 Transfer complete.
ftp: 123 bytes received in 0.14Seconds 5.21Kbytes/sec.
ftp> quit
221
ちなみに、FTPで使用される代表的な内部コマンドを次に記述します。
! | Escape to the shell |
cd | Change remote working directory |
ascii | Set ascii transfer type |
binary | Set binary transfer type |
debug | Toggle debugging mod |
delete | Delete remote file |
get | Receive file |
ls | List contents of remote directory |
mdelete | Delete multiple files |
mdir | List contents of multiple remote directories |
mget | Get multiple files |
mkdir | Make directory on the remote machine |
mput | Send multiple files |
open | Connect to remote tftp |
put | Send one file |
pwd | Print working directory on remote machine |
quit | Terminate ftp session and exit |
rename | Rename file |
rmdir | Remove directory on the remote machine |