英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论