【问题标题】:how to direct output into a txt file in bat script in windows如何在Windows中的bat脚本中将输出定向到txt文件
【发布时间】:2009-10-24 11:49:12
【问题描述】:

在 linux 中,假设我想启动 tomcat,并且想将所有控制台登录到一个文本文件中,我可以写一个 bash 脚本:

./startup.sh > 输出.txt

但是在 Windows 中,我们可以使用 .bat 脚本来做类似的事情吗?

如何编写这个 bat 脚本???

(我知道正确的方法应该要求应用程序做日志,但应用程序不是我写的,他们使用 system.out.print 打印日志,所有日志都在控制台中,但我想归档日志,以防万一我可以回溯)

【问题讨论】:

    标签: windows scripting batch-file


    【解决方案1】:

    在 Windows 中完全一样。

    ping google.com > logfile.txt
    

    或在 powershell 中,您也可以执行以下操作以将日志显示到控制台

    ping google.com | tee-object -filepath C:\mylog.txt
    

    【讨论】:

      【解决方案2】:

      来自 linux 世界,您会发现这些用于 windows 的 unxutils 非常有用。这样你甚至可以说:

      whatever | tee.exe text.txt
      

      ...查看输出并同时将其保存到 text.txt。

      【讨论】:

      • 查看powershell,即随便| tee-object - 文件名 C:\mylog.txt
      【解决方案3】:

      您可以编写一个 bar 脚本来执行您想要的所有操作,然后将整个输出发送到一个文件,或者您可以通过在 .bat 文件中指定重定向来指定输出的某些部分到文件。

      案例一:

      C:\>copy con 1.bat
      dir
      pause
      dir /b^Z
              1 file(s) copied.
      
      C:\>1
      
      C:\>dir
       Volume in drive C is Windows
       Volume Serial Number is A4FA-F45F
      
       Directory of C:\
      
      10/25/2009  06:05 PM                18 1.bat
      12/19/2007  10:13 AM    <DIR>          Documents and Settings
      09/04/2009  05:30 AM    <DIR>          Program Files
      10/20/2009  11:48 PM    <DIR>          WINDOWS
                     1 File(s)         86,525 bytes
                     3 Dir(s)     946,864,128 bytes free
      
      C:\>pause
      Press any key to continue . . .
      
      C:\>dir /b
      1.bat
      Documents and Settings
      Program Files
      WINDOWS
      
      C:\>1 > test1.txt
      
      C:\>type test1.txt
      
      C:\>dir
       Volume in drive C is Windows
       Volume Serial Number is A4FA-F45F
      
       Directory of C:\
      
      10/25/2009  06:05 PM                18 1.bat
      12/19/2007  10:13 AM    <DIR>          Documents and Settings
      09/04/2009  05:30 AM    <DIR>          Program Files
      10/25/2009  06:06 PM                 0 test1.txt
      10/20/2009  11:48 PM    <DIR>          WINDOWS
                     2 File(s)         86,525 bytes
                     3 Dir(s)     946,860,032 bytes free
      
      C:\>pause
      Press any key to continue . . .
      
      C:\>dir /b
      1.bat
      Documents and Settings
      Program Files
      test1.txt
      WINDOWS
      
      C:\>
      

      案例 2:

      C:\>copy con 2.bat
      dir
      pause
      dir /b > test2.txt^Z
              1 file(s) copied.
      
      C:\>2
      
      C:\>dir
       Volume in drive C is Windows
       Volume Serial Number is A4FA-F45F
      
       Directory of C:\
      
      10/25/2009  06:05 PM                18 1.bat
      10/25/2009  06:14 PM                30 2.bat
      12/19/2007  10:13 AM    <DIR>          Documents and Settings
      09/04/2009  05:30 AM    <DIR>          Program Files
      10/25/2009  06:06 PM             1,112 test1.txt
      10/20/2009  11:48 PM    <DIR>          WINDOWS
                     3 File(s)         87,667 bytes
                     3 Dir(s)     946,601,984 bytes free
      
      C:\>pause
      Press any key to continue . . .
      
      C:\>dir /b  1>test2.txt
      
      C:\>type test2.txt
      1.bat
      2.bat
      Documents and Settings
      Program Files
      test1.txt
      test2.txt
      WINDOWS
      
      C:\>
      

      【讨论】:

        猜你喜欢
        • 2011-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-31
        • 2011-07-10
        • 1970-01-01
        • 2011-05-05
        相关资源
        最近更新 更多