标题: [问题求助] 为什么vbs脚本无法遍历到所有的文件?转成exe后用管理员身份运行也不行。 [打印本页]
作者: 悬崖之树 时间: 2021-1-23 19:02 标题: 为什么vbs脚本无法遍历到所有的文件?转成exe后用管理员身份运行也不行。
本帖最后由 悬崖之树 于 2021-1-23 19:22 编辑
- On Error Resume Next
- Dim FileName,fs,foldername, iCount
- foldername = InputBox("请输入要遍历的文件夹")
- If foldername = "" Then
- wscript.quit
- End If
- Set fs = CreateObject("scripting.filesystemobject")
- digui(foldername)'调用递归函数进行遍历
- MsgBox FileName
-
- '下面是递归查找函数
- Function digui(path)
- Set folder = fs.getfolder(path)
- Set subfolders = folder.subfolders
- Set Files = folder.Files
- For Each i In Files
- FileName=FileName & i.path & vbNewLine '找到则追加到变量FileName中
- iCount = iCount + 1
- If iCount = 10 Then
- MsgBox FileName
- FileName = ""
- iCount = 0
- End If
- Next
- For Each j In subfolders
- digui (j.path) '递归查找子目录
- Next
- End Function
复制代码
估计是脚本在运行时出现了"没有文件或文件夹权限",然后就自动停止后面的递归操作了。能否先判断一下文件或者文件夹权限后,再决定是否对该文件或文件夹进行操作?
作者: WHY 时间: 2021-1-27 10:40
- Dim ws
- Set ws = CreateObject("WScript.Shell")
-
- If WSH.Arguments.Count = 0 Then
- ws.Run "cscript " & chr(34) & WSH.ScriptFullName & chr(34) & " ARG", 0
- WScript.Quit
- End If
-
- Dim folderPath, iCount, str, objExec
- folderPath = InputBox("输入要遍历的文件夹路径")
- If isEmpty(folderPath) Then WScript.Quit
-
- iCount = 0
- str = ""
- Set objExec = ws.Exec( "cmd /c dir /b /a-d /s " & chr(34) & folderPath & chr(34) )
-
- Do Until objExec.StdOut.AtEndOfStream
- iCount = iCount + 1
- str = str & objExec.StdOut.ReadLine & vbLf
- If iCount Mod 10 = 0 Then
- MsgBox str
- str = ""
- End If
- Loop
- If str <> "" Then MsgBox str
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |