|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Prerequisites |
Version 4.71 and later of Shlwapi.dll. |
|
Windows
Shlwapi.dll exposes a great number of path-oriented functions designed for use by the Windows shell. For more information on the shell
APIs please see PathFileExists: A Local/Network File/Folder/Drive Exists Routine.
PathQuoteSpaces searches a path for spaces, and if spaces are found anywhere in the string, the entire path is enclosed in quotation marks. lpsz points to a buffer with a string containing the path to search. The size of this buffer should be padded to MAX_PATH to ensure that it is large enough to hold the returned string. The target paths do not have to exist for this call. The illustration shows the result of the call when each filename was altered from "somefile.txt" to "some file.txt". In order to show a complete demo using a combination of path and file strings representing typical application needs, each demo uses the same 26 strings as its test bed. This provides the opportunity to see exactly how each category of path information is handled by a specific call, as well as provide a single test bed upon which you can reuse the form while building a BAS module that contains just the functions you need. To create the environment to test these routines, I created a folder on C:\ called 'coolfocus', which contained a subfolder named javatools. In the javatools folder I placed a file named 'somefile.txt'. I also placed a copy of the somefile.txt in the c:\ root folder. I shared the coolfocus folder, and mapped a drive to it (drive m). The form code below creates and labels the controls appropriate for this demo by just placing a single text box and label on the form, and setting the index of each to 0 in order to create a control array. |
BAS Module Code |
Place the following code into the general declarations area of a bas module: |
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Declare Function PathQuoteSpaces Lib "shlwapi" _ Alias "PathQuoteSpacesA" _ (ByVal pPath As String) As Long Public Function SetPathQuotes(ByVal sPath As String) As String 'Searches a path for spaces. If spaces are found, 'the entire path is enclosed in quotation marks. 'buff is a string containing the path to search. 'The size of this buffer should be set to MAX_PATH 'to ensure that it is large enough to hold the 'returned string. 'Given a path and/or filename, return 'a string enclosed in quotes if the 'passed string contains spaces 'Requires Version 4.71 and later of Shlwapi.dll Dim buff As String 'buffer must be max_path in length buff = sPath & Chr$(0) & Space$(MAX_PATH - Len(sPath)) Call PathQuoteSpaces(buff) SetPathQuotes = TrimNull(buff) End Function Private Function TrimNull(item As String) Dim pos As Integer pos = InStr(item, Chr$(0)) If pos Then TrimNull = Left$(item, pos - 1) Else TrimNull = item End If End Function |
Form Code |
Add one label (Label1), one text box (Text1) to a form. Set the index property of each to 0 to create control arrays; the demo load event code will create and label the needed controls. Add a command button (Command1), along with the following code: |
|
Option Explicit Private Sub Form_Load() Dim cnt As Long For cnt = 0 To 25 If cnt > 0 Then Load Label1(cnt) Load Text1(cnt) End If With Label1(cnt) .Left = 120 .Top = 250 + (300 * cnt) .Visible = True End With With Text1(cnt) .Left = 3300 .Top = 210 + (300 * cnt) .Width = 3300 .Visible = True End With Next With Command1 .Left = 120 .Top = Text1(25).Top + (Text1(18).Height * 1.5) .Width = 2300 .Caption = "SetPathQuotes" End With 'display the strings that will be used in the call 'file only Label1(0).Caption = "some file.txt" 'absolute paths Label1(1).Caption = "c:\" Label1(2).Caption = "c:\some file.txt" Label1(3).Caption = "c:\javatools\" Label1(4).Caption = "c:\javatools\some file.txt" 'relative paths Label1(5).Caption = "..\..\" Label1(6).Caption = "..\..\some file.txt" Label1(7).Caption = "..\..\javatools\" Label1(8).Caption = "..\..\javatools\some file.txt" 'mapped drive Label1(9).Caption = "m:\" Label1(10).Caption = "m:\some file.txt" Label1(11).Caption = "m:\coolfocus\" Label1(12).Caption = "m:\coolfocus\some file.txt" 'UNC Label1(13).Caption = "\\workstation" Label1(14).Caption = "\\workstation\some file.txt" Label1(15).Caption = "\\workstation\javatools" Label1(16).Caption = "\\workstation\javatools\some file.txt" 'IP address Label1(17).Caption = "\\192.168.1.100" Label1(18).Caption = "\\192.168.1.100\some file.txt" Label1(19).Caption = "\\192.168.1.100\d$" Label1(20).Caption = "\\192.168.1.100\d$\javatools\" Label1(21).Caption = "\\192.168.1.100\d$\javatools\some file.txt" 'URL Label1(22).Caption = "ms.htm" Label1(23).Caption = "www.microsoft.com" Label1(24).Caption = "http://www.microsoft.com" Label1(25).Caption = "http://www.microsoft.com/ms.htm" End Sub Private Sub Command1_Click() 'Searches a string using a DOS 'wild card match type. 'as filename alone Text1(0).Text = SetPathQuotes(Label1(0).Caption) 'via absolute path Text1(1).Text = SetPathQuotes(Label1(1).Caption) Text1(2).Text = SetPathQuotes(Label1(2).Caption) Text1(3).Text = SetPathQuotes(Label1(3).Caption) Text1(4).Text = SetPathQuotes(Label1(4).Caption) 'via relative path Text1(5).Text = SetPathQuotes(Label1(5).Caption) Text1(6).Text = SetPathQuotes(Label1(6).Caption) Text1(7).Text = SetPathQuotes(Label1(7).Caption) Text1(8).Text = SetPathQuotes(Label1(8).Caption) 'via mapped drive Text1(9).Text = SetPathQuotes(Label1(9).Caption) Text1(10).Text = SetPathQuotes(Label1(10).Caption) Text1(11).Text = SetPathQuotes(Label1(11).Caption) Text1(12).Text = SetPathQuotes(Label1(12).Caption) 'via UNC Text1(13).Text = SetPathQuotes(Label1(13).Caption) Text1(14).Text = SetPathQuotes(Label1(14).Caption) Text1(15).Text = SetPathQuotes(Label1(15).Caption) Text1(16).Text = SetPathQuotes(Label1(16).Caption) 'via IP address Text1(17).Text = SetPathQuotes(Label1(17).Caption) Text1(18).Text = SetPathQuotes(Label1(18).Caption) Text1(19).Text = SetPathQuotes(Label1(19).Caption) Text1(20).Text = SetPathQuotes(Label1(20).Caption) Text1(21).Text = SetPathQuotes(Label1(21).Caption) 'via URL Text1(22).Text = SetPathQuotes(Label1(22).Caption) Text1(23).Text = SetPathQuotes(Label1(23).Caption) Text1(24).Text = SetPathQuotes(Label1(24).Caption) Text1(25).Text = SetPathQuotes(Label1(25).Caption) End Sub |
Comments |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |