連携 FTPexeを使いタスク登録し自動時刻に実行

※より実装に近く表示させる為、コードの改行を避けています。スマホ等で閲覧される際は向きを変えてご覧ください。

※実装するバージョンによってはバージョンアップの仕様により動作しないコードもあります。実装には動作確認の上ご使用下さい。

Option Explicit


Sub FTPAutoTimeTaskRegist(CDpath As String, LCDpath As String)
'***************************************
'FTPexeを使いタスク登録し自動時刻に実行
'***************************************
'CDpath     UPするサーバアドレス                    例[www/test/]
'LCDpath    UPされるファイルがあるローカルアドレス  例[C:\Temp\アップ]

'FTPautoTaskRegist-FTPexeを使いタスク登録し指定時刻に実行を改良
'タスクの同時刻実行を回避するため基本開始時刻を設定し定間隔で実行
'コマンドファイルをカウントし一定間隔(分)毎に実行
'※batファイルによりタスク実行後は該当コマンドファイルは削除される
'※によりコマンドファイルは蓄積される事は無い
'パラメータで指定するstrFilePathのパスには関係の無い.txtは置かない事
'※パラメータで指定する基本開始時刻strTimeの初回実行時刻は+Interval
'※よってIntervalが20の場合
'"20:00"に1回目を実行させたい場合はstrTimeを"19:40"にする必要がある

Dim strFilePath As String
Dim strFileName As String
Dim CommandFileName As String
Dim BatFileName As String
Dim ServerName As String
Dim UserID As String
Dim UserPassword As String
Dim strMode As String 'ascii / binary or asc / bin アスキー / バイナリ
Dim Extension As String
Dim FileNO As Integer 'ファイル番号
Dim CommandFileFullPath As String
Dim BatFileFullPath As String
Dim strTime As String
Dim Interval As Long '定間隔

'---------------------------------------------------------------------
'パラメータ(お好み環境に変更してください)
strFilePath = "C:\Temp"         'コマンドファイルの場所(.txt数をカウント)
ServerName = "jp-ia.com"        'UPサーバ名
UserID = "xxxx"                 'ユーザーID
UserPassword = "zzzzzz"         'パスワード
strMode = "ascii"               'モード
Extension = "htm"               'UP対象ファイルの拡張子
strTime = "20:00"               '基本開始時刻例[20:00](小文字必須)
Interval = 20                   '次のタスクを行う間隔(分)
'---------------------------------------------------------------------
'コマンドファイル作成 ①

'ファイル名作成
strFileName = Format(Date, "yyyymmdd") & "_" & Format(time, "hhnnss")
'コマンドファイル名
CommandFileName = strFileName & ".txt"

CommandFileFullPath = strFilePath & "\" & CommandFileName 'フルパス定義
FileNO = FreeFile 'ファイルID取得

Open CommandFileFullPath For Output As #FileNO '新規作成

Print #FileNO, "open " & ServerName
Print #FileNO, "user " & UserID & " " & UserPassword
Print #FileNO, "hash"
Print #FileNO, strMode
Print #FileNO, "cd " & CDpath
Print #FileNO, "lcd " & LCDpath
Print #FileNO, "mput *." & Extension
Print #FileNO, "Quit"

Close #FileNO 'ファイルを閉じる
'---------------------------------------------------------------------
'実行バッチファイルの作成 ②

'バッチファイル名
BatFileName = strFileName & ".bat"
BatFileFullPath = strFilePath & "\" & BatFileName 'フルパス定義
FileNO = FreeFile 'ファイルID取得
Open BatFileFullPath For Output As #FileNO '新規作成

