如何得到一个文件夹的所有子文件夹和文件?
如何得到一个文件夹的所有子文件夹和文件? 问题点数:100、回复次数:5Top
1 楼288794()回复于 2002-03-15 16:54:26 得分 10
最简单的方法就是使用 Shell 对象
在你的工程里面引用 Shell32.Dll
然后在工程里面:
dim m_Shell as New Shell
dim Folder as Folder
dim FolderItem as FolderItem
dim FolderItems as FolderItems
set Folder=m_shell.nam???(Path) 'Path是文件夹的路记径,nam???是个函数,我在网吧上网记的不太清楚在VB里面输入na后回提示你的了。
set FolderItems=Folder.Items
for each FolderItem in FolderItems
'FolderItem 就是你所需要的了,如果在 WimMe以上版本的Window里面还有很多功能.
next
Top
2 楼wjying(葡萄)回复于 2002-03-15 18:05:34 得分 20
获得一个目录下所有子文件夹名和文件名
使用FSO
Function Index(strFolder)
Dim filesys As Object
Dim folder As Object
Dim file As Object
Set filesys = CreateObject("Scripting.FileSystemObject"
Set folder = filesys.getfolder(strFolder)
For Each f In folder.Files
Set file = filesys.getfile(f)
Next
For Each sfd In folder.subfolders
Index sfd
Next
Set file = Nothing
Set folder = Nothing
Set filesys = Nothing
End Function
Top
3 楼SKJG(司空见惯)回复于 2002-03-15 22:18:55 得分 50
public sub GetAll(Folder as string)
dim temp as string
dim FullName as string
temp=dir(Folder) 'dir函数的第二个参数我不记得了,请查msdn
while temp<>""
FullName=Folder & temp '找到的文件或文件夹的全名
temp=dir
wend
end sub
Top
4 楼sonicdater(事过境迁,除了技术,我什么都没有了)回复于 2002-03-15 23:04:42 得分 20
Create a new project with a form containing five text boxes (Text1, Text2, Text3, Text4, Text5), a check box (Check1), a list box (List1), two option buttons in an option array (Option1(0), Option1(1), and a command button (Command1). Add labels as desired along with the following code.
------------------------------------------------------------------
Private Const vbDot = 46
Private Const MAX_PATH = 260
Private Const INVALID_HANDLE_VALUE = -1
Private Const vbBackslash = "\"
Private Const ALL_FILES = "*.*"
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Private Type FILE_PARAMS
bRecurse As Boolean
bFindOrExclude As Long '1=find matching, 0=exclude matching
nCount As Long
nSearched As Long
sFileNameExt As String
sFolderExt As String
sFileRoot As String
End Type
Private Declare Function FindClose Lib "kernel32" _
(ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" _
Alias "FindFirstFileA" _
(ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" _
Alias "FindNextFileA" _
(ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Function lstrlen Lib "kernel32" _
Alias "lstrlenW" (ByVal lpString As Long) As Long
Private Declare Function PathMatchSpec Lib "shlwapi" _
Alias "PathMatchSpecW" _
(ByVal pszFileParam As Long, _
ByVal pszSpec As Long) As Long
Private fp As FILE_PARAMS 'holds search parameters
Private Sub Form_Load()
Text1.Text = "c:\inetpub"
Text2.Text = "_vti*"
Option1(0).Value = True
Command1.Caption = "Begin Search"
End Sub
Private Sub Command1_Click()
Dim tstart As Single 'timer var for this routine only
Dim tend As Single 'timer var for this routine only
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
List1.Clear
List1.Visible = False
With fp
.sFileRoot = QualifyPath(Text1.Text) 'start path
.sFolderExt= Text2.Text 'folder type(s) of interest
.bRecurse = Check1.Value = 1 'True = recursive search
.nCount = 0 'results
.nSearched = 0 'results
.bFindOrExclude = IIf(Option1(0).Value = True, 1, 0) '0=include, 1=exclude
End With
tstart = GetTickCount()
Call SearchForFolders(fp.sFileRoot)
tend = GetTickCount()
List1.Visible = True
Text3.Text = Format$(fp.nSearched, "###,###,###,##0")
Text4.Text = Format$(fp.nCount, "###,###,###,##0")
Text5.Text = FormatNumber((tend - tstart) / 1000, 2) & " seconds"
End Sub
Private Sub SearchForFolders(sRoot As String)
Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
hFile = FindFirstFile(sRoot & ALL_FILES, WFD)
If hFile <> INVALID_HANDLE_VALUE Then
Do
If (WFD.dwFileAttributes And vbDirectory) Then
If Asc(WFD.cFileName) <> vbDot Then
If fp.bRecurse Then
'check if the folder name matches the
'parameters specified for exclusion
If MatchSpec(WFD.cFileName, fp.sFolderExt) Then
fp.nCount = fp.nCount + 1
'list the full path to the folder
List1.AddItem sRoot & TrimNull(WFD.cFileName)
End If
'if a folder and recurse was specified,
'call method again passing the current folder
SearchForFolders sRoot & _
TrimNull(WFD.cFileName) & _
vbBackslash
End If
End If
End If 'If WFD.dwFileAttributes
fp.nSearched = fp.nSearched + 1
Loop While FindNextFile(hFile, WFD)
End If 'If hFile
Call FindClose(hFile)
End Sub
Private Function QualifyPath(sPath As String) As String
If Right$(sPath, 1) <> vbBackslash Then
QualifyPath = sPath & vbBackslash
Else: QualifyPath = sPath
End If
End Function
Private Function TrimNull(startstr As String) As String
TrimNull = Left$(startstr, lstrlen(StrPtr(startstr)))
End Function
Private Function MatchSpec(sFile As String, sSpec As String) As Boolean
MatchSpec = PathMatchSpec(StrPtr(sFile), StrPtr(sSpec)) = fp.bFindOrExclude
End Function
Top
5 楼zyl910(编程的乐趣在于编程控制硬件,与用图形学实现绚丽效果)回复于 2002-03-15 23:07:52 得分 0
来晚了!内容同上!(蹭点分)Top




