开发者

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

in linux, let say i want to start tomcat, and want 开发者_JAVA百科to direct all the console log into a text file, i can do write a bash script:

./startup.sh > output.txt

but in windows, can we do the similar stuff using .bat script.

how can i write this bat script???

(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)


It's exactly the same in Windows.

i.e

ping google.com > logfile.txt

or in powershell you could do the following to display the log to console as well

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


Coming from the linux world you will find these unxutils for windows very useful. With that you can even say:

whatever | tee.exe text.txt

...to see the output and save it to text.txt at the same time.


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.

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