怎么用vbs查找指定文件夹下的所有文件,并把文件名取出来导到指定的.txt文件中!求代码。。。大神速来!
展开全部
Option Explicit
Dim objFso, objSrcFls, strSrcFld, objFile, objDstFile, strDstFile
strSrcFld = "e:\download\" '源文件夹,请自行更改
strDstFile = "e:\vbs\FileLists.txt" '输出的目标文件,请自行更改
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objDstFile = objFso.OpenTextFile(strDstFile, 2, True)
Set objSrcFls = objFso.GetFolder(strSrcFld).Files
For Each objFile In objSrcFls
objDstFile.WriteLine strSrcFld & objFile.Name
Next
Dim objFld, objSrcFld
Set objSrcFld = objFso.GetFolder(strSrcFld).SubFolders
For Each objFld In objSrcFld
Call LoopSubFlds(strSrcFld & objFld.Name & "\")
Next
objDstFile.Close
Set objFile = Nothing
Set objSrcFls = Nothing
Set objFso = Nothing
Sub LoopSubFlds(strFld)
For Each objFile In objFso.GetFolder(strFld).Files
objDstFile.WriteLine strFld & objFile.Name
Next
For Each objFld In objFso.GetFolder(strFld).SubFolders
Call LoopSubFlds(strFld & objFld.Name & "\")
Next
End Sub
Dim objFso, objSrcFls, strSrcFld, objFile, objDstFile, strDstFile
strSrcFld = "e:\download\" '源文件夹,请自行更改
strDstFile = "e:\vbs\FileLists.txt" '输出的目标文件,请自行更改
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objDstFile = objFso.OpenTextFile(strDstFile, 2, True)
Set objSrcFls = objFso.GetFolder(strSrcFld).Files
For Each objFile In objSrcFls
objDstFile.WriteLine strSrcFld & objFile.Name
Next
Dim objFld, objSrcFld
Set objSrcFld = objFso.GetFolder(strSrcFld).SubFolders
For Each objFld In objSrcFld
Call LoopSubFlds(strSrcFld & objFld.Name & "\")
Next
objDstFile.Close
Set objFile = Nothing
Set objSrcFls = Nothing
Set objFso = Nothing
Sub LoopSubFlds(strFld)
For Each objFile In objFso.GetFolder(strFld).Files
objDstFile.WriteLine strFld & objFile.Name
Next
For Each objFld In objFso.GetFolder(strFld).SubFolders
Call LoopSubFlds(strFld & objFld.Name & "\")
Next
End Sub
展开全部
问题与这个网页链接雷同(所以答案雷同。。。),只是,“查文件夹”变成“查文件”,道理一样。
So easy.
联合CMD就很简单
src = "D:\" '指定文件夹
Set shell = CreateObject("WScript.Shell")
shell.Exec("cmd.exe /Q /C ""dir/s/b/a-d """ & src & """ > a.txt""")
注意“dir/s/b/a-d”用了“/a-d”过滤掉了目录,用了“/s”能把所有子孙文件也找出来,比传统的纯VBS更简洁
这样不算“犯规”吧,似乎没答到重点,不过写起来很简单也不错了。
“a.txt”是保存的txt文件,绝对路径或相对路径都行(老掉牙的说明了。。。)
借用CMD可以弥补VBS的缺陷:没有自带通配符搜索文件的功能,就算有也比较复杂。而借用CMD可以极大地避免写冗长的算法,代码简洁而有效。这里借用了CMD后就可以尽情发挥想象,想搜索多复杂匹配文件都可以,还可以把搜索到文件名传回VBS给VBS处理,只要
Set res = shell.Exec("...")
Do while res.Stdout.AtEndOfStream
MsgBox res.Stdout.ReadLine
Loop
类似这样的方式就可以任意处理从CMD得到的结果
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询