■ 前提条件 cscript.exe で実行される事 使用されるオブジェクト 1) Shell.Application : フォルダ選択 / ファイルの圧縮 2) Scripting.FileSystemObject : 空のzip書庫作成 3) WScript.Shell : カレントディレクトリ取得 4) WMI : プロセスの変化の非同期監視処理 厳密な処理ではありませんが、VBScript のテクニカルな処理のヒント集のような ものです。特に、WMI では一般的に知られていない処理が大量にあり、これはその ほんの一つのサンプルです
str = WScript.FullName str = Right( str, 11 ) str = Ucase( str ) if str <> "CSCRIPT.EXE" then Wscript.Echo "cscript.exe で実行して下さい" Wscript.Quit end if ' ********************************************************** ' 必要な基本オブジェクト ' ********************************************************** Set Shell = WScript.CreateObject("Shell.Application") Set Fso = WScript.CreateObject("Scripting.FileSystemObject") Set WshShell = WScript.CreateObject("WScript.Shell") ' ********************************************************** ' 圧縮フォルダ選択 ' ********************************************************** Set objFolder = Shell.BrowseForFolder( 0, "フォルダ選択", 11, 0 ) if objFolder is nothing then WScript.Quit end if if not objFolder.Self.IsFileSystem then WScript.Echo "ファイルシステムではありません" WScript.Quit end if ' ********************************************************** ' 空の zip 書庫作成 ' ********************************************************** Set Handle = Fso.CreateTextFile( "test.zip", True ) EmptyData = Chr(&H50) & Chr(&H4B) & Chr(&H5) & Chr(&H6) EmptyData = EmptyData & String( 18, Chr(0) ) Handle.Write EmptyData Handle.Close ' ********************************************************** ' 圧縮 ' ********************************************************** strTargetZipFile = WshShell.CurrentDirectory & "\test.zip" Set objTargetFolder = Shell.NameSpace( strTargetZipFile ) ' 非同期の圧縮処理となります Call objTargetFolder.CopyHere( objFolder.Items(), 0 ) ' ********************************************************** ' WMI 非同期イベントの監視 ' ********************************************************** Set SINK = WScript.CreateObject("WbemScripting.SWbemSink","SINK_") Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") objWMIService.ExecNotificationQueryAsync _ SINK, _ "SELECT * FROM __InstanceOperationEvent WITHIN 1 " & _ "WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name = 'explorer.exe'" Wscript.Echo "ZIP保存処理を開始します(★終了するまで何も触らないで下さい)" ' ********************************************************** ' 3秒毎に処理が終了したか判断して終了します ' ********************************************************** Do sv_counter = counter WScript.Sleep 3000 if sv_counter = counter then Wscript.Echo "処理を終了します" Wscript.quit end if Loop ' ********************************************************** ' この処理は、explorer.exe の動作中に呼び出されます ' ********************************************************** Sub SINK_OnObjectReady(objLatestEvent, objAsyncContext) counter = counter + 1 Wscript.Echo "処理中 : " & objLatestEvent.TargetInstance.Name End Sub
|