一步一步学VBScript(6)之WSH对象五

前沿:

  终于回到上海了。不管怎么样。我的这个博客还是会坚持的。

在工作上能给您带来帮助就是我继续写作最大的动力。

如果您有什么关于脚本的问题。可以联系我。我也非常乐意

与您一起探讨相关的问题。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

继续上篇话题

上篇要点:

1.介绍脚本的参数引入

 

本篇主要内容

1.介绍关于如何控制脚本的运行(sleep,quit,time-out)

2.介绍如何输出脚本环境的信息

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

一 介绍关于如何控制脚本的运行(sleep,quit,time-out)

wscript.sleep

wscript.quit

wscript.timeout

 


  1. Wscript.Timeout = 3 
  2. Wscript.Sleep 5000  
  3. Wscript.Echo "Hello World." 

呵呵,结果会是什么呢

 

一步一步学VBScript(6)之WSH对象五

不是明明有helloword的吗。为什么会不出现呢。

原因很简单。超时了。

这里用了wscript.timeout 3

注意这个参数单位为秒

wscript.sleep 单位则为毫秒。

 

这段脚本运作过程是

设定脚本运行3秒,否则就为超时

下一步则让脚本挂起5秒

下一步则是显示hello world

脚本肯定超时了麻,当然hello world就不显示了。

 

再看段代码

 


  1. Wscript.Timeout = 3 
  2. Wscript.Sleep 2000  
  3. Wscript.Echo "Hello World." 

同样是超时设定为3秒钟

脚本挂起2秒,然后显示hello world

让我们来看看结果

 

一步一步学VBScript(6)之WSH对象五

那么wscript.timeout有什么用呢。

他能帮助您控制您脚本运行的时间。比如您的一个处理必须在60秒钟结束。那么它绝对是最好的帮手。

 

 

最后还有个wscript.quit。

来看段代码

 


  1. Dim a  
  2. a = 1 
  3. If a = 0 Then  
  4. WScript.Echo "Hello world"  
  5. Else  
  6. WScript.Quit  
  7. End If  
  8. WScript.Echo "Nice to meet you" 

那个代码会有什么结果呢。您估计很快就明白了。

wscript.quit会有什么用呢。他能让您选择性的结束脚本。

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

二.输出脚本环境的信息

同样先看一段脚本

 


  1. Wscript.Echo "Script Full Name: " & Wscript.ScriptFullName 
  2. Wscript.Echo "Script Name: " & Wscript.ScriptName 
  3. Wscript.Echo "Version: " & WScript.Version 
  4. Wscript.Echo "Build: " & Wscript.BuildVersion 
  5. Wscript.Echo "Name: " & Wscript.Name 
  6. Wscript.Echo "Full Name: " & Wscript.FullName 
  7. Wscript.Echo "Path: " & Wscript.Path 

结果会是什么呢


一步一步学VBScript(6)之WSH对象五

相信大家都看明白了

Wscript.ScriptFullName,当前脚本的运行全路径

Wscript.ScriptName,脚本的名称
WScript.Version,脚本宿主的版本(脚本宿主的概念请参考第一篇)
Wscript.BuildVersion,脚本宿主的build号,所以完整的版本为5.8.16385
Wscript.Name,这个名称总是为windows script host
Wscript.FullName,脚本宿主的全路径当然就是wscript.exe或cscript.exe的路径
Wscript.Path,脚本宿主的所在文件夹当然就是wscript.exe或cscript.exe

 

那么脚本宿主环境有什么用呢。

第一它能提供给您一个脚本运行环境的判断。如下代码

它能控制您脚本运行的最基本的前提条件。


  1. If Wscript.Version <> "5.6" Then 
  2.     Wscript.Echo "This script must be run under WSH 5.6." 
  3.     Wscript.Quit 
  4. End If 

第二脚本运行的脚本宿主,如下代码


  1. If UCase(Right(Wscript.FullName, 11)) = "WSCRIPT.EXE" Then 
  2.     Wscript.Echo "This script must be run under CScript." 
  3.     Wscript.Quit 
  4. End If 

第三,我觉得最有用的就是提供当前脚本的路径,帮助您保存输出的文件。

