windows 如何在Windows中的bat脚本中将输出直接输出到txt文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1617778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to direct output into a txt file in bat script in windows
提问by jojo
in linux, let say i want to start tomcat, and want to direct all the console log into a text file, i can do write a bash script:
在 linux 中,假设我想启动 tomcat,并且想将所有控制台日志定向到一个文本文件中,我可以编写一个 bash 脚本:
./startup.sh > output.txt
./startup.sh > 输出.txt
but in windows, can we do the similar stuff using .bat script.
但是在 Windows 中,我们可以使用 .bat 脚本来做类似的事情吗?
how can i write this bat script???
我该如何编写这个 bat 脚本???
(i know the proper way should ask the application to do the log, but the application is not written by me, they print the log using system.out.print, all the logs are in the console, but i want to archive the log, in case something happen i can back trace)
(我知道正确的方法应该让应用程序做日志,但应用程序不是我写的,他们使用 system.out.print 打印日志,所有日志都在控制台中,但我想归档日志,万一发生什么事我可以回溯)
回答by Matt Davison
It's exactly the same in Windows.
这在 Windows 中完全相同。
i.e
IE
ping google.com > logfile.txt
or in powershell you could do the following to display the log to console as well
或者在 powershell 中,您也可以执行以下操作以将日志显示到控制台
ping google.com | tee-object -filepath C:\mylog.txt
回答by tangens
回答by Faiz
You can write a bar script to do all that you want and then send the entire output to a file or you can specify some portion of the output to file by specifying the redirection in side the .bat file.
您可以编写一个 bar 脚本来完成您想要的所有操作,然后将整个输出发送到一个文件,或者您可以通过在 .bat 文件旁边指定重定向来指定输出的某些部分到文件。
Case 1:
情况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:\>
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:\>
Case 2:
案例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:\>