`
wcvq08wcvq
  • 浏览: 14801 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

如何在命令行输出重定向

 
阅读更多

如何在命令行输出重定向
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
分享到:
评论

相关推荐

    c#命令行输出重定向demo

    c#命令行输出重定向demo.比如可以直接在textbox中输入ipconfig,然后程序会在另一个TextBox中实时显示出命令行的执行结果.源码简单易懂,有一定的参考价值.

    Windows cmd命令行输入输出重定向问题

    主要介绍了Windows cmd命令行输入输出重定向问题,需要的朋友可以参考下

    重定向命令行程序的输出到windows剪贴板

    功能:把命令行程序的输出重定向到剪贴板。用来做演示文档,PPT等非常有用。自用程序,做的很简单,没有容错,请别见怪。 用法:[command] [option]|cmd2cb 例如:dir|cmd2cb 会把dir的输出都重定向到剪贴板,...

    vim编辑器中掌握输入输出重定向与管道命令的应用

    掌握输入输出重定向与管道命令的应用 1.使用重定向 重定向就是不使用系统的标准输入端口、标准输出端口或标准错误端口,而进行重新的指定,所以重定向分为输入重定向、输出重定向和错误重定向。通常情况下,重定向到...

    Go-websocketd把命令行的输出stdout和stderr重定向为一个websocket服务的工具

    websocketd是一个小型命令行工具,它将包装现有的命令行界面程序,并允许通过WebSocket访问它。

    通过管道进行cmd进程输入输出重定向,源代码

    源代码这是一个头文件,解决了命令行输入输出重定向问题,可以直接使用。测试代码如下: #include "console.h" #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { Console console; if( ...

    MyLog (Window下DOS重定向)

    一个用于windows 下命令行输出重定向的工具. 有时候我们即需要命令行在shell上有输出, 同时也希望输出能重定向到log 文件中. 在Windows下并没有直接的命令来作这个, 所以我自己写了个小程序.

    Linux标准输入、输出和重定向

    执行一个shell命令行时通常会自动打开三个标准文件,即标准输入文件(stdin),通常对应终端的键盘;标准输出文件(stdout)和标准错误输出文件(stderr),这两个文件都对应终端的屏幕。进程将从标准输入文件中得到...

    操作系统期末考试试题

    是一些考试非常有可能会用到的,希望对大家有帮助

    Linux中crontab输出重定向不生效问题的解决方法

    近期在crontab中添加了一个定时任务,该任务执行之后默认会有正常输出。为了确保在任务执行过程中的异常信息也可以捕获,方便问题定位,因此在crontab中我写了这么一条命令: 01 09 * * * cd /opdir/test/ && ./...

    神奇的shell命令行输入与输出功能介绍

    标准输入/输出和重定向,Linux发行版Fedora Core Linux,而Red Hat公司原来Red Hat Linux的开发团队也将继续参与这一发行版本的开发工作。 标准输入与输出 我们知道,执行一个shell命令行时通常会自动打开三个标准...

    浅析Linux中重定向问题

     在计算领域,重定向是大多数命令行解释器所具有的功能,包括各种可以将标准流重定向用户规定地点的Unix shells。类Unix操作系统的程序可以通过dup2系统调用完成重定向,或者通过缺少一些灵活性但是更高一级层次的...

    Windows管道(Pipe)重定向stdout,stderr,stdin

    有时我们需要重定向stdout,stderr,stdin。比如:将输出写入文件,又或者我们要将命令行程序输出结果显示到Windows对话框中。源码示范如何在Windows中实现,封装了几个简单易用的函数。

    Bash Shell入门

    linux bash shell学习 Shell基本语法: 1. Shell脚本的第一行中的符号...输出重定向符“>”的作用是把命令的标准输出重新定向到指定文件。输出附加定向符“>>”将命令的输出附加到指定文件的后面,而不破坏原有的内容。

    shell脚本编程基础(二)

    重定向输入和输出 想要保存某个命令的输出,可以将命令的输出...在命令行上,命令总是在左侧,而重定向符号“指向”数据流动的方向。 wc命令可以对数据中的文本进行计数。默认情况下输出文本的行数、词数、字节数

    myshell操作系统

    在ubuntu环境下编写的myshell,实现输入输出重定向和管道功能。

    操作系统期末试题(9套)

    1 分别举出一个在命令行使用输入重定向、输出重定向、管道的例子,并说明使用相应命令的意图。 2 对比“进程”和“线程”的异同。 3 对比动态分页系统中的全局、局部页面置换策略。 4 什么是设备无关性?...

    bash shell学习笔记

    四、呈现数据:在脚本中重定向输入与输出、创建自己的重定向、阻止输出、创建临时文件、记录消息 五、控制脚本:处理信号、后台运行脚本、非控制台运行脚本、定时运行作业等 六、创建函数:基本的脚本函数、返回值、...

    命令行工具curl的常用命令汇总.doc

    Curl可以在终端中使用,通过命令行发送HTTP请求,获取响应并将其输出到终端。它可以用于测试API、下载文件、上传文件、发送HTTP基本认证、发送cookie数据、设置请求头等多种用途。Curl支持各种请求方法,包括GET、...

Global site tag (gtag.js) - Google Analytics