File1.Path = "F:\My\TEM" File1.Pattern = "*.txt" RichTextBox1.Text = ""For i = 0 To File1.ListCount - 1 RichTextBox1.SelText = File1.List(i) & vbCrLf Next
谢谢楼上大侠的答复!请教:File1是什么控件?请教请教!谢谢先!
无需FILEBOX控件: Private Sub Command1_Click() richtextbox1.Text = dirs("C:\abc\") End SubFunction 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
用file控件。过滤一下txt,可以取得一个文件名数组的。
这样也可以满足你的要求 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
用treeview吧,很高级的,给你一个自己的函数: 初始化时调用:call fileconnection("C:\abc\",false)'窗体上要一个treeview控键,名为treeview1Public 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
File1.Pattern = "*.txt"
RichTextBox1.Text = ""For i = 0 To File1.ListCount - 1
RichTextBox1.SelText = File1.List(i) & vbCrLf
Next
Private Sub Command1_Click()
richtextbox1.Text = dirs("C:\abc\")
End SubFunction 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
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
有两个方法,用隐藏的文件控件返回,但是时间复杂度较大,另外是可以象在C++里编文件之类的方法用API。
初始化时调用:call fileconnection("C:\abc\",false)'窗体上要一个treeview控键,名为treeview1Public 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