您的位置:首页 > 编程语言 > VB

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

2011-03-22 11:22 375 查看
一步一步学VBScript(6)之WSH对象五

前沿:

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

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

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

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

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

继续上篇话题

上篇要点:

1.介绍脚本的参数引入

本篇主要内容

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

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

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

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

wscript.sleep

wscript.quit

wscript.timeout

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

呵呵,结果会是什么呢





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

原因很简单。超时了。

这里用了wscript.timeout 3

注意这个参数单位为秒

wscript.sleep 单位则为毫秒。

这段脚本运作过程是

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

下一步则让脚本挂起5秒

下一步则是显示hello world

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

再看段代码

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

同样是超时设定为3秒钟

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

让我们来看看结果





那么wscript.timeout有什么用呢。

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

最后还有个wscript.quit。

来看段代码

Dim a
a = 1
If a = 0 Then
WScript.Echo "Hello world"
Else
WScript.Quit
End If
WScript.Echo "Nice to meet you"

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

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

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

二.输出脚本环境的信息

同样先看一段脚本

Wscript.Echo "Script Full Name: " & Wscript.ScriptFullName
Wscript.Echo "Script Name: " & Wscript.ScriptName
Wscript.Echo "Version: " & WScript.Version
Wscript.Echo "Build: " & Wscript.BuildVersion
Wscript.Echo "Name: " & Wscript.Name
Wscript.Echo "Full Name: " & Wscript.FullName
Wscript.Echo "Path: " & Wscript.Path

结果会是什么呢





相信大家都看明白了

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

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

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

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

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

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

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

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

如下代码

Dim strScriptPath
Dim arrayScriptPath

strScriptPath = WScript.ScriptFullName
arrayScriptPath = pathtoname(strScriptPath)
WScript.Echo arrayScriptPath(0)
WScript.Echo arrayScriptPath(1)
WScript.Echo arrayScriptPath(2)
WScript.Echo arrayScriptPath(3)
WScript.Echo arrayScriptPath(4)
WScript.Echo arrayScriptPath(5)

'********************************************************************
'*
'* function pathtoname(strImputpath)
'* Purpose: imput the path and give the name,fatherfoldername etc.
'* Input:   strImputpath
'* Output:  array
'* Notes:   array(0) 'ex: "D:\abc\efg.txt"
'*          array(1) 'ex: "D:\abc\efg"
'*          array(2) 'ex: "D:\abc"
'*          array(3) 'ex: "abc"
'*          array(4) 'ex: "efg"
'*          array(5) 'ex: "txt"
'*
'********************************************************************

function pathtoname(strImputpath)
Dim strFilePath,strSubname,strNoSubpath,strFilesortname,strfolderpath,strFatherfoldername
Dim temppathArray()

If InStrRev(strImputpath,".") = 0 Then
If InStrRev(strImputpath,"\") = 0 Then
ReDim temppathArray(0)
temppathArray(0) = Trim(strImputpath)
Else
ReDim temppathArray(1)
temppathArray(0) = Trim(strImputPath)
temppathArray(1) = Mid(Trim(strImputPath),InstrRev(Trim(strImputPath),"\")+1)
End If
Else
ReDim temppathArray(5)
strFilePath = Trim(strImputpath)
temppathArray(0) = strFilePath
'ex: "D:\abc\efg.txt"
'MsgBox(strFilePath)

strSubname = Mid(strFilePath,InstrRev(strFilePath,".")+1)
temppathArray(5) = strSubname
'ex: "txt"
'MsgBox(strSubname)

strNoSubpath = Mid(strFilePath,1,InStrRev(strFilePath,".")-1)
temppathArray(1) = strNoSubpath
'ex: "D:\abc\efg"
'MsgBox(strNoSubpath)

strFilesortname = Mid(strNoSubpath,InstrRev(strFilePath,"\")+1)
temppatharray(4) = strFilesortname
'ex: "efg"
'MsgBox(strFilesortname)

strfolderpath = Mid(strNoSubpath,1,InStrRev(strNoSubpath,"\")-1)
temppathArray(2) = strfolderpath
'ex: "D:\abc"
'MsgBox(strfolderpath)

strFatherfoldername = Mid(strfolderpath,InstrRev(strfolderpath,"\")+1)
temppatharray(3) = strFatherfoldername
'ex: "abc"
'MsgBox(strFatherfoldername)
End If

pathtoname = temppatharray
End function

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

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

总结

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

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

下期中将会

1.介绍WshShell中的相关对象

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

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

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

谢谢您耐心的观看。谢谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息