如何在命令行输出重定向


如何在命令行输出重定向
2010年08月02日
  要使用 WSH 带有任何控制台命令,需要以下两行: 
  在 VBScript 中: 
  Set WshShell = WScript.CreateObject("WScript.Shell")
  WshShell.Run("%comspec% /c ")
  在 JScript 中: 
  var WshShell = WScript.CreateObject("WScript.Shell");
  WshShell.Run("%comspec% /c ");
  下面是如何使用 DIR 命令的具体示例。此命令的典型的参数是源目录路径和目标输出文件: 
  在 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("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 If
  Else
  Wscript.Echo "dir.vbs usage: dir.vbs  "
  Wscript.Echo "example: dir.vbs c:\temp c:\test.txt"
  End If
  在 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("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  "); 
  WScript.Echo("example: cscript.exe dir.js c:\\temp c:\\dir.txt");
  }
  有关更多的信息,请访问 Microsoft 开发人员网络 (MSDN) 的网站: 
  http://msdn.microsoft.com/library (http://msdn.microsoft.com/library) 
  有关更多的信息,请访问 Microsoft Windows 脚本技术网站: 
  http://msdn2.microsoft.com/en-us/library/ms950396. aspx (http://msdn2.microsoft.com/en-us/library/ms950396. aspx) 
  -------------------------------------------------- ------------------------------
  这篇文章中的信息适用于:
  Microsoft Windows Scripting Host 2.5

猜你喜欢

转载自wcvq08wcvq.iteye.com/blog/1363213
今日推荐