手慰みのスクリプト which

手慰みの作成スクリプト。Win2000、XP、2003で動く、はず。
どこの実行ファイルが実行されるか探す。
Win2003では where.exe として似た機能が提供されている(オプションで色々指定可)
これは拡張子無しで指定した場合、OSが実際にどれを選ぶかに近い筈。
使い方例
C:> which explorer
C:WINDOWSexplorer.exe
C:> which kernel32.dll
C:WINDOWSsystem32kernel32.dll
下記which.bat と which_f.vbs をパスが通った同ディレクトリに保存して利用。
which.bat——
@echo off
cscript //Nologo which_f.vbs %*
———-
which_f.vbs ——-
Option Explicit
Dim args,fname
Set args = Wscript.Arguments
if args.count>0 then
fname=args(0)
else
‘引数がなければ終了
wscript.quit 0
end if
call main(fname)
Sub main(strFname)
Dim strExtname
Dim strBase
Dim objFS
Dim arrPaths,strPath
Dim arrExts ,strExt
Dim tso
Set objFS=CreateObject(“Scripting.FileSystemObject”)
‘パス確定
arrPaths=GetPath()
‘拡張子確定
strExtName=””
strExtName=objFS.GetExtensionName(strFname)
arrExts=GetExt(strExtName)
‘ベース名確定
strBase=objFS.GetBaseName(strFname)
for each strPath in arrPaths
if right(strPath,1) = “” then
strPath=left(strPath,len(strPath)-1)
end if
for each strExt in arrExts
if objFS.FileExists(strPath &”” & strBase & strExt) then
WScript.echo objFS.GetAbsolutePathName(strPath &”” & strBase & strExt)
exit sub
else
‘not found
end if
next
next
end sub
Function GetExt(arg)
‘*********************************************************************
‘2006/09/30
‘ 拡張子をarrayに設定
‘*********************************************************************
Dim strExt
Dim WshShell
if arg <> “” then
‘拡張子あり
strExt=”.” & arg
else
‘拡張子なし。環境変数PATHEXTから取得
Set WshShell = WScript.CreateObject(“WScript.Shell”)
strExt = WshShell.ExpandEnvironmentStrings(“%PATHEXT%”)
end if
GetExt=Split(strExt,”;”)
end Function
Function GetPath()
‘*********************************************************************
‘2006/09/30
‘ パスをarrayに設定
‘*********************************************************************
Dim strExt
Dim WshShell
Set WshShell = WScript.CreateObject(“WScript.Shell”)
strExt = “.;”
strExt = strExt & WshShell.ExpandEnvironmentStrings(“%PATH%”)
GetPath=Split(strExt,”;”)
end Function

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA