如何将.CMD文件的日志写入文本文件?

时间:2022-11-23 14:05:10

I have a sample.cmd file with some commands , I want to write log of that command file to a text file?

我有一个带有一些命令的sample.cmd文件,我想将该命令文件的日志写入文本文件?

2 个解决方案

#1


1  

Use

>"theFile.log" (
 command1
 command2
 ...
)

As inside a block, any output is redirected to theFile.log. More over, the file is opened and closed only once.

在块内部,任何输出都会重定向到File.log。此外,该文件仅打开和关闭一次。

As all output is sent to theFile.log, if you want to display some info on the screen, then redirect it to console >CON

当所有输出都发送到theFile.log时,如果要在屏幕上显示某些信息,则将其重定向到console> CON

>"theFile.log" (
 command1
 command2
 command3>CON & rem this is echoed in screen 
 ...
)

Any time you use > file is created. If you want append mode change >"theFile.log" ( to >>"theFile.log" (

任何时候你创建使用>文件。如果你想要追加模式更改>“theFile.log”(到>>“theFile.log”(

EDIT, for your comment

编辑,为您的评论

Don't understand your logic...

不明白你的逻辑......

ECHO ON 
REM STEP 1- CHECK FILE 
IF EXIST C:\SAMPLE.TXT (DEL C:\SAMPLE.TXT) 
findstr /I /B .START C:\SAMPLE.TXT > C:\SAMPLE_START.TXT 
EXIT 16

You are running findstr on a file just deleted.

您正在刚刚删除的文件上运行findstr。

If you want to check if test if .START is present or not

如果要检查测试是否存在.START

@echo off
rem STEP 1- CHECK FILE 
rem Think this line must be 
IF EXIST C:\SAMPLE_START.TXT DEL C:\SAMPLE_START.TXT 

findstr /I /B .START C:\SAMPLE.TXT > C:\SAMPLE_START.TXT && (
  start C:\SAMPLE_START.TXT
) || (
  echo Not found
)  
rem EXIT 16 
exit/B

This uses the conditional operators && (previous command successful) and || (previous command failed).

这使用条件运算符&&(上一个命令成功)和|| (上一个命令失败)。

#2


0  

Got it... just need to run the file as below on command prompt

得到它...只需要在命令提示符下运行如下文件

C:\user\username>"Test.cmd">"test.log"

Thanks for your suggestions...

谢谢你的建议......

#1


1  

Use

>"theFile.log" (
 command1
 command2
 ...
)

As inside a block, any output is redirected to theFile.log. More over, the file is opened and closed only once.

在块内部,任何输出都会重定向到File.log。此外,该文件仅打开和关闭一次。

As all output is sent to theFile.log, if you want to display some info on the screen, then redirect it to console >CON

当所有输出都发送到theFile.log时,如果要在屏幕上显示某些信息,则将其重定向到console> CON

>"theFile.log" (
 command1
 command2
 command3>CON & rem this is echoed in screen 
 ...
)

Any time you use > file is created. If you want append mode change >"theFile.log" ( to >>"theFile.log" (

任何时候你创建使用>文件。如果你想要追加模式更改>“theFile.log”(到>>“theFile.log”(

EDIT, for your comment

编辑,为您的评论

Don't understand your logic...

不明白你的逻辑......

ECHO ON 
REM STEP 1- CHECK FILE 
IF EXIST C:\SAMPLE.TXT (DEL C:\SAMPLE.TXT) 
findstr /I /B .START C:\SAMPLE.TXT > C:\SAMPLE_START.TXT 
EXIT 16

You are running findstr on a file just deleted.

您正在刚刚删除的文件上运行findstr。

If you want to check if test if .START is present or not

如果要检查测试是否存在.START

@echo off
rem STEP 1- CHECK FILE 
rem Think this line must be 
IF EXIST C:\SAMPLE_START.TXT DEL C:\SAMPLE_START.TXT 

findstr /I /B .START C:\SAMPLE.TXT > C:\SAMPLE_START.TXT && (
  start C:\SAMPLE_START.TXT
) || (
  echo Not found
)  
rem EXIT 16 
exit/B

This uses the conditional operators && (previous command successful) and || (previous command failed).

这使用条件运算符&&(上一个命令成功)和|| (上一个命令失败)。

#2


0  

Got it... just need to run the file as below on command prompt

得到它...只需要在命令提示符下运行如下文件

C:\user\username>"Test.cmd">"test.log"

Thanks for your suggestions...

谢谢你的建议......