'標準モジュール Option Explicit 'SHLWAPI.DLL ' Windows 95 + IE4.0以上(一部5.0以上) ' Windows 98 ' Windows me ' Windows NT 4.0 + IE4.0以上(一部5.0以上) ' Windows 2000 ' Windows XP ' 詳細はMSDN Library(http://msdn.microsoft.com/library/)を参照 'pathに\を追加(pathの実在チェックは行わない) Public Declare Function PathAddBackslash Lib "SHLWAPI.DLL" Alias "PathAddBackslashA" (ByVal pszPath As String) As Long 'pszPath[in,out]:path名,\を追加したpathがかえる 'path名の最後に\がついている場合はそのままのpathがかえる 'pathを指定数の文字列にコンパクトにする(pathの実在チェックは行わない) Public Declare Function PathCompactPathEx Lib "SHLWAPI.DLL" Alias "PathCompactPathExA" (ByVal pszOut As String, ByVal pszSrc As String, ByVal cchMax As Integer, ByVal dwFlags As Long) As Boolean 'pszOut[out]:コンパクトpathがかえる 'pszSrc[in]:path名 'cchMax[in]:指定文字数 'dwFlags:Reserved 'Return :成功 True、失敗 False がかえる。 'ファイルが存在するか Public Declare Function PathFileExists Lib "SHLWAPI.DLL" Alias "PathFileExistsA" (ByVal pszPath As String) As Long 'pszPath[in]:ファイル名 'Return :存在すれば 1、存在しなければ 0 がかえる。 '(注)SDKでは \\ serverのように Universal Naming Convention (UNC)を指定すると 0 がかえると記述があるが、正常に動作する。 '指定されたパスがが有効なディレクトリであるか Public Declare Function PathIsDirectory Lib "SHLWAPI.DLL" Alias "PathIsDirectoryA" (ByVal pszPath As String) As Boolean 'pszPath[in]:path名 'Return :有効な場合 True、有効でなければ False がかえる。 '指定されたパスが空のディレクトリであるか Public Declare Function PathIsDirectoryEmpty Lib "SHLWAPI.DLL" Alias "PathIsDirectoryEmptyA" (ByVal pszPath As String) As Boolean 'pszPath[in]:path名 'Return :空の場合 True、空でなければ False がかえる。 'パスとファイルのパス部分を取り除く。(pathの実在チェックは行わない) Public Declare Sub PathStripPath Lib "SHLWAPI.DLL" Alias "PathStripPathA" (ByVal pszPath As String) 'pszPath[in, out] :path名,ファイル名または最後のパス名がかえる 'パス文字列から装飾を取り除く。(pathの実在チェックは行わない) Public Declare Sub PathUndecorate Lib "SHLWAPI.DLL" Alias "PathUndecorateA" (ByVal pszPath As String) 'pszPath[in, out] :path名,装飾を取り除いたパス名がかえる 'C:\Path\File[5].txt → C:\Path\File.txt 'C:\Path\File[12] → C:\Path\File 'C:\Path\File.txt → C:\Path\File.txt 'C:\Path\[3].txt → C:\Path\[3].txt '-------------------------------------------------------------------------------------------------- 'Formモジュール Option Explicit Dim fileExists As String Dim file As String Dim StartSec As Single Dim rc As Long Dim i As Integer Private Sub cmdPathFileExists_Click() 'ファイルが存在するか PathFileExists StartSec = Timer For i = 1 To 1000 rc = PathFileExists(fileExists) Next i MsgBox Timer - StartSec & " sec" End Sub Private Sub cmdDir_Click() 'ファイルが存在するか Dir$ StartSec = Timer For i = 1 To 1000 file = Dir$(fileExists) Next i MsgBox Timer - StartSec & " sec" End Sub Private Sub cmdPathFileExists_Path_Click() 'パスが存在するか '"\\RF_NEC\C\TANKEN"のようなUNCもチェックできます。 rc = PathFileExists(Environ("windir")) MsgBox rc End Sub Private Sub cmdPathAddBackslash_Click() 'パスに\を追加(PathAddBackslash) Dim pszPath As String Dim rc As Long pszPath = Environ("windir") & String$(4096, vbNullChar) rc = PathAddBackslash(ByVal pszPath) MsgBox rc & vbCr & Left$(pszPath, InStr(pszPath, vbNullChar) - 1) End Sub Private Sub cmdPathCompactPathEx_Click() 'パスを指定数の文字列にコンパクト化する(PathCompactPathEx) Dim pszOut As String, pszSrc As String, cchMax As Long, dwFlags As Long Dim rc As Boolean pszOut = String$(4096, vbNullChar) pszSrc = Environ("windir") & "\Explorer.exe" cchMax = 22 rc = PathCompactPathEx(pszOut, ByVal pszSrc, ByVal cchMax, ByVal dwFlags) pszOut = Left$(pszOut, InStr(pszOut, vbNullChar) - 1) MsgBox pszSrc & vbCr & rc & vbCr & LenB(StrConv(pszSrc, vbFromUnicode)) & vbCr & pszOut & vbCr & LenB(StrConv(pszOut, vbFromUnicode)) End Sub Private Sub cmdPathIsDirectory_Click() '指定されたパスがが有効なディレクトリであるか(PathIsDirectory) MsgBox Environ("windir") & vbCr & PathIsDirectory(ByVal Environ("windir")) End Sub Private Sub cmdPathIsDirectoryEmpty_Click() '指定されたパスが空のディレクトリであるか(PathIsDirectoryEmpty) MsgBox Environ("windir") & vbCr & PathIsDirectoryEmpty(ByVal Environ("windir")) End Sub Private Sub cmdPathStripPath_Click() 'パスとファイルのパス部分を取り除く(PathStripPath) Dim pszPath As String pszPath = fileExists & String$(4096, vbNullChar) PathStripPath ByVal pszPath MsgBox fileExists & vbCr & Left$(pszPath, InStr(pszPath, vbNullChar) - 1) End Sub Private Sub cmdPathUndecorate_Click() 'パス文字列から装飾を取り除く(PathUndecorate) Dim wFile As String Dim pszPath As String wFile = Environ("windir") & "\test[1].gif" pszPath = wFile & String$(4096, vbNullChar) PathUndecorate ByVal pszPath MsgBox wFile & vbCr & Left$(pszPath, InStr(pszPath, vbNullChar) - 1) End Sub Private Sub Form_Open(Cancel As Integer) fileExists = Environ("windir") & "\Explorer.exe" End Sub