Wscript 和 Cscript 运行脚本的区别详解
WSCRIPT与CScript区别
窗口中运行,WSCRIPT;命令行中运行,CScrip;
cscript因为把输出放进了控制台所以可以对它重定向方便程序调用
wscript直接用窗口输出适合调试程序和编写小工具在windows下不用cmd的话cscript的输出会一闪而过大多数情况下,列在下表中的选项适用于WScript.exe和CScript.exe。例外情况会加以注释。
参数 说明
//B 批处理模式;隐藏用户提示和脚本错误在命令行中的显示。默认模式是交互模式。
//D 打开调试程序。
//E:engine 用指定的脚本引擎执行脚本。
//H:CScript或//H:Wscript将CScript.exe或WScript.exe注册为运行脚本的默认应用程序。如果未指定,则将WScript.exe假设为默认应用程序。
//I 默认。交互模式;允许显示用户提示和脚本错误。与批处理模式相反。
//Job:<JobID> 从.wsf文件运行指定的JobID。
//logo 默认。显示标题。与nologo相反。
//nologo 防止在运行时显示执行标题。默认设置是logo。
//S 保存该用户的当前命令行选项。
//T:nn 启用超时:脚本可以运行的最大秒数。默认设置是无限制。//T参数通过设置定时器来防止脚本执行过度。当执行时间超过指定值时,CScript用IActiveScript::InterruptThread方法中断脚本引擎,并终止过程。
//U 用于WindowsNT和Windows2000,强制命令行以Unicode格式输出。CScript无法决定以Unicode还是以ANSI输出;默认设置为ANSI。
//X 在调试程序中启动该程序。
//?
ScriptHosts
Thescripthostinitiatesandcoordinatestherunningofyourscript;itreadsyourscriptfileandinteractswithcomponentsoftheWSHenvironmentandanyCOMobjectsrequiredbythescript.Itisalsotheresponsibilityofthescripthosttodeterminewhichlanguageenginetousewhenrunningthescript.Forexample,ifthescripthasa.vbsextension,thescripthostwillloadtheVBScriptlanguageengineandbeginworkingwiththatenginetoexecutethecode.
TheWSHenvironmentincludestwoscripthosts:theconsole-basedCScriptandtheGUI-basedWScript.Thetwoscripthostsprovidenearlyidenticalcapabilities,andinmostcases,itdoesnotmatterwhichofthescripthostsyouusetorunyourscripts.
Thetwoexceptionslieinhowyouinteractwithascript;thatis,howyougetinformationintoascript(input)andhowthescriptdisplaysinformationithasretrieved(output).Ingeneral,CScriptreceivesinputfromthecommandpromptanddisplaysoutputinacommandwindow.WScript,bycontrast,receivesinputthroughagraphicaldialogboxanddisplaysoutputinagraphicalmessagebox.
Otherwise,thetwoscripthostsarelargelyidentical:Ifyouhaveascriptthatdoesnotrequireuserinteraction,youcanrunthatscriptundereitherCScriptorWScript.Forexample,thefollowingscriptmapsanetworkdrive.Becauseitneitherrequiresinputnordisplaysoutput,itrunsexactlythesameundereitherscripthost:
SetobjNetwork=Wscript.CreateObject("WScript.Network")objNetwork.MapNetworkDrive"g:","\\atl-fs-01\Sales"
Ontheotherhand,thefollowingscriptwhichdisplaysaseriesofmessagesrunsmuchdifferentlyunderCScript(wherethemessagesaredisplayedasindividuallineswithinacommandwindow)andWScript(wherethemessagesaredisplayedasaseriesofmessageboxes).Ifyouareinterestedinseeingthedifferenceforyourself,copythescriptintoNotepad,saveitwitha.vbsfileextension,andthenrunitunderbothCScriptandWScript.(Formoreinformationaboutrunningscriptsunderascripthost,see"RunningWSHScripts"laterinthischapter.)
Wscript.Echo"Line1." Wscript.Echo"Line2." Wscript.Echo"Line3." Wscript.Echo"Line4."