vb编程问题 高手进来回答一下 谢谢!
使用shell函数打开D盘下所有exe文件(包括子文件夹中的),代码写了一部分,应该在原来的基础上加几行就可以了,高手帮忙看一下,谢谢!代码如下SubGetPath(By...
使用shell函数打开D盘下所有exe文件(包括子文件夹中的),代码写了一部分,应该在原来的基础上加几行就可以了,高手帮忙看一下,谢谢!
代码如下
Sub GetPath(ByVal FilePath As String, ByVal list As ListBox)
'获取文件路径
FilePath = IIf(Right(FilePath, 1) = "\", FilePath, FilePath & "\")
'获取当前目录内的文件名
Dim FileName As String
FileName = Dir(FilePath) '初次使用dir函数需指明路径
'使用一个循环,遍历当前目录内的文件,并逐一验证其属性
Do While FileName <> ""
If Right(FileName, 3) = "jpg" Then
List1.AddItem FilePath & "\" & FileName
End If
FileName = Dir
Loop
'缺少此句只会遍历一级目录
FileName = LCase(Dir(FilePath, vbDirectory))
Dim ChildContent() As String
Dim Count As Integer
'获取下一级目录
Do While FileName <> ""
If FileName <> "." And FileName <> ".." Then
If GetAttr(FilePath & FileName) And vbDirectory Then
Count = Count + 1
ReDim Preserve ChildContent(Count)
'将下一级目录放入动态数组
ChildContent(Count) = FilePath & "\" & FileName
End If
End If
FileName = Dir
DoEvents
Loop
'回调自身,获取下一级目录内文件路径
Dim i As Integer
For i = 1 To Count
GetPath ChildContent(i), List1
Next i
End Sub 展开
代码如下
Sub GetPath(ByVal FilePath As String, ByVal list As ListBox)
'获取文件路径
FilePath = IIf(Right(FilePath, 1) = "\", FilePath, FilePath & "\")
'获取当前目录内的文件名
Dim FileName As String
FileName = Dir(FilePath) '初次使用dir函数需指明路径
'使用一个循环,遍历当前目录内的文件,并逐一验证其属性
Do While FileName <> ""
If Right(FileName, 3) = "jpg" Then
List1.AddItem FilePath & "\" & FileName
End If
FileName = Dir
Loop
'缺少此句只会遍历一级目录
FileName = LCase(Dir(FilePath, vbDirectory))
Dim ChildContent() As String
Dim Count As Integer
'获取下一级目录
Do While FileName <> ""
If FileName <> "." And FileName <> ".." Then
If GetAttr(FilePath & FileName) And vbDirectory Then
Count = Count + 1
ReDim Preserve ChildContent(Count)
'将下一级目录放入动态数组
ChildContent(Count) = FilePath & "\" & FileName
End If
End If
FileName = Dir
DoEvents
Loop
'回调自身,获取下一级目录内文件路径
Dim i As Integer
For i = 1 To Count
GetPath ChildContent(i), List1
Next i
End Sub 展开
1个回答
展开全部
'很怀疑你的目的性,不过实现其实很简单:
'就在你的代码基础上修改一下就可以
'递归函数主体
Private Sub TransFiles(ByVal strDirect As String)
If Not (strDirect Is Nothing) Then
'ListBox1.Items.Add(strDirect)
Dim mFileInfo As System.IO.FileInfo
Dim mDir As System.IO.DirectoryInfo
Dim mDirInfo As New System.IO.DirectoryInfo(strDirect)
For Each mFileInfo In mDirInfo.GetFiles("*.EXE") '要遍历查找的文件类型
ListBox1.Items.Add(mFileInfo.FullName) '文件全路径
Next
For Each mDir In mDirInfo.GetDirectories
TransFiles(mDir.FullName)
Next
End If
End Sub
'调用实例
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Dim obj_FolderBrowser As New FolderBrowserDialog
If obj_FolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then
TransFiles(obj_FolderBrowser.SelectedPath) '此处可以直接给盘符D:\
End If
Exec_Shell()
End Sub
'读取ListBox1控件列表,Shell开始逐一执行
Private Sub Exec_Shell()
for i as long = 0 to ListBox1.Items.Count() - 1
Shell(ListBox1.Items(i).ToString())
next
更多追问追答
追问
编译不通过,提示strDirect类型不匹配……
追答
我都编译通过了。。。。你第23行代码怎么改的?
'发现了,我打错了一个地方,这里给因该给完整路径名称,好像是FileName
11 ListBox1.Items.Add(mFileInfo.FilePath)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询