2009-04-21 94 views
14

我救了这个VBScript脚本,以我的本地机器为c:\ test.vbs:为什么这个VBScript会给我一个错误?

WScript.StdOut.WriteLine "This is a test" 

当我在命令行中运行它,我得到这个错误:

--------------------------- 
Windows Script Host 
--------------------------- 
Script: C:\test.vbs 
Line: 1 
Char: 1 
Error: The handle is invalid. 
Code: 80070006 
Source:  (null) 

--------------------------- 
OK 
--------------------------- 

我得到这个在Windows Vista(SP1)和Windows XP Pro(SP3)下。

回答

6

正如文章中公认的答案所描述的,我的剧本工作时,我从这样的命令提示符下称它:

cscript test.vbs 

您还可以更改默认脚本宿主,以便调用CSCRIPT没有必要每一次。完成之后,原始命令可以不加修改地工作。

cscript //h:cscript //s 

您可以恢复原来的行为:

cscript //h:wscript //s 

谢谢!

1

我在bug“cscript - print output on same line on console?”中提交了这个解决方案,我觉得这个问题与这个问题有关。

我在我的JavaScript中使用以下“日志”功能来支持wscript或cscript环境。正如你所看到的,只有在可以的情况下,这个函数才会写入标准输出。

var ExampleApp = { 
    // Log output to console if available. 
    //  NOTE: Script file has to be executed using "cscript.exe" for this to work. 
    log: function (text) { 
     try { 
      // Test if stdout is working. 
      WScript.stdout.WriteLine(text); 
      // stdout is working, reset this function to always output to stdout. 
      this.log = function (text) { WScript.stdout.WriteLine(text); }; 
     } catch (er) { 
      // stdout is not working, reset this function to do nothing. 
      this.log = function() { }; 
     } 
    }, 
    Main: function() { 
     this.log("Hello world."); 
     this.log("Life is good."); 
    } 
}; 

ExampleApp.Main(); 
+0

根据这个http://stackoverflow.com/questions/4999364/try-catch-end-try-in-vbscript vbscript没有尝试抓住。你能够设置一个变量,取决于它是哪种情况下,在VBScript? – barlop 2013-10-26 23:32:40

相关问题