簡體   English   中英

如何在Windows中的bat腳本中將輸出定向到txt文件

[英]how to direct output into a txt file in bat script in windows

在linux中,假設我要啟動tomcat,並想將所有控制台日志都定向到一個文本文件中,我可以編寫一個bash腳本:

./startup.sh> output.txt

但是在Windows中,我們可以使用.bat腳本來做類似的事情嗎?

我怎么寫這個蝙蝠腳本???

(我知道應該要求應用程序執行日志的正確方法,但是應用程序不是我編寫的,他們使用system.out.print打印日志,所有日志都在控制台中,但是我想將日志存檔,以防萬一我可以追溯)

在Windows中完全相同。

ping google.com > logfile.txt

或在powershell中,您也可以執行以下操作以將日志顯示到控制台

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

來自linux世界,您會發現這些用於Windows的unxutils非常有用。 這樣,您甚至可以說:

whatever | tee.exe text.txt

...以查看輸出並同時將其保存到text.txt。

您可以編寫一個條形腳本來執行所需的所有操作,然后將整個輸出發送到文件,或者可以通過在.bat文件中指定重定向來將輸出的一部分指定到文件。

情況1:

 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:\>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM