google搜索 51Testing站内搜索                    软件测试门户 | 软件测试培 训 | 文章资料精选 | 软件测试论坛 | 软件测试博客 | 测试招聘求职 
打印

请教各位哥哥姐姐,关于WScript这个的问题

请教各位哥哥姐姐,关于WScript这个的问题


Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec    = WshShell.Exec("%comspec% /c dir")

Function ReadAllFromAny(oExec)

     If Not oExec.StdOut.AtEndOfStream Then
          ReadAllFromAny = oExec.StdOut.ReadAll
          Exit Function
     End If

     If Not oExec.StdErr.AtEndOfStream Then
          ReadAllFromAny = oExec.StdErr.ReadAll
          Exit Function
     End If
     
     ReadAllFromAny = -1
End Function

Dim allInput, tryCount

allInput = ""
tryCount = 0

Do While True

     Dim input
     input = ReadAllFromAny(oExec)

     If -1 = input Then
          If tryCount > 10 And oExec.Status = 1 Then
               Exit Do
          End If
          tryCount = tryCount + 1
          WScript.Sleep 100
     Else
          allInput = allInput & input
          tryCount = 0
     End If
Loop

If oExec.ExitCode <> 0 Then
     msgbox "Warning: Non-zero exit code"
End If

msgbox  allInput

运行到“ WScript.Sleep 100 ”这句话时,系统提示“ 缺少对象: 'WScript' ”,请问各位帮小弟看一下,问题错在哪里,小弟先谢谢了。

TOP

把这两句放进循环里面
Set WshShell = CreateObject("WScript.Shell")
Set oExec    = WshShell.Exec("%comspec% /c dir")

如:
Do While True
Set WshShell = CreateObject("WScript.Shell")
Set oExec    = WshShell.Exec("%comspec% /c dir")


     Dim input
     input = ReadAllFromAny(oExec)

     If -1 = input Then
          If tryCount > 10 And oExec.Status = 1 Then
               Exit Do
          End If
          tryCount = tryCount + 1
          WScript.Sleep 100
     Else
          allInput = allInput & input
          tryCount = 0
     End If
Loop

你这程序有什么意义?

TOP

引用:
原帖由 xiaonan 于 2006-8-2 17:06 发表
把这两句放进循环里面
Set WshShell = CreateObject("WScript.Shell")
Set oExec    = WshShell.Exec("%comspec% /c dir")

如:
Do While True
Set WshShell = CreateObject("WSc ...
把那两句放进循环里面,脚本是可以执行,但没有执行到
WScript.Sleep 100  ,如果执行到了还是会提示“ 缺少对象: 'WScript' 

把楼主的脚本放到DOS下运行是没有问题的,但放到QTP里面运行就提示有问题,
我觉得有可能是语言的问题.老是提示没有定义,但定义了也是提示不行,执行QTP HELP里面的脚本也是这样.

你可以这样做,既然DOS可以执行,那你就用QTP执行DOS调用脚本,这样是没有问题的.
其它的办法还没有想到!

[ 本帖最后由 QA_BAY 于 2006-8-2 18:03 编辑 ]

TOP

请问QA_BAY版主我该怎样写那个 (QTP执行DOS调用脚本 ) ,我刚学QTP没有多久啊。请版主稍微指导一下,小弟先谢谢了。

TOP

你可以这样做
先把脚本保存起来(保证你的脚本能正确运行),例如命名为test.vbs
然后在QTP写以下脚本就可以正确运行了!
Dim oShell
Set oShell =CreateObject ("WSCript.shell")
oShell.run "cmd /K wscript e:\test.vbs"
Set oShell = Nothing

TOP

谢谢,  QA_BAY版主,我的问题解决了。
非常感谢版主对小弟的指导

TOP

WScript.Sleep 100改成wait(0,100)可以运行的
Only social practise can be the criterion of truth.

TOP

能问下("%comspec% /c dir")是啥意思吗?它打开哪个哇?
Only social practise can be the criterion of truth.

TOP

up

TOP

o  o

TOP

ding

TOP

 
当前时区 GMT+8, 现在时间是 2008-8-30 12:06Copyright(C)上海博为峰软件技术有限公司 2001-2007 电话:021-64471599-8017
当您在访问网站、论坛及博客过程中遇到问题时可发送email:webmaster@51testing.com或发送论坛短信至管理员风在吹