将命令的输出复制到批处理脚本中的2个日志文件中。

huangapple go评论63阅读模式
英文:

copy the output of a command into 2 log files in batch scripting

问题

在批处理脚本中的场景:

有 file1.log 和 file2.log

set file1 = D:\Test\file1.log

set file2 = D:\Test\file2.log

还有一个命令

if exist "%abc%" xcopy %abc% "D:\Test" /Y >> %file1%

我希望相同的命令输出也被复制到 file2

我尝试了
Type %file1% > %file2% ----> 但然后我失去了 file2 中的数据
Type %file1% >> %file2% ----> 生成了很多重复数据

有没有一种同时执行的方法?可以将命令的输出存储在一个变量中,然后稍后写入这两个文件中?

英文:

Scenario in batch scripting:

There is file1.log and file2.log

set file1 = D:\Test\file1.log

set file2 = D:\Test\file2.log

and there is a command

if exist "%abc%" xcopy %abc% "D:\Test" /Y >> %file1%

I want the same command output to also be copied to file2

I tried
Type %file1% > %file2% ----> but then I lose the data in file2 and

Type %file1% >> %file2% ----> generates lot of duplicate data

Is there a way to do it simultaneously? Can the output of the command be stored in a variable and later written into the 2 files?

答案1

得分: 0

以下是您提供的代码的翻译部分:

if exist "%abc%" (
 xcopy %abc% "D:\Test" /Y > %file3%
 type %file3%>>%file1%
 type %file3%>>%file3%
 del %file3%
)

if exist "%abc%" (for /f "delims=" %%r in ('xcopy %abc% "D:\Test" /Y') do echo %%r>>%file1%&echo %%r>>%file2%

请注意,我已将代码中的HTML实体转义字符还原为其原始形式,以便更好地理解。如果您需要更多帮助,请随时提问。

英文:
if exist "%abc%" (
 xcopy %abc% "D:\Test" /Y > %file3%
 type %file3%>>%file1%
 type %file3%>>%file3%
 del %file3%
)

or

if exist "%abc%" (for /f "delims=" %%r in ('xcopy %abc% "D:\Test" /Y') do echo %%r>>%file1%&echo %%r>>%file2%

spring immediately to mind.

huangapple
  • 本文由 发表于 2023年3月7日 16:30:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定