Previous Topic: Accessing the Operating SystemNext Topic: Running Scripts


Using the OSRedirect Class

AutoShell uses the predefined OSRedirect class for output redirection when invoking child commands. To redirect output and capture the child process return code, create an OSRedirect object and pass it in the command invocation using the optional –output clause:

out = new OSRedirect();
! dir -output out

When you interactively execute this command in the AutoShell console, it does not display any information in the console. It redirects the output to the specified object. After completion, you can call a set of methods on the specified object to get information about the command execution:

? "Error occurred during command execution:", out.errorOccurred()
? "Did command complete:", out.hasCompleted()
? "Output produced by command:", out.output()
? "Command return code (errorlevel):", out.result()

The methods output() and result() return the required information. The output() method returns a string with the complete directory listing. You can use the standard JavaScript string functions to further process this output. For example, parse the lines into an array and display them line-by-line:

lines=out.output().split("\n");
for(i=0;i<lines.length;i++)qout(lines[i]);

If you want to invoke the dir operating system command with the ! AutoShell command, allocate an OSRedirect object before issuing the ! command. To save this step, you can also invoke OS commands using the !! command. The !! command creates an OSRedirect object and assigns it to the specified variable if it does not exist.

The following command outputs the current directory listing to the variable out, even if no OSRedirect object is explicitly allocated:

!! dir -output out

If you issue the !! command without the optional -output clause, it automatically writes the directory information to variable $$stdout:

!! dir
? $$stdout.output()