Microsoft KB Archive/278411

= How to redirect command-Line output =

Article ID: 278411

Article Last Modified on 8/8/2007

-

APPLIES TO


 * Microsoft Windows Scripting Host 2.5
 * Microsoft Windows Scripting Host 2.5

-



This article was previously published under Q278411



SUMMARY
The purpose of this article is to describe how to redirect command-line output by way of Windows Scripting Host (WSH) in VBScript (.vbs) and JScript (.js).



MORE INFORMATION
The following two lines are required to use any console command with WSH:   In VBScript: Set WshShell = WScript.CreateObject(&quot;WScript.Shell&quot;) WshShell.Run(&quot;%comspec% /c &quot;)   In JScript: var WshShell = WScript.CreateObject(&quot;WScript.Shell&quot;); WshShell.Run(&quot;%comspec% /c &quot;); 

The following is a specific example of how to use the DIR command. For this command, typical arguments are a source directory path and a destination output file:   In VBScript: Option Explicit

Dim WshShell Dim fso Dim src, dest

'Create the object that is used to execute the command-line output. Set WshShell = Wscript.CreateObject(&quot;Wscript.Shell&quot;) 'Create the object that is used to create your destination file. Set fso = WScript.CreateObject(&quot;Scripting.FileSystemObject&quot;)

'Read in the arguments that are passed to the script. If Wscript.Arguments.Count = 2 Then 'Store the arguments. src = WScript.Arguments(0) dest = WScript.Arguments(1) 'Make sure that the source path exists. If fso.FolderExists(src) Then 'Make sure the destination path exists. If Left(dest, InstrRev(dest, &quot;\&quot;)) = &quot;&quot; or fso.FolderExists(Left(dest, InstrRev(dest, &quot;\&quot;))) Then 'Execute the command-line output command. WshShell.Run &quot;%comspec% /c Dir &quot; & chr(34) & src & chr(34) & &quot; > &quot; & chr(34) & dest & chr(34) Else 'Present useful errors. WScript.Echo &quot;** Destination path not found ** &quot; & Left(dest, InstrRev(dest, &quot;\&quot;)) End If Else WScript.Echo &quot;** Source directory not found ** &quot; & src End If Else Wscript.Echo &quot;dir.vbs usage: dir.vbs &quot; Wscript.Echo &quot;example: dir.vbs c:\temp c:\test.txt&quot; End If

  In JScript: var sPath var dPath var x var quote = String.fromCharCode(34); // Create the object to run the command-line output. var WshShell = WScript.CreateObject(&quot;WScript.Shell&quot;); // Create the object that is used to write the output file. var fso = WScript.CreateObject(&quot;Scripting.FileSystemObject&quot;); // Read in the arguments that are passed to the command. var objArgs = WScript.Arguments; // Error checking to make sure that two arguments are passed. if (objArgs.length == 2) { sPath = objArgs.item(0); dPath = objArgs.item(1); // Make sure that the source path exists. if (fso.FolderExists(sPath)) {   x = dPath.lastIndexOf(&quot;\\&quot;); // Make sure the destination path exists. if ((x == -1) || (fso.FolderExists(dPath.substring(0, x)))) {     WshShell.Run(&quot;%comspec% /c Dir &quot; + quote + sPath + quote + &quot; > &quot; + quote + dPath + quote); }   else WScript.Echo(&quot;** Destination path not found ** &quot; & tmp2dPath); } else WScript.Echo(&quot;** Source path not found ** &quot; & sPath); } else { WScript.Echo(&quot;dir.js usage: dir.js  &quot;); WScript.Echo(&quot;example: cscript.exe dir.js c:\\temp c:\\dir.txt&quot;); }

</ul>

For more information, visit the Microsoft Developer Network (MSDN) Web site:

http://msdn.microsoft.com/library

For additional information, visit the Microsoft Windows Script Technologies Web site:

http://msdn2.microsoft.com/en-us/library/ms950396.aspx

Keywords: kbdswmanage2003swept kbhowto KB278411

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.