请教:如何用VB将C:\abc\下面的所有文本文件名,列出来放在一个richtextbox控件显示?

imur 2003-11-30 10:01:16
我现在要写个小软件,中间要用到这一招,请教请教!
...全文
151 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Europa 2003-12-01
  • 打赏
  • 举报
回复
Dir好像只能搜索一层目录吧?要是人家在其他更深层的目录下又咋办呢?!
有两个方法,用隐藏的文件控件返回,但是时间复杂度较大,另外是可以象在C++里编文件之类的方法用API。
SoHo_Andy 2003-12-01
  • 打赏
  • 举报
回复
这样也可以满足你的要求
dim filename as string
filename = Dir("C:\abc\*.txt")
Do While filename <> ""
If filename <> "." And filename <> ".." Then
RichTextBox1.Text =RichTextBox1.Text & filename & vbNewLine
End If
filename = Dir ' 查找下一个文件。
Loop
planetike 2003-12-01
  • 打赏
  • 举报
回复
用file控件。过滤一下txt,可以取得一个文件名数组的。
jpinglee 2003-12-01
  • 打赏
  • 举报
回复
用treeview吧,很高级的,给你一个自己的函数:
初始化时调用:call fileconnection("C:\abc\",false)'窗体上要一个treeview控键,名为treeview1

Public Sub fileconnection(strpath As String, flag As Boolean)
Dim fso As New FileSystemObject
Dim folder As folder
Dim node As node
Set folder = fso.GetFolder(strpath)
Dim f As File
Dim strkey As String
TreeView1.LineStyle = tvwRootLines
strkey = strpath
If flag Then
Else
Set node = TreeView1.Nodes.Add(, , strkey, folder.Path)
End If
Dim sf As folder
If folder.Files.Count > 0 Then
For Each f In folder.Files
TreeView1.Nodes.Add strkey, tvwChild, f.Path, f.Name
Next f
End If
If folder.SubFolders.Count > 0 Then
For Each sf In folder.SubFolders
TreeView1.Nodes.Add strkey, tvwChild, sf.Path, sf.Name
Call fileconnection(sf.Path, True)
Next sf
End If
Set f = Nothing
Set sf = Nothing
End Sub
northwolves 2003-11-30
  • 打赏
  • 举报
回复
无需FILEBOX控件:
Private Sub Command1_Click()
richtextbox1.Text = dirs("C:\abc\")
End Sub

Function dirs(ByVal x As String) As String
Dim sfile As String, i As Long
sfile = Dir(x + "*.txt")
dirs = sfile
i = 1
Do While Len(sfile) > 0
sfile = UCase(Trim(sfile))
i = i + 1
sfile = Dir '下一个文件
dirs = dirs & vbCrLf & sfile
Loop
End Function
imur 2003-11-30
  • 打赏
  • 举报
回复
谢谢楼上大侠的答复!

请教:File1是什么控件?

请教请教!谢谢先!
TimLee✿ 2003-11-30
  • 打赏
  • 举报
回复
用FSO~~~

和楼上差不多
captainivy 2003-11-30
  • 打赏
  • 举报
回复
File1.Path = "F:\My\TEM"
File1.Pattern = "*.txt"
RichTextBox1.Text = ""

For i = 0 To File1.ListCount - 1
RichTextBox1.SelText = File1.List(i) & vbCrLf
Next

7,762

社区成员

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

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