即使没有执行,VBScript文件也会自行删除

时间:2017-01-31 07:56:52

标签: vbscript

我有VBScript启动TestNG测试。在调用测试之前,它应该下载测试中的.jar文件。以下是代码的主要部分:

'Variables - input args
Dim objArgs
Set objArgs = wscript.Arguments
finalHour = CInt(objArgs(1))

Set oShell = WScript.CreateObject("WSCript.shell")
Set UAC = CreateObject("Shell.Application")

'save script stating date
startDay = Day(Now)
startHour = Hour(Now)

downloadLink = "http://....ru:.../.../"
savePath = "C:\monitoring\build-under-test\"

WScript.Echo "Download jar files from '" & downloadLink & "...'"

'DownloadFile downloadLink   & "some.jar",    savePath & "some.jar"
Dim xHttp: Set xHttp = CreateObject("Microsoft.XMLHTTP")
Dim bStrm: Set bStrm = CreateObject("ADODB.Stream")
xHttp.Open "GET", downloadLink & "account.jar", False
xHttp.Send

With bStrm
    .Type = 1 '//binary
    .Open
    .Write xHttp.ResponseBody
    .SaveToFile savePath & "account.jar", 1 '//overwrite
    .Close
End With
xHttp = Nothing
bStrm = Nothing

Do
    WScript.Echo "Executing: run-suite.bat " & objArgs(0) & "        - at " & Now
    WScript.Echo "    finalHour = " & finalHour
    'With waiting of last command execution
    oShell.Run "run-suite.bat " & objArgs(0) & " " & objArgs(1) & " false true > C:\monitoring\results\logfiles\" & logBatName, 1, True

    currentHour = Hour(Now)
    WScript.Echo "    checking: currentHour < finalHour :: " & currentHour & " < " & finalHour
Loop While currentHour < finalHour

问题是当我执行这个文件时:

cscript.exe "run-monitoring.vbs" "test-suite\monitoring-all-tests.xml" 21

此文件已自行删除。然后我重新创建文件后,它再次删除自己没有运行命令。所以我开始重新创建它并注释行以找到强制删除文件的行。那是:

.SaveToFile savePath & "account.jar", 1 '//overwrite

当我删除评论时,文件会自行删除。即使在PC重启后也是如此。

为什么会这样?为什么即使我不运行文件也会重新执行文件?

1 个答案:

答案 0 :(得分:0)

有两行代码可以使用相同的资源(.jar文件)

.SaveToFile savePath & "account.jar", 1 '//overwrite

和(使用此lib的java测试):

oShell.Run "run-suite.bat " & objArgs(0) & " " & objArgs(1) & " false true > C:\monitoring\results\logfiles\" & logBatName, 1, True

评论这些行中的任何一行都会修复自我删除。因此我使用的解决方案非常简单:将.jar文件保存操作移动到另一个vbscript文件download-jar.vbs并在脚本中调用它:

oShell.run "cscript.exe download-jar.vbs " & downloadJarsLink, 1, True

也许还有另一种方式(保存后释放资源),但我需要快速稳定的解决方案,就在这里。