如何用VB获得某一目录下的所有文件和文件夹名称?

如何用VB获得某一目录下的所有文件和文件夹名称?,第1张

Dim a() As String

Private Sub Dir1_Change()

File1.Path = Dir1.Path

Label1.Caption = Dir1.Path

End Sub

Private Sub Drive1_Change()

On Error GoTo err1

Dir1.Path = Drive1.Drive

Exit Sub

err1:

MsgBox Err.Description, vbInformation + vbOKOnly, "提示"

End Sub

Private Sub Command1_Click()

Dim i As Integer, j As Integer

Dim ifieldcount As Integer, irecordcount As Integer

Dim wdapp As Word.Application

Dim wddoc As Word.Document

Dim atable As Word.Table

If Option2.Value = True Then

ReDim a(1 To File1.ListCount)

For i = 1 To File1.ListCount

b = File1.List(i)

a(i) = b

Next

End If

If Option1.Value = True Then

ReDim a(1 To Dir1.ListCount)

For i = 1 To Dir1.ListCount

b = Dir1.List(i)

a(i) = b

Next

End If

End Sub

这段程序 复制过去 就行了 创建的控件有 两个option 让你选择 要提取文件夹 还是文件歼好路径的 选中option 1 就是提取文件夹的 option2 就是提取文件的 然后再创建 Drive1 Dir1 File1 这三个亮改消 选择文件路径的 控件 再加一个 command 按钮 就行了 有疑问再联系我!!!!敬知!这个 只是 文件路径 你看看 是你想要的话 我再给你写 提取文件夹和文件名的代码!!!!

给你个VB遍历文件夹的代码吧搭简

Private

Declare

Function

FindFirstFile

Lib

"kernel32"

Alias

"FindFirstFileA"

(ByVal

lpFileName

As

String,

lpFindFileData

As

WIN32_FIND_DATA)

As

Long

'查找下一个拦胡文件的API

Private

Declare

Function

FindNextFile

Lib

"kernel32"

Alias

"FindNextFileA"

(ByVal

hFindFile

As

Long,

lpFindFileData

As

WIN32_FIND_DATA)

As

Long

'获取文件属性的API

Private

Declare

Function

GetFileAttributes

Lib

"kernel32"

Alias

"GetFileAttributesA"

(ByVal

lpFileName

As

String)

As

Long

'关闭查找文件的API

Private

Declare

Function

FindClose

Lib

"kernel32"

(ByVal

hFindFile

As

Long)

As

Long

Const

MAX_PATH

=

260

Const

MAXDWORD

=

&HFFFF

Const

FILE_ATTRIBUTE_DIRECTORY

=

&H10简枝拦

Private

Type

FILETIME

dwLowDateTime

As

Long

dwHighDateTime

As

Long

End

Type

Dim

tempstr

As

String

'定义类(用于查找文件)

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

Dim

filecount

As

Integer

Dim

dirs()

As

String

Dim

curr

As

Long

Dim

ss()

As

String

Private

Sub

Command1_Click()

tempstr

=

"c:"

searchdir

tempstr

filecount

=

0

End

Sub

Public

Function

searchdir(path

As

String)

Dim

WFD

As

WIN32_FIND_DATA

Dim

i

As

Long

Dim

temp

As

String

Dim

h

As

Long

Dim

zhaodao

As

Long

Dim

iindex

As

Integer

Dim

dirs()

As

String

Dim

l

As

Long

zhaodao

=

1

h

=

FindFirstFile(path

&

"\*.*",

WFD)

If

h

<>

-1

Then

While

zhaodao

zhaodao

=

1

temp

=

Left(WFD.cFileName,

InStr(WFD.cFileName,

Chr$(0))

-

1)

If

temp

<>

"."

And

temp

<>

".."

Then

If

WFD.dwFileAttributes

And

vbDirectory

Then

ReDim

Preserve

dirs(iindex)

dirs(iindex)

=

path

&

"\"

&

temp

iindex

=

iindex

+

1

ReDim

Preserve

ss(filecount)

ss(filecount)

=

path

&

"\"

&

temp

filecount

=

filecount

+

1

End

If

End

If

zhaodao

=

FindNextFile(h,

WFD)

Wend

End

If

FindClose

(h)

If

iindex

>

0

Then

For

i

=

0

To

iindex

-

1

Call

searchdir(dirs(i))

Next

i

End

If

End

Function

解决这个问题有两种方法。

第一种:知道了文件的全路径,那么路径中当弯闭毕然也包含文件所在的文件夹信息,只要从中提取即可。例如,已知文件全路径为“C:\Windows\System32\abc.dll”并赋予变量strPt,可用过下面语句获取文件夹。

left(strPt,instrrev(strPt,"\"))

instrrev函数的作用是从右侧开始查找指定字符串,并返回数值,此处埋芹返回值为20.left函数的作用是从左往右取N个字符,此例中取20个,最后结果为:C:\Windows\System32\。

第二种方法:使用FileSystemObject对象。代码如下:

dim fso as object, strFolder as object

set fso = createobject("scripting.filesystemobject")

set strFolder = fso.getfolder("C:\Windows\System32\abc.dll"态颤)

msgbox strFolder.path

文件系统对象FSO的英文全称是File System Object ,这种对象模型提出了有别于传统的文件 *** 作语句处理文件和文件夹的方法。通过采用object.method这种在面向对象编程中广泛使用的语法,将一系列 *** 作文件和文件夹的动作通过调用对象本身的属性直接实现。


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

原文地址: http://outofmemory.cn/tougao/12221547.html

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

发表评论

登录后才能评论

评论列表(0条)

保存