如下代码

 


  1. Dim strScriptPath 
  2. Dim arrayScriptPath 
  3.  
  4. strScriptPath = WScript.ScriptFullName 
  5. arrayScriptPath = pathtoname(strScriptPath) 
  6. WScript.Echo arrayScriptPath(0) 
  7. WScript.Echo arrayScriptPath(1) 
  8. WScript.Echo arrayScriptPath(2) 
  9. WScript.Echo arrayScriptPath(3) 
  10. WScript.Echo arrayScriptPath(4) 
  11. WScript.Echo arrayScriptPath(5) 
  12.  
  13.  
  14.  
  15. '******************************************************************** 
  16. '* 
  17. '* function pathtoname(strImputpath) 
  18. '* Purpose: imput the path and give the name,fatherfoldername etc. 
  19. '* Input:   strImputpath 
  20. '* Output:  array 
  21. '* Notes:   array(0) 'ex: "D:\abc\efg.txt" 
  22. '*          array(1) 'ex: "D:\abc\efg" 
  23. '*          array(2) 'ex: "D:\abc" 
  24. '*          array(3) 'ex: "abc" 
  25. '*          array(4) 'ex: "efg" 
  26. '*          array(5) 'ex: "txt" 
  27. '* 
  28. '******************************************************************** 
  29.  
  30. function pathtoname(strImputpath) 
  31. Dim strFilePath,strSubname,strNoSubpath,strFilesortname,strfolderpath,strFatherfoldername 
  32. Dim temppathArray() 
  33.  
  34. If InStrRev(strImputpath,".") = 0 Then 
  35.     If InStrRev(strImputpath,"\") = 0 Then  
  36.         ReDim temppathArray(0) 
  37.         temppathArray(0) = Trim(strImputpath) 
  38.     Else 
  39.         ReDim temppathArray(1) 
  40.         temppathArray(0) = Trim(strImputPath) 
  41.         temppathArray(1) = Mid(Trim(strImputPath),InstrRev(Trim(strImputPath),"\")+1) 
  42.     End If 
  43. Else 
  44. ReDim temppathArray(5) 
  45. strFilePath = Trim(strImputpath) 
  46. temppathArray(0) = strFilePath 
  47. 'ex: "D:\abc\efg.txt" 
  48. 'MsgBox(strFilePath) 
  49.  
  50. strSubname = Mid(strFilePath,InstrRev(strFilePath,".")+1) 
  51. temppathArray(5) = strSubname 
  52. 'ex: "txt" 
  53. 'MsgBox(strSubname) 
  54.  
  55. strNoSubpath = Mid(strFilePath,1,InStrRev(strFilePath,".")-1) 
  56. temppathArray(1) = strNoSubpath 
  57. 'ex: "D:\abc\efg" 
  58. 'MsgBox(strNoSubpath) 
  59.  
  60. strFilesortname = Mid(strNoSubpath,InstrRev(strFilePath,"\")+1) 
  61. temppatharray(4) = strFilesortname 
  62. 'ex: "efg" 
  63. 'MsgBox(strFilesortname) 
  64.  
  65. strfolderpath = Mid(strNoSubpath,1,InStrRev(strNoSubpath,"\")-1) 
  66. temppathArray(2) = strfolderpath 
  67. 'ex: "D:\abc" 
  68. 'MsgBox(strfolderpath) 
  69.  
  70. strFatherfoldername = Mid(strfolderpath,InstrRev(strfolderpath,"\")+1) 
  71. temppatharray(3) = strFatherfoldername 
  72. 'ex: "abc" 
  73. 'MsgBox(strFatherfoldername) 
  74. End If 
  75.  
  76. pathtoname = temppatharray 
  77. End function

这样,您就能获取您脚本的相关的路径了。然后您也能在您脚本运行的文件夹中创建相关的文件夹等了。

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

总结

1.介绍关于如何控制脚本的运行(sleep,quit,time-out)

2.介绍如何输出脚本环境的信息

 

下期中将会

1.介绍WshShell中的相关对象

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

期待您的观看。您的宝贵的意见与建议将是我继续的前行的动力。

如果您有什么意见与建议的话,请务必先联系我。

谢谢您耐心的观看。谢谢