'実行コマンドファイルの変数定義
Print #FileNO, "set cmdTxtPath=" & CommandFileFullPath
'ログファイル生成場所の変数定義
Print #FileNO, "set cmdLogPath=" & strFilePath & "\ftplog"
'日付を取得及び変数定義
Print #FileNO, "set cmdDateA=%date%"
'必要箇所文字を取り出し結合
Print #FileNO, "set cmdDateB=%cmdDateA:~0,4%%cmdDateA:~-5,2%%cmdDateA:~-2,2%"
'時刻を取得及び変数定義
'空白を0に置き換え格納
Print #FileNO, "set cmdTimeA=%time: =0%"
'必要箇所文字を取り出し結合
Print #FileNO, "set cmdTimeB=%cmdTimeA:~0,2%%cmdTimeA:~3,2%%cmdTimeA:~6,2%"
'ログを保存するフォルダ作成
Print #FileNO, "mkdir " & """%cmdLogPath%\"""
'コマンドファイル実行及び④ログファイルの生成
Print #FileNO, "ftp -vni -s:%cmdTxtPath%>%cmdLogPath%\%cmdDateB%_%cmdTimeB%.txt"
'実行コマンドファイルの削除①
Print #FileNO, "del %cmdTxtPath%"

Close #FileNO 'ファイルを閉じる
'---------------------------------------------------------------------
'作成した実行バッチファイルをタスクに登録 ③

'.txt数をカウント   ③-1
Dim buf As String, i As Long
Dim FindExtension As String
Dim CountFiles As Long

FindExtension = "txt"   'カウントする拡張子
i = 0                   '一応
buf = Dir(strFilePath & "\*." & FindExtension)
Do While buf <> ""
    i = i + 1
    buf = Dir()
Loop

CountFiles = i          '合致した該当拡張子のファイル数

'タスク実行時間設定 ③-2
'エラー回避の為の時間形式変換(念のため)
strTime = Format(TimeValue(strTime), "h:mm")
'タスク実行時間を既存の.txt数で設定
    'CountFilesは①で作成される為、最低1つは存在する
    'そのため、初回実行時刻はstrTime+Intervalになる
    '例えばstrTimeを"20:00"に設定し
    'Intervalを20と設定した場合
    '初回実行時間は"20:20"になる
    '[-1]で処理も出来るが何らかのエラー発生事態を考え回避した

strTime = Format(DateAdd("n", Interval * CountFiles, strTime), "h:mm")

'タスクに登録       ③-3
Dim cmd(6) As String
Dim RetVal As Variant
Dim batPath As String

batPath = "" & BatFileFullPath & ""
cmd(1) = "at "
cmd(2) = strTime
cmd(3) = " /"
cmd(4) = "interactive "
cmd(5) = ""                         'オプション[本日1回だけ実行]

'コマンド及び実行バッチパス
cmd(6) = cmd(1) & cmd(2) & cmd(3) & cmd(4) & cmd(5) & batPath

'タスクID取得及びタスクスケジューラに登録
RetVal = Shell(cmd(6), 6)

If RetVal <> 0 Then
    MsgBox batPath & vbCr & "本日" & strTime & "実行のタスク登録が登録されました。" _
    & vbCr & CommandFileFullPath & vbCr & "タスク" & vbCr & _
    "は自動的にタスク実行後に削除されます。", vbInformation, "[タスクID]" & RetVal
Else
    MsgBox batPath & vbCr & "タスク登録は実行出来ません。", vbCritical, "[ERROR]"
End If

End Sub


Private Sub Test()
    FTPAutoTimeTaskRegist "www/test/", "C:\Temp\アップ"
End Sub


Mstask.exe(タスクスケジューラ)
  • 同一時刻に実行されるよう設定された 2 つのタスクが存在すると問題が発生することがあります。最初のタスクが正しく発行されると、[タスクを実行中] と表示されます。このとき次のタスクが実行されないと、最初のタスクの後処理が完了しないままとなります。タスクの状態が [タスクを実行中] と表示されているため、以後 Mstask.exe がタスクを実行しようとし続けても実行されません。

 

 

 

2000年01月01日|[VBサンプルコード]:[連携]