WSH のテストをする場合、GUI を使用するとどうしても Wscript.exe が起動されて、デバッグの為に表示している Wscript.Echo が 実行毎にダイアログ表示になってしまいます。これでは、ループ内で処理している場合等は悲惨な事になります。 そうならない為の関数です。 具体的には cscript で実行して無かった場合、コマンドプロンプトを開いて、そこから cscript で自分自身を実行させて、終了したら PAUSE します。( Wscript で実行されたスクリプトは終了させます ) ▼ 以下のコードでは、Crun 関数がその部分ですが、それを利用して『デスクトップのショートカットのアイコン情報』の一覧を表示しています。
Call Crun() Set Shell = WScript.CreateObject( "Shell.Application" ) Set WshShell = WScript.CreateObject( "WScript.Shell" ) ' デスクトップ Set objFolder = Shell.NameSpace( 0 ) Set objFolderItems = objFolder.Items() ' 一覧の数 nCount = objFolderItems.Count ' デスクトップ一覧の列挙 For i = 0 to nCount - 1 strPath = objFolderItems.Item(i).Path ' パスをピリオドで分解 aData = Split( strPath, "." ) ' 配列の上限値より、一番右端にある拡張子を取得 strTarget = aData( Ubound( aData ) ) ' 大文字に変換 strTarget = Ucase( strTarget ) if strTarget = "LNK" then Set oShellLink = WshShell.CreateShortcut(strPath) strTarget = oShellLink.IconLocation strTarget = WshShell.ExpandEnvironmentStrings(strTarget) if strTarget = ",0" then ' アイコンは、TargetPath の 0 番目のアイコンを使用 Wscript.Echo oShellLink.TargetPath else ' アイコンは、strTarget を , で分解して抽出 Wscript.Echo strTarget end if end if Next ' ********************************************************** ' Cscript.exe で実行を強制 ' Cscript.exe の実行終了後 pause で一時停止 ' ********************************************************** Function Crun( ) Dim str,WshShell str = WScript.FullName str = Right( str, 11 ) str = Ucase( str ) if str <> "CSCRIPT.EXE" then str = WScript.ScriptFullName Set WshShell = CreateObject( "WScript.Shell" ) strParam = " " For I = 0 to Wscript.Arguments.Count - 1 if instr(Wscript.Arguments(I), " ") < 1 then strParam = strParam & Wscript.Arguments(I) & " " else strParam = strParam & Dd(Wscript.Arguments(I)) & " " end if Next Call WshShell.Run( "cmd.exe /c cscript.exe " & Dd(str) & strParam & " & pause", 1 ) WScript.Quit end if End Function ' ********************************************************** ' 文字列を " で囲む関数 ' ********************************************************** Function Dd( strValue ) Dd = """" & strValue & """" End function
|
【VBScript ベーシックの最新記事】
【VBScript関連のカテゴリ】