'You Can Use FSO!Working with Folders This list shows common folder tasks and the methods for doing them:Task Method Create a folder FileSystemObject.CreateFolder Delete a folder Folder.Delete or FileSystemObject.DeleteFolder Move a folder Folder.Move or FileSystemObject.MoveFolder Copy a folder Folder.Copy or FileSystemObject.CopyFolder Retrieve the name of a folder Folder.Name Find out if a folder exists on a drive FileSystemObject.FolderExists Get an instance of an existing Folder object FileSystemObject.GetFolder Find out the name of a folder's parent folder FileSystemObject.GetParentFolderName Find out the path of system folders FileSystemObject.GetSpecialFolder Example This example demonstrates usage of the Folder and FileSystemObject objects to manipulate folders and gain information about them:Private Sub Command10_Click() ' Get instance of FileSystemObject. Dim fso As New FileSystemObject, fldr As Folder, s As String ' Get Drive object. Set fldr = fso.GetFolder("c:") ' Print parent folder name. Debug.Print "Parent folder name is: " & fldr ' Print drive name. Debug.Print "Contained on drive " & fldr.Drive ' Print root file name. If fldr.IsRootFolder = True Then Debug.Print "This folder is a root folder." Else Debug.Print "This folder isn't a root folder." End If ' Create a new folder with the FileSystemObject object. fso.CreateFolder ("c:\Bogus") Debug.Print "Created folder C:\Bogus" ' Print the base name of the folder. Debug.Print "Basename = " & fso.GetBaseName("c:\bogus") ' Get rid of the newly-created folder. fso.DeleteFolder ("c:\Bogus") Debug.Print "Deleted folder C:\Bogus" End Sub'More Information You Can Consult MSDN.
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BROWSEINFO) As Long Private Const BIF_RETURNONLYFSDIRS = &H1 Private Const BIF_NEWDIALOGSTYLE = &H40 Private Type BROWSEINFO hOwner As Long pidlRoot As Long pszDisplayName As String lpszTitle As String ulFlags As Long lpfn As Long lParam As Long iImage As Long End Type Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongPrivate Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long lpstrInitialDir As String lpstrTitle As String flags As Long nFileOffset As Integer nFileExtension As Integer lpstrDefExt As String lCustData As Long lpfnHook As Long lpTemplateName As String End Type'浏览文件夹函数 Public Function BrowseFolder(ByVal hwnd As Long, ByVal Title As String) As String Dim bi As BROWSEINFO Dim rtn, pid As Long Dim path As String * 512 Dim pos As Integer With bi .hOwner = hwnd .ulFlags = BIF_RETURNONLYFSDIRS Or BIF_NEWDIALOGSTYLE .lpszTitle = Title End With pid = SHBrowseForFolder(bi) rtn = SHGetPathFromIDList(ByVal pid, ByVal path) If rtn Then pos = InStr(path, Chr(0)) BrowseFolder = Left(path, pos - 1) Else BrowseFolder = "" End If End Function Private Sub Command1_Click() Text1.Text = BrowseFolder(Me.hwnd, "请选择文件夹") End Sub
This list shows common folder tasks and the methods for doing them:Task Method
Create a folder FileSystemObject.CreateFolder
Delete a folder Folder.Delete or
FileSystemObject.DeleteFolder
Move a folder Folder.Move or
FileSystemObject.MoveFolder
Copy a folder Folder.Copy or
FileSystemObject.CopyFolder
Retrieve the name of a folder Folder.Name
Find out if a folder exists on a drive FileSystemObject.FolderExists
Get an instance of an existing Folder object FileSystemObject.GetFolder
Find out the name of a folder's parent folder FileSystemObject.GetParentFolderName
Find out the path of system folders FileSystemObject.GetSpecialFolder
Example
This example demonstrates usage of the Folder and FileSystemObject objects to manipulate folders and gain information about them:Private Sub Command10_Click()
' Get instance of FileSystemObject.
Dim fso As New FileSystemObject, fldr As Folder, s As String
' Get Drive object.
Set fldr = fso.GetFolder("c:")
' Print parent folder name.
Debug.Print "Parent folder name is: " & fldr
' Print drive name.
Debug.Print "Contained on drive " & fldr.Drive
' Print root file name.
If fldr.IsRootFolder = True Then
Debug.Print "This folder is a root folder."
Else
Debug.Print "This folder isn't a root folder."
End If
' Create a new folder with the FileSystemObject object.
fso.CreateFolder ("c:\Bogus")
Debug.Print "Created folder C:\Bogus"
' Print the base name of the folder.
Debug.Print "Basename = " & fso.GetBaseName("c:\bogus")
' Get rid of the newly-created folder.
fso.DeleteFolder ("c:\Bogus")
Debug.Print "Deleted folder C:\Bogus"
End Sub'More Information You Can Consult MSDN.
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BROWSEINFO) As Long
Private Const BIF_RETURNONLYFSDIRS = &H1
Private Const BIF_NEWDIALOGSTYLE = &H40
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongPrivate Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type'浏览文件夹函数
Public Function BrowseFolder(ByVal hwnd As Long, ByVal Title As String) As String
Dim bi As BROWSEINFO
Dim rtn, pid As Long
Dim path As String * 512
Dim pos As Integer
With bi
.hOwner = hwnd
.ulFlags = BIF_RETURNONLYFSDIRS Or BIF_NEWDIALOGSTYLE
.lpszTitle = Title
End With
pid = SHBrowseForFolder(bi)
rtn = SHGetPathFromIDList(ByVal pid, ByVal path)
If rtn Then
pos = InStr(path, Chr(0))
BrowseFolder = Left(path, pos - 1)
Else
BrowseFolder = ""
End If
End Function
Private Sub Command1_Click()
Text1.Text = BrowseFolder(Me.hwnd, "请选择文件夹")
End Sub