wscript與cscript區(qū)別
窗口中運行,wscript;命令行中運行,cscrip;
cscript因為把輸出放進了控制臺 所以可以對它重定向 方便程序調(diào)用
wscript直接用窗口輸出 適合調(diào)試程序和編寫小工具 在windows下不用cmd的話cscript的輸出會一閃而過 大多數(shù)情況下,列在下表中的選項適用于 wscript.exe 和 cscript.exe。例外情況會加以注釋。
參數(shù)說明
//b
批處理模式;隱藏用戶提示和腳本錯誤在命令行中的顯示。默認模式是交互模式。
//d
打開調(diào)試程序。
//e:engine
用指定的腳本引擎執(zhí)行腳本。
//h:cscript或 //h:wscript 將 cscript.exe 或 wscript.exe 注冊為運行腳本的默認應用程序。如果未指定,則將 wscript.exe 假設為默認應用程序。
//i
默認。交互模式;允許顯示用戶提示和腳本錯誤。與批處理模式相反。
//job:<jobid>
從 .wsf 文件運行指定的 jobid。
//logo
默認。顯示標題。與 nologo 相反。
//nologo
防止在運行時顯示執(zhí)行標題。默認設置是 logo。
//s
保存該用戶的當前命令行選項。
//t:nn
啟用超時:腳本可以運行的最大秒數(shù)。默認設置是無限制。//t 參數(shù)通過設置定時器來防止腳本執(zhí)行過度。當執(zhí)行時間超過指定值時,cscript 用 iactivescript::interruptthread 方法中斷腳本引擎,并終止過程。
//u
用于 windows nt 和 windows 2000,強制命令行以 unicode 格式輸出。cscript 無法決定以 unicode 還是以 ansi 輸出;默認設置為 ansi。
//x
在調(diào)試程序中啟動該程序。
//?
微軟的解釋:
script hosts
the script host initiates and coordinates the running of your script; it reads your script file and interacts with components of the wsh environment and any com objects required by the script. it is also the responsibility of the script host to determine which language engine to use when running the script. for example, if the script has a .vbs extension, the script host will load the vbscript language engine and begin working with that engine to execute the code.
the wsh environment includes two script hosts: the console-based cscript and the gui-based wscript. the two script hosts provide nearly identical capabilities, and in most cases, it does not matter which of the script hosts you use to run your scripts.
the two exceptions lie in how you interact with a script; that is, how you get information into a script (input) and how the script displays information it has retrieved (output). in general, cscript receives input from the command prompt and displays output in a command window. wscript, by contrast, receives input through a graphical dialog box and displays output in a graphical message box.
otherwise, the two script hosts are largely identical: if you have a script that does not require user interaction, you can run that script under either cscript or wscript. for example, the following script maps a network drive. because it neither requires input nor displays output, it runs exactly the same under either script host:
set objnetwork = wscript.createobject(wscript.network)objnetwork.mapnetworkdrive g:, \\atl-fs-01\sales
on the other hand, the following script which displays a series of messages runs much differently under cscript (where the messages are displayed as individual lines within a command window) and wscript (where the messages are displayed as a series of message boxes). if you are interested in seeing the difference for yourself, copy the script into notepad, save it with a .vbs file extension, and then run it under both cscript and wscript. (for more information about running scripts under a script host, see running wsh scripts later in this chapter.)
代碼如下:
wscript.echo line 1.
wscript.echo line 2.
wscript.echo line 3.
wscript.echo line 4.