1. 如何用 VB 获取一个文件夹下的文件名列表
下面的别人写的!以前写过一个,是仿照换了时光的vbs
写的!可惜代码不知道放那里了!记得那个好像比下面的方法要简单的多
用VB函数Dir实现递归搜索目录
日期:2006-03-09 来源: 作者:
我在很久以前就实现了这个方法了.它没有采用任何的控件形式.也没有调用系统API函数FindFirst,FindNext进行递归调用,和别人有点不同的就是我用的是VB中的Dir()函数.事实上,直接采用Dir()函数是不能进行自身的递归的调用的,但我们可以采用一种办法把Dir将当前搜索目录的子目录给保存下来,然后在自身的search(strPathName)递归函数中依次进行递归的调用,这样就可以把指定的目录搜索完毕.
具体代码如下:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'函数GetExtName
'功能:得到文件后缀名(扩展名)
'输入:文件名
'输出:文件后缀名(扩展名)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function GetExtName(strFileName As String) As String
Dim strTmp As String
Dim strByte As String
Dim i As Long
For i = Len(strFileName) To 1 Step -1
strByte = Mid(strFileName, i, 1)
If strByte <> "." Then
strTmp = strByte + strTmp
Else
Exit For
End If
Next i
GetExtName = strTmp
End Function
Public Function search(ByVal strPath As String, Optional strSearch As String = "") As Boolean
Dim strFileDir() As String
Dim strFile As String
Dim i As Long
Dim lDirCount As Long
On Error GoTo MyErr
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
strFile = Dir(strPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
While strFile <> "" '搜索当前目录
DoEvents
If (GetAttr(strPath + strFile) And vbDirectory) = vbDirectory Then '如果找到的是目录
If strFile <> "." And strFile <> ".." Then '排除掉父目录(..)和当前目录(.)
lDirCount = lDirCount + 1 '将目录数增1
ReDim Preserve strFileDir(lDirCount) As String
strFileDir(lDirCount - 1) = strFile '用动态数组保存当前目录名
End If
Else
If strSearch = "" Then
Form1.List1.AddItem strPath + strFile
ElseIf LCase(GetExtName(strPath + strFile)) = LCase(GetExtName(strSearch)) Then
'满足搜索条件,则处理该文件
Form1.List1.AddItem strPath + strFile '将文件全名保存至列表框List1中
End If
End If
strFile = Dir
Wend
For i = 0 To lDirCount - 1
Form1.Label3.Caption = strPath + strFileDir(i)
Call search(strPath + strFileDir(i), strSearch) '递归搜索子目录
Next
ReDim strFileDir(0) '将动态数组清空
search = True '搜索成功
Exit Function
MyErr:
search = False '搜索失败
End Function
2. vb 获取目录所有文件名
1、首先,在电脑上打开VB6.0属性窗口,新建一个EXE工程窗口。
3. 如何用VB获得某一目录下的所有文件和文件夹名称
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 按钮 就行了 有疑问再联系我!!!!!这个 只是 文件路径 你看看 是你想要的话 我再给你写 提取文件夹和文件名的代码!!!!
4. VB.Net怎样得到某个文件夹里的所有文件名
如果想要获得当前文件中的文件名只需要String [] fileName = file.list();就可以了。如果要包括文件中的文件名就内可以用递归的方容式。下面是两个具体的实现。 其中public static String [] getFileName(String path)是只得到当前文件中的文件名。
5. VB怎样快速获取当前文件夹下的所有文件的路径(包括子目录)
有两种方法:
1、使用DIR递归,不过这是微软明确不推荐的
2、最快但不太正规的
使用 Shell "cmd /k dir c:\windows /s/b/a >>d:\jg.txt"
c:\windows 目标文件夹,当前文件夹为 app.path,你可以根据要求生成命令字符串
d:\jg.txt 为临时文件
执行这行后再读取临时文件即可。极快!
6. 用VB怎样获取一个文件夹内所有文件名
VB可以使用FileListBox 控件来获取指定文件夹内的所有文件名。
FileListBox 控件,在运行时团丛弯,在 Path 属性指定的目录中,FileListBox 控件将文件定位并列举出来。该控件用来显示所选择文件类型的文郑凳件列表。例如,可以在应用程序中创建对话框,通过它选择一个文件或者一组文件。
以下是组合使塌闷用DriveListBox 、控件DirListBox 控件和FileListBox 控件来获取硬盘下任何一个文件夹内的文件名。
PrivateSubDir1_Change()
File1.Path=Dir1.Path
EndSub
PrivateSubDrive1_Change()
Dir1.Path=Drive1.Drive
EndSub
PrivateSubFile1_Click()
DimiAsLong
Debug.Print"目录夹内存在"&File1.ListCount&"个文件。"
EndSub