找文件的问题==>

pursue 2000-04-07 04:26:00
我想把C盘所有目录下的*.txt文件Print到Form上,我知道的用到FindFirstFile和FindNextFile但不知该如何做,请指点。
...全文
177 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wgtech 2000-07-09
  • 打赏
  • 举报
回复
不好意思,看错题了,呵呵。重给你答复:
在FORM1上创建FILELISTBOX 名为:fleList,创建DIRLISTBOX 名为:dirList
然后...
Sub SearchAllFile(ByVal Path As String)
Dim i As Integer, OldPath As String, Lc As Integer
Dim OldPath2 As String
OldPath = dirList.Path
dirList.Path = Path
OldPath2 = dirList.Path
Lc = dirList.ListCount - 1
'Search Dirs
For i = 0 To Lc
SearchAllFile dirList.List(i)
Next
fleList.Path = OldPath2
'Search Files
For i = 0 To fleList.ListCount - 1
List1.AddItem dirList.Path & "\" & fleList.List(i)
Next
dirList.Path = OldPath
End Sub
wgtech 2000-07-09
  • 打赏
  • 举报
回复
你们的办法太麻烦,告诉你们一个简单的办法:
在VB中建立一个FILELIST BOX, 名为FILE1,然后...
File1.Path = "c:\"
File1.Pattern = "*.txt"
For i% = 0 to File1.ListCount - 1
Form1.Print File1.List(i)
Next i
怎么样?又快又好!!!
bluewater 2000-07-09
  • 打赏
  • 举报
回复


下面是一个列出指定目录下所有符合要求的文件名的函数:
  Public Function AutoListFiles(ByVal sDirName As String,ByVal FileFilter As String ) As Boolean
  On Error GoTo RF_ERROR
  Dim sName As String, sFile As String, sExt As String
  Dim sDirList() As String, iDirNum As Integer, i As Integer
  ′首先枚举所有文件
  sFile = Dir(sDirName + FileFilter, vbNormal + vbArchive + vbHidden)
  Do While Len(sFile) >0
  sFile = UCase(Trim(sFile))
  ′在此处可以将 sFile 加入到一个 Text 控件...
  sFile = Dir ′下一个文件
  Loop
  iDirNum = 0
  sName = Dir(sDirName + ″*.*″, vbDirectory + vbNormal)
  Do While Len(sName) >0
  If sName <> ″.″ And sName <> ″..″ Then
  iDirNum = iDirNum + 1
  ReDim Preserve sDirList(1 To iDirNum)
  sDirList(iDirNum) = sDirName + sName + ″\″
  End If
  sName = Dir ′下一个目录
  Loop
  For i = 1 To iDirNum
  AutoListFiles sDirList(i) ′递归调用
  Next
  End If
  RF_EXIT:
  AutoListFiles = True
  Exit Function
  RF_ERROR:
  MsgBox Err.Description, vbCritical, ″″
  Resume RF_EXIT
  End Function
  调用上述函数的示例:
  AutoListFiles(″C:\″,″*.txt")
  
Un1 2000-04-13
  • 打赏
  • 举报
回复
不用啦,VB中使用:

dim s as string
s = dir("*.*")

do
if len(s)<=0 then exit do
print s
s = dir()
loop

就可以了!
dengdun 2000-04-13
  • 打赏
  • 举报
回复
dim szFileName as String
szFileName = Dir("*.txt") '查找第一个txt文件。
Do While szFileName <> "" ' 开始循环。
form1.Print szFileName
szFileName = Dir ' 查找下一个txt文件。
Loop
Chen_Lin 2000-04-08
  • 打赏
  • 举报
回复
就是Trim().
pursue 2000-04-07
  • 打赏
  • 举报
回复
另外,TrimNull()是什么?
Chen_Lin 2000-04-07
  • 打赏
  • 举报
回复
Public Const MAX_PATH = 260

Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

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

Public Declare Function FindFirstFile _
Lib "kernel32" Alias "FindFirstFileA" _
(ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA) As Long

Public Declare Function FindNextFile _
Lib "kernel32" Alias "FindNextFileA" _
(ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As Long
在你自己的函数中:

Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
Dim sFile As String

hFile = FindFirstFile(sPath & "*.*" & Chr$(0), WFD)
If hFile <> -1 Then
sFile = TrimNull(WFD.cFileName)
While FindNextFile(hFile, WFD)

sFile = TrimNull(WFD.cFileName)
...
下面你自己写吧,注意"." 和".."

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