You are currently offline, waiting for your internet to reconnect

How to redirect command-Line output

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
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("WScript.Shell")WshShell.Run("%comspec% /c <Console Command>")					
  • In JScript:
    var WshShell = WScript.CreateObject("WScript.Shell");WshShell.Run("%comspec% /c <Console Command>");					
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 ExplicitDim WshShellDim fsoDim src, dest'Create the object that is used to execute the command-line output.Set WshShell = Wscript.CreateObject("Wscript.Shell")'Create the object that is used to create your destination file.Set fso = WScript.CreateObject("Scripting.FileSystemObject")'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, "\")) = "" or fso.FolderExists(Left(dest, InstrRev(dest, "\"))) Then      'Execute the command-line output command.      WshShell.Run "%comspec% /c Dir " & chr(34) & src & chr(34) & " > " & chr(34) & dest & chr(34)    Else      'Present useful errors.      WScript.Echo "** Destination path not found ** " & Left(dest, InstrRev(dest, "\"))    End If  Else    WScript.Echo "** Source directory not found ** " & src  End IfElse  Wscript.Echo "dir.vbs usage: dir.vbs <source path> <destination file>"  Wscript.Echo "example: dir.vbs c:\temp c:\test.txt"End If					
  • In JScript:
    var sPathvar dPathvar xvar quote = String.fromCharCode(34);// Create the object to run the command-line output.var WshShell = WScript.CreateObject("WScript.Shell");// Create the object that is used to write the output file.var fso = WScript.CreateObject("Scripting.FileSystemObject");// 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("\\");    // Make sure the destination path exists.    if ((x == -1) || (fso.FolderExists(dPath.substring(0, x))))    {      WshShell.Run("%comspec% /c Dir " + quote + sPath + quote + " > " + quote + dPath + quote);    }    else      WScript.Echo("** Destination path not found ** " & tmp2dPath);  }  else    WScript.Echo("** Source path not found ** " & sPath);}else{  WScript.Echo("dir.js usage: dir.js <source path> <destination path>");   WScript.Echo("example: cscript.exe dir.js c:\\temp c:\\dir.txt");}					
For more information, visit the Microsoft Developer Network (MSDN) Web site:For additional information, visit the Microsoft Windows Script Technologies Web site:

Article ID: 278411 - Last Review: 06/19/2014 12:18:00 - Revision: 4.0

  • kbdswmanage2003swept kbhowto KB278411