Lothar Haensler
November 8th, 1999, 06:16 AM
some "Anonymous" poster in the C++ forum just posted an anwser to a question and mentioned the shlwapi.dll.
This DLL comes with IE 4 and above and contains a whole lot of functions for Path and directory handling that are very useful to VB programmers.
I wrote a class module that encapsulates some of the functions and a sample for using it:
Dim o as new stdPath
o.spec = "C:\winnt"
Debug.print "dir: " & o.spec
Debug.print "isdir? " & o.IsDir
Debug.print "empty? " & o.IsDirEmpty
Debug.print "isurl? " & o.isURL
Debug.print "isurl(file:///c:\test.htm? " & o.isURL("file:///c:\test.htm")
Debug.print "isunc? " & o.isUNC
Debug.print "isunc(file:///c:\test.htm?) " & o.isUNC("file:///c:\test.htm")
Debug.print "isunc(\\fns3\swdev_t\test) " & o.isUNC("\\fns3\swdev_t\test")
Debug.print "isnetworkpath: " & o.isNetworkPath("v:\test")
Debug.print "addBackSlash: " & o.AddBackslash("c:\test")
Debug.print "removeBackSlash: " & o.RemoveBackslash("c:\test\")
Debug.print "matchspec: c:\test\test.txt, *.txt): " & o.MatchSpec("*.txt", "c:\test\test.TXt")
Debug.print "strippath: " & o.StripPath("c:\winnt\system32\odbc32.dll")
See how easy it is to check for the existence of a file or directory...
the class module:
option Explicit
private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (byval strPath as string) as Long
private Declare Function PathIsDirectory Lib "shlwapi.dll" Alias "PathIsDirectoryA" (byval strPath as string) as Long
private Declare Function PathIsDirectoryEmpty Lib "shlwapi.dll" Alias "PathIsDirectoryEmptyA" (byval strPath as string) as Long
private Declare Function PathIsURL Lib "shlwapi.dll" Alias "PathIsURLA" (byval strPath as string) as Long
private Declare Function PathIsUNC Lib "shlwapi.dll" Alias "PathIsUNCA" (byval strPath as string) as Long
private Declare Function PathIsNetworkPath Lib "shlwapi.dll" Alias "PathIsNetworkPathA" (byval strPath as string) as Long
private Declare Function PathAddBackslash Lib "shlwapi.dll" Alias "PathAddBackslashA" (byval strPath as string) as string
private Declare Function PathRemoveBackslash Lib "shlwapi.dll" Alias "PathRemoveBackslashA" (byval strPath as string) as string
private Declare Function PathMatchSpec Lib "shlwapi.dll" Alias "PathMatchSpecA" (byval strPath as string, byval strSpec as string) as Long
private Declare Sub PathStripPath Lib "shlwapi.dll" Alias "PathStripPathA" (byval strPath as string)
Dim mstrSpec as string
public property let spec(byval strSpec as string)
mstrSpec = strSpec
End property
public property get spec() as string
spec = mstrSpec
End property
public Function FileExists(byval strSpec as string) as Boolean
FileExists = PathFileExists(strSpec) <> 0
End Function
public Function IsDir(optional byval strSpec as string) as Boolean
If strSpec = "" then strSpec = mstrSpec
IsDir = PathIsDirectory(strSpec) <> 0
End Function
public Function IsDirEmpty(optional byval strSpec as string) as Boolean
If strSpec = "" then strSpec = mstrSpec
IsDirEmpty = PathIsDirectoryEmpty(strSpec) <> 0
End Function
public Function isURL(optional byval strSpec as string) as Boolean
If strSpec = "" then strSpec = mstrSpec
isURL = PathIsURL(strSpec) <> 0
End Function
public Function isUNC(optional byval strSpec as string) as Boolean
If strSpec = "" then strSpec = mstrSpec
isUNC = PathIsUNC(strSpec) <> 0
End Function
public Function isNetworkPath(optional byval strSpec as string) as Boolean
If strSpec = "" then strSpec = mstrSpec
isNetworkPath = PathIsNetworkPath(strSpec) <> 0
End Function
public Function AddBackslash(optional byval strSpec as string) as string
If strSpec = "" then strSpec = mstrSpec
Dim strBuff as string * 255
strBuff = strSpec & vbNullChar
Call PathAddBackslash(strBuff)
AddBackslash = Left$(strBuff, InStr(strBuff, vbNullChar) - 1)
End Function
public Function RemoveBackslash(optional byval strSpec as string) as string
If strSpec = "" then strSpec = mstrSpec
Dim strBuff as string * 255
strBuff = strSpec & vbNullChar
Call PathRemoveBackslash(strBuff)
RemoveBackslash = Left$(strBuff, InStr(strBuff, vbNullChar) - 1)
End Function
public Function MatchSpec(byval strSpec as string, optional byval strPath as string) as Boolean
If strPath = "" then strPath = mstrSpec
MatchSpec = PathMatchSpec(strPath, strSpec) <> 0
End Function
public Function StripPath(optional byval strSpec as string) as string
Dim strTemp as string * 255
If strSpec = "" then
strTemp = mstrSpec
else
strTemp = strSpec
End If
Call PathStripPath(strTemp)
StripPath = strTemp
End Function
There are many more functions in this DLL, but the sample shows how easy it is to make use of that functionality.
(the addBackSlash and RemoveBackslash functions are, of course, easier to implement in VB...).
This DLL comes with IE 4 and above and contains a whole lot of functions for Path and directory handling that are very useful to VB programmers.
I wrote a class module that encapsulates some of the functions and a sample for using it:
Dim o as new stdPath
o.spec = "C:\winnt"
Debug.print "dir: " & o.spec
Debug.print "isdir? " & o.IsDir
Debug.print "empty? " & o.IsDirEmpty
Debug.print "isurl? " & o.isURL
Debug.print "isurl(file:///c:\test.htm? " & o.isURL("file:///c:\test.htm")
Debug.print "isunc? " & o.isUNC
Debug.print "isunc(file:///c:\test.htm?) " & o.isUNC("file:///c:\test.htm")
Debug.print "isunc(\\fns3\swdev_t\test) " & o.isUNC("\\fns3\swdev_t\test")
Debug.print "isnetworkpath: " & o.isNetworkPath("v:\test")
Debug.print "addBackSlash: " & o.AddBackslash("c:\test")
Debug.print "removeBackSlash: " & o.RemoveBackslash("c:\test\")
Debug.print "matchspec: c:\test\test.txt, *.txt): " & o.MatchSpec("*.txt", "c:\test\test.TXt")
Debug.print "strippath: " & o.StripPath("c:\winnt\system32\odbc32.dll")
See how easy it is to check for the existence of a file or directory...
the class module:
option Explicit
private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (byval strPath as string) as Long
private Declare Function PathIsDirectory Lib "shlwapi.dll" Alias "PathIsDirectoryA" (byval strPath as string) as Long
private Declare Function PathIsDirectoryEmpty Lib "shlwapi.dll" Alias "PathIsDirectoryEmptyA" (byval strPath as string) as Long
private Declare Function PathIsURL Lib "shlwapi.dll" Alias "PathIsURLA" (byval strPath as string) as Long
private Declare Function PathIsUNC Lib "shlwapi.dll" Alias "PathIsUNCA" (byval strPath as string) as Long
private Declare Function PathIsNetworkPath Lib "shlwapi.dll" Alias "PathIsNetworkPathA" (byval strPath as string) as Long
private Declare Function PathAddBackslash Lib "shlwapi.dll" Alias "PathAddBackslashA" (byval strPath as string) as string
private Declare Function PathRemoveBackslash Lib "shlwapi.dll" Alias "PathRemoveBackslashA" (byval strPath as string) as string
private Declare Function PathMatchSpec Lib "shlwapi.dll" Alias "PathMatchSpecA" (byval strPath as string, byval strSpec as string) as Long
private Declare Sub PathStripPath Lib "shlwapi.dll" Alias "PathStripPathA" (byval strPath as string)
Dim mstrSpec as string
public property let spec(byval strSpec as string)
mstrSpec = strSpec
End property
public property get spec() as string
spec = mstrSpec
End property
public Function FileExists(byval strSpec as string) as Boolean
FileExists = PathFileExists(strSpec) <> 0
End Function
public Function IsDir(optional byval strSpec as string) as Boolean
If strSpec = "" then strSpec = mstrSpec
IsDir = PathIsDirectory(strSpec) <> 0
End Function
public Function IsDirEmpty(optional byval strSpec as string) as Boolean
If strSpec = "" then strSpec = mstrSpec
IsDirEmpty = PathIsDirectoryEmpty(strSpec) <> 0
End Function
public Function isURL(optional byval strSpec as string) as Boolean
If strSpec = "" then strSpec = mstrSpec
isURL = PathIsURL(strSpec) <> 0
End Function
public Function isUNC(optional byval strSpec as string) as Boolean
If strSpec = "" then strSpec = mstrSpec
isUNC = PathIsUNC(strSpec) <> 0
End Function
public Function isNetworkPath(optional byval strSpec as string) as Boolean
If strSpec = "" then strSpec = mstrSpec
isNetworkPath = PathIsNetworkPath(strSpec) <> 0
End Function
public Function AddBackslash(optional byval strSpec as string) as string
If strSpec = "" then strSpec = mstrSpec
Dim strBuff as string * 255
strBuff = strSpec & vbNullChar
Call PathAddBackslash(strBuff)
AddBackslash = Left$(strBuff, InStr(strBuff, vbNullChar) - 1)
End Function
public Function RemoveBackslash(optional byval strSpec as string) as string
If strSpec = "" then strSpec = mstrSpec
Dim strBuff as string * 255
strBuff = strSpec & vbNullChar
Call PathRemoveBackslash(strBuff)
RemoveBackslash = Left$(strBuff, InStr(strBuff, vbNullChar) - 1)
End Function
public Function MatchSpec(byval strSpec as string, optional byval strPath as string) as Boolean
If strPath = "" then strPath = mstrSpec
MatchSpec = PathMatchSpec(strPath, strSpec) <> 0
End Function
public Function StripPath(optional byval strSpec as string) as string
Dim strTemp as string * 255
If strSpec = "" then
strTemp = mstrSpec
else
strTemp = strSpec
End If
Call PathStripPath(strTemp)
StripPath = strTemp
End Function
There are many more functions in this DLL, but the sample shows how easy it is to make use of that functionality.
(the addBackSlash and RemoveBackslash functions are, of course, easier to implement in VB...).