VB 枚举指定盘符下的某类型文件(包括子目录)?

VB 枚举指定盘符下的某类型文件(包括子目录)?,第1张

这个是查找指定类型文件的函数,你调用下迅闷:

Public

Function

TreeSearch(ByVal

sPath

As

String,

ByVal

sFileSpec

As

String,

sFiles()

As

String)

As

Long

Static

lngFiles

As

Long

Dim

sDir

As

String

Dim

sSubDirs()

As

String

Dim

lngIndex

As

Long

Dim

lngTemp&

If

Right(sPath,

1)

<>

"\"

Then

sPath

=

sPath

&

"\"

sDir

=

Dir(sPath

&

sFileSpec)

Do

While

Len(sDir)

lngFiles

=

lngFiles

+

1

ReDim

Preserve

sFiles(1

To

lngFiles)

sFiles(lngFiles)

=

sPath

&

sDir

sDir

=

Dir

Loop

lngIndex

=

0

sDir

=

Dir(sPath

&

"*.*",

vbDirectory)

Do

While

Len(sDir)

If

Left(sDir,

1)

<>

"."闭缺

And

Left(sDir,

1)

<>亩态弯

".."

Then

If

GetAttr(sPath

&

sDir)

And

vbDirectory

Then

lngIndex

=

lngIndex

+

1

ReDim

Preserve

sSubDirs(1

To

lngIndex)

sSubDirs(lngIndex)

=

sPath

&

sDir

&

"\"

End

If

End

If

sDir

=

Dir

Loop

For

lngTemp

=

1

To

lngIndex

Call

TreeSearch(sSubDirs(lngTemp),

sFileSpec,

sFiles())

Next

lngTemp

TreeSearch

=

lngFiles

End

Function

'用API搜索所有文件夹

Public Sub FileSearch(ByVal sPath As String)

Static lngFiles As Long

Dim sDir As String

Dim sSubDirs() As String

Dim lngIndex As Long

Dim lngTemp&

DoEvents

If Right(sPath, 1) <搏此>"\" Then sPath = sPath &"\"

DoEvents

lngIndex = 0

sDir = Dir(sPath &"*.*", vbDirectory)

Do While Len(sDir)

If Left(sDir, 1) <>"." And Left(sDir, 1) <>".." Then

If GetAttr(sPath &sDir) And vbDirectory Then

lngIndex = lngIndex + 1

ReDim Preserve sSubDirs(1 To lngIndex)

sSubDirs(lngIndex) = sPath &sDir &"基枯迅\"

List1.AddItem sSubDirs(lngIndex)

DoEvents

End If

End If

sDir = Dir

Loop

End Sub

Private Sub Command1_Click() '测试代码

FileSearch "d:\" '败烂设置初始路径

End Sub

'添加一个按钮,一个列表框,效果很好的,不明白的加我百度HI


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12314236.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存