Windows Script Host常用方法和属性
1.Exec Method
Runs an application in a child command-shell, providing access to the StdIn/StdOut/StdErr streams.
object.Exec(strCommand)
Arguments
object
WshShell object.
strCommand
String value indicating the command line used to run the script. The command line should appear exactly as it would if you typed it at the command prompt.
Remarks
The Exec method returns a WshScriptExec object, which provides status and error information about a script run with Exec along with access to the StdIn, StdOut, and StdErr channels. The Exec method allows the execution of command line applications only. The Exec method cannot be used to run remote scripts. Do not confuse the Exec method with the Execute method (of the WshRemote object).
Example
The following example demonstrates the basics of the Exec method.
VBScript
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("calc")
Do While oExec.Status = 0
WScript.Sleep 100
Loop
WScript.Echo oExec.Status
JScript
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("calc");
while (oExec.Status == 0)
{
WScript.Sleep(100);
}
WScript.Echo(oExec.Status);
2.StdOut Property (WshScriptExec)
Exposes the write-only stdout output stream of the Exec object.
Object.StdOut
Arguments
Object
WshScriptExec object.
Remarks
The StdOut property contains a read-only copy of any information the script may have sent to the standard output.
Example
The following code starts a batch file and waits for the user input prompt. After entering the needed data through the StdIn stream, the batch file will be able to complete.
VBScript:::
Dim WshShell, oExec, input
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("test.bat")
input = ""
Do While True
If Not oExec.StdOut.AtEndOfStream Then
input = input & oExec.StdOut.Read(1)
If InStr(input, "Press any key") <> 0 Then Exit Do
End If
WScript.Sleep 100
Loop
oExec.StdIn.Write VbCrLf
Do While oExec.Status <> 1
WScript.Sleep 100
Loop
JScript::::
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("test.bat");
var input = "";
while (true)
{
if (!oExec.StdOut.AtEndOfStream)
{
input += oExec.StdOut.Read(1);
if (input.indexOf("Press any key") != -1)
break;
}
WScript.Sleep(100);
}
oExec.StdIn.Write("\n");
while (oExec.Status != 1)
WScript.Sleep(100);
3.Run Method
Runs a program in a new process.
object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
Arguments
object
WshShell object.
strCommand
String value indicating the command line you want to run.
You must include any parameters you want to pass to the executable file.
intWindowStyle
Optional. Integer value indicating the appearance of the program's window. Note that not all programs make use of this information.
bWaitOnReturn
Optional. Boolean value indicating whether the script should wait for the program to finish executing before continuing to the next statement in your script. If set to true, script execution halts until the program finishes, and Run returns any error code returned by the program. If set to false (the default), the Run method returns immediately after starting the program, automatically returning 0 (not to be interpreted as an error code).
Remarks
The Run method returns an integer. The Run method starts a program running in a new Windows process. You can have your script wait for the program to finish execution before continuing. This allows you to run scripts and programs synchronously. Environment variables within the argument strCommand are automatically expanded. If a file type has been properly registered to a particular program, calling run on a file of that type executes the program. For example, if Word is installed on your computer system, calling Run on a *.doc file starts Word and loads the document. The following table lists the available settings for intWindowStyle.
[intWindowStyle] Description
[0]Hides the window and activates another window.
[1]Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
[2]Activates the window and displays it as a minimized window.
[3]Displays a window in its most recent size and position. The active window remains active.
[4]Activates the window and displays it in its current size and position.
[5]Minimizes the specified window and activates the next top-level window in the Z order.
[6]Displays the window as a minimized window. The active window remains active.
[7]Displays the window in its current state. The active window remains active.
[8] Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
[9]Sets the show-state based on the state of the program that started the application.
The following VBScript code opens a copy of the currently running script with Notepad.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "%windir%\notepad " & WScript.ScriptFullName
The following VBScript code does the same thing, except it specifies the window type, waits for Notepad to be shut down by the user, and saves the error code returned from Notepad when it is shut down.
Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("notepad " & WScript.ScriptFullName, 1, true)
The following VBScript code opens a command window, changes to the path to C:\ , and executes the DIR command.
Dim oShell
Set oShell = WScript.CreateObject ("WSCript.shell")
oShell.run "cmd /K CD C:\ & Dir"
Set oShell = Nothing
// MyWhere.JS
if (WScript.Arguments.Count() == 0)
WScript.Quit();
var Pattern = WScript.Arguments(0);
var Shell = new ActiveXObject("WScript.Shell");
var Pipe = Shell.Exec("%comspec% /c \"cscript //nologo mydir.vbs | cscript //nologo mygrep.vbs " + Pattern + "\"");
while(!Pipe.StdOut.AtEndOfStream)
WScript.StdOut.WriteLine(Pipe.StdOut.ReadLine());
' VBScript.
' MYDIR.VBS
Option Explicit
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
DoDir FSO.GetFolder(".")
Sub DoDir(Folder)
On Error Resume Next
Dim File, SubFolder
For Each File In Folder.Files
WScript.StdOut.WriteLine File.Path
Next
For Each SubFolder in Folder.SubFolders
DoDir SubFolder
Next
End Sub
' MyGrep.VBS
Option Explicit
Dim RE, Line
If WScript.Arguments.Count = 0 Then WScript.Quit
Set RE = New RegExp
RE.IgnoreCase = True
RE.Pattern = WScript.Arguments(0)
While Not WScript.StdIn.AtEndOfStream
Line = WScript.StdIn.ReadLine
If RE.Test(Line) Then WScript.StdOut.WriteLine Line
WEnd
作者:anonymous 更新日期:2005-01-09
来源:internet
浏览次数:
相关文章
相关评论 发表评论
- No Comments