【问题标题】:How to output specific lines from a batch routine?如何从批处理例程中输出特定行?
【发布时间】:2016-10-13 20:39:46
【问题描述】:

我有以下批处理脚本,对给定文件夹中的所有文件进行递归迭代:

FOR /R %%i IN ("*.wmv") DO "C:\Program Files\7-Zipa\7za.exe" a -mx0 -tzip -pPassword -mem=AES256 -y "%%~dpni.zip" "%%i"

在运行时,它会为每个它处理的文件生成以下输出:

 7-Zip (a) [64] 16.04 : Copyright (c) 1999-2016 Igor Pavlov : 2016-10-04

 Scanning the drive:
 1 file, 382316 bytes (374 KiB)

 Creating archive: C:\test\7208969.zip

 Items to compress: 1


 Files read from disk: 1
 Archive size: 382524 bytes (374 KiB)
 Everything is Ok

我想要创建一个输出文本文件,每次迭代只包含这两行:

Creating archive: C:\test\7208969.zip
Everything is Ok

我尝试使用类似的方式从另一个批处理中调用主批处理脚本(这只是一个说明性示例):

FOR /F "(tried with skip, delims, tokens, etc)" %%G IN ('7zip.bat') DO echo %%G

但我尝试过的所有东西都写了很多奇怪的东西,除了我需要的东西。

如果有人能告诉我如何以任何方式完成此任务,将不胜感激!

【问题讨论】:

    标签: windows batch-file for-loop


    【解决方案1】:

    只需使用 findstr 和 2 个表达式在循环中输出命令输出,每行一个。

    FOR /R %%i IN ("*.wmv") DO "C:\Program Files\7-Zipa\7za.exe" a -mx0 -tzip -pPassword -mem=AES256 -y "%%~dpni.zip" "%%i" | findstr /C:"Creating archive" /C:"Everything"
    

    在这一点上,我没有出现问题的情况。显然,您可以添加任意数量的字符串:

    ... | findstr /C:"Creating archive" /C:"Everything is OK" /C:"Something went wrong"
    

    【讨论】:

    • 谢谢一百万!。你完全把它钉在了最简单的地方! =)。当我尝试它时发生的一件事是,如果我使用“创建存档”它不会输出该行,但是当只使用“创建”时,它就像一个魅力。
    • 完全!对不起,我以前没有接受它,我对这个社区有点陌生。再次感谢! =)
    猜你喜欢
    • 2015-09-28
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多