今回は後者の FTP クライアント(外部プログラム)を呼び出す方法でファイル転送してみます。
何も準備しなくていいので、これが一番手軽な方法でしょう。
FTP の利用方法
open <host> <user> <password> <command> : bye
バッチファイルを実行する。
バッチファイルと FTP コマンドファイルを消す。
サンプルスクリプト
'FTP コマンドファイル、BAT ファイルを作る With CreateObject("Scripting.FileSystemObject") 'FTP コマンドファイルを作る Dim ftpCommandFilePath ftpCommandFilePath = .BuildPath(.GetSpecialFolder(2).Path, .GetTempName) With .CreateTextFile(ftpCommandFilePath, True, False) .Write "open hoge.co.jp" & vbCrLf & _ "john" & vbCrLf & _ "pass1234" & vbCrLf & _ "dir ""temp"" ""C:\ftptest\list.txt""" & vbCrLf & _ "bye" .Close End With 'FTP コマンドを実行する BAT ファイルを作る Dim batFilePath batFilePath = .BuildPath(.GetSpecialFolder(2).Path, .GetTempName & ".bat") With .CreateTextFile(batFilePath, True, False) .WriteLine "ftp -s:""" & ftpCommandFilePath & """" .WriteLine "exit" End With End With 'FTP を実行する(BAT ファイル経由で) With CreateObject("WScript.Shell") 'ウインドウ非表示、実行終了まで待機する Dim exitCode exitCode = .Run(batFilePath, 0, True) If exitCode = 0 Then 'TODO: 正常終了時のコードを書く WScript.Echo "FTP成功" Else 'TODO: 異常終了時のコードを書く WScript.Echo "FTP失敗" End If End With 'FTP コマンドファイル、BAT ファイルを消す With CreateObject("Scripting.FileSystemObject") .DeleteFile ftpCommandFilePath .DeleteFile batFilePath End With
open hoge.co.jp john pass1234 dir "temp" "C:\ftptest\list.txt" bye
dir コマンド以降の部分に任意のコマンドを書きます。
たとえば get ならファイル受信、put ならファイル送信となります。
FTP の処理ログを取るには
出力内容をテキストファイルにリダイレクトすれば、そのまま処理ログとして使えます。
スクリプトのバッチファイルを作る部分を、以下のように変更してください。
With .CreateTextFile(batFilePath, True, False)
.WriteLine "ftp -d -s:""" & ftpCommandFilePath & """ > """ & 〈処理ログのパス〉 & """"
.WriteLine "exit"
End With