[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[已解决]VBS的if语句怎样判断空变量?

这是我写的一个代码,它的用处是计算出当前目前内的拥有hide 和system属性的文件并显示出来,从我的测试来看两个计算代码是正常的,但最后面两个if判断句却出现错误,在当前目录中无论存不存在system和hide属性文件或文件夹都不会运行msgbox函数,我自己有一个解决办法,但不是很好,希望有坛友给出问题的根源。谢谢。
  1. dim ws,fs,current_path_files,current_path_subfolders,att,files_sum,subfolders_sum
  2. 'on error resume next
  3. set ws=wscript.createobject("wscript.shell")
  4. set fs=wscript.createobject("scripting.filesystemobject")
  5. set current_path_files=fs.getfolder(ws.CurrentDirectory).files
  6. set current_path_subfolders=fs.getfolder(ws.CurrentDirectory).subfolders
  7. '==========================================================================
  8. for each file in current_path_files
  9.   'ignore the shortcut and link
  10.   att=file.attributes
  11.   if att=4+2 or att=4+2+1 or att=4+2+32 or att=4+2+32+1 then
  12.     files_sum=files_sum & file.name & vbnewline
  13.   end if
  14. next
  15. for each subfolder in current_path_subfolders
  16.   'ignore the shortcut and link
  17.   att=subfolder.attributes
  18.   if att=4+2 or att=4+2+16 or att=4+2+16+1 then
  19.     subfolders_sum=subfolders_sum & subfolder.name & vbnewline
  20.   end if
  21. next
  22. '==========================================================================
  23. if files_sum <> "" then
  24.   msgbox files_sum,,"Exist Hide and System file(s):"
  25. end if
  26. if folders_sum <> "" then
  27.   msgbox subfolders_sum,,"Exist Hide and System folder(s):"
  28. end if
复制代码

[ 本帖最后由 s700800900 于 2010-6-20 23:48 编辑 ]
1

评分人数

    • Batcher: 感谢主动给标题标注[已解决]字样PB + 2

使用挺正常的

  1. dim ws,fs,current_path_files,current_path_subfolders,att,files_sum,subfolders_sum
  2. 'on error resume next
  3. set ws=wscript.createobject("wscript.shell")
  4. set fs=wscript.createobject("scripting.filesystemobject")
  5. set current_path_files=fs.getfolder(ws.CurrentDirectory).files
  6. set current_path_subfolders=fs.getfolder(ws.CurrentDirectory).subfolders
  7. '==========================================================================
  8. for each file in current_path_files
  9.   'ignore the shortcut and link
  10.   att=file.attributes
  11.   if att=4+2 or att=4+2+1 or att=4+2+32 or att=4+2+32+1 then
  12.     files_sum=files_sum & file.name & vbnewline
  13.   end if
  14. next
  15. for each subfolder in current_path_subfolders
  16.   'ignore the shortcut and link
  17.   att=subfolder.attributes
  18.   if att=4+2 or att=4+2+16 or att=4+2+16+1 then
  19.     subfolders_sum=subfolders_sum & subfolder.name & vbnewline
  20.   end if
  21. next
  22. '==========================================================================
  23. if files_sum <> "" then
  24.   msgbox files_sum,,"Exist Hide and System file(s):"
  25. end if
  26. if subfolders_sum <> "" then
  27.   msgbox subfolders_sum,,"Exist Hide and System folder(s):"
  28. end if
复制代码
http://www.2164.top/

TOP

虽然各种手册都说了最好在代码中加上Option Explicit 语句,但由于vbs一般比较短小,很难出现变量名写错的情况,且为了简洁,一般不会用它。今天终于出了一个这样的错误(太难得了,几乎没碰到过)
最后一个if语句中,folders_sum -> subfolders_sum

另外,属性比较最好用位运算:if (att AND 6) <> 0 then
命令行参考:hh.exe ntcmds.chm::/ntcmds.htm
求助者请拿出诚心,别人才愿意奉献热心!
把查看手册形成条件反射!

TOP

啊。。。哭 了都,原来是变量写错了,,,想了几天这个问题白想了。   T_T。

谢谢啊。。。终于明白了,在使用之前要定义变量是多么的重要。

TOP

返回列表