Board logo

标题: [问题求助] [已解决]VBS脚本循环搜索X盘目录的某文件为什么不会自动终止? [打印本页]

作者: samsinn    时间: 2012-1-14 14:42     标题: [已解决]VBS脚本循环搜索X盘目录的某文件为什么不会自动终止?

这段脚本是循环搜索E盘第一层目录的一个文件,直到文件找到后删除而终止,但是它执行后为什么不会自动终止呢?我知道在sub f()中加入WSCRIPT.QUIT可以退出脚本,但我不想退出脚本,而是继续执行脚本其他命令,请问要如何修改呢?
  1. set ws=createobject("wscript.shell")
  2. set fso=createobject("scripting.filesystemobject")
  3. wc="aa.exe"
  4. pth="e:\"
  5. while true
  6. d pth
  7. wscript.sleep 1000
  8. wend
  9. sub d(x)
  10. for each i in fso.getfolder(x).subfolders
  11. f i
  12. next
  13. end sub
  14. sub f(x)
  15. for each c in fso.getfolder(x).files
  16. if instr(1,c,wc,1)>0 then
  17. fso.deleteFile c
  18. msgbox "ok"
  19. end if
  20. next
  21. end sub
复制代码

作者: broly    时间: 2012-1-14 15:31

加一句exit sub
作者: samsinn    时间: 2012-1-14 15:45

回复 2# broly

谢谢版主的回复,请问是这样添加吗?
  1. sub f(x)
  2. for each c in fso.getfolder(x).files
  3. if instr(1,c,wc,1)>0 then
  4. fso.deleteFile c
  5. msgbox "ok"
  6. exit sub
  7. end if
  8. next
  9. end sub
复制代码
但我测试之后,脚本依旧在执行"d pth"命令,还是没有终止
作者: broly    时间: 2012-1-14 16:03

你那个是无限循环来的
  1. while true
  2. d pth
  3. wscript.sleep 1000
  4. wend
复制代码
这里要改。
  1. set ws=createobject("wscript.shell")
  2. set fso=createobject("scripting.filesystemobject")
  3. wc="aa.exe"
  4. pth="e:\"
  5. d pth
  6. wscript.sleep 1000
  7. sub d(x)
  8. for each i in fso.getfolder(x).subfolders
  9. f i
  10. next
  11. end sub
  12. sub f(x)
  13. for each c in fso.getfolder(x).files
  14. if instr(1,c,wc,1)>0 then
  15. fso.deleteFile c
  16. msgbox "ok"
  17. end if
  18. next
  19. end sub
复制代码

作者: samsinn    时间: 2012-1-14 16:18

本帖最后由 samsinn 于 2012-1-15 11:20 编辑

其实我的意思是想弄个监视文件创建后删除并继续其他命令的脚本,我就是不知道要如何改才能不死循环
  1. Set Fso = CreateObject("scripting.filesystemobject")
  2. Set Folder = Fso.GetFolder("e:\")
  3. set SubFolders = Folder.SubFolders
  4. do
  5. For Each SubFolder In SubFolders
  6. xf = SubFolder&"\xxx.txt"
  7. If Fso.FileExists(xf) then
  8. Fso.DeleteFile xf, True
  9. exit do
  10. End If
  11. Next
  12. wscript.sleep 100
  13. loop
复制代码
自己找到答案:不使用SUB执行递归的方式,在条件符合后利用exit do跳过即可。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2