-2

我有一个包含文件的文件夹:File1.txt 和 File2.txt

file1.txt 的内容为:

"DTS053C0 RUN DATE 10/01/11 DATATRAK SYSTEM PAGE 001
RUN TIME 13:35:08
INPUT PROGRAM TRANSMISSION STATUS REPORT
STATUS - INPUT RECEIVED BY DTCC'S DATATRAK SYSTEM
DETAIL RECORDS RECEIVED 0
HEADER RECORD RECEIVED
HDR.SSYSID.E00.CORIG.SSUBOMMDDYYYY HEADERFILEDESCRIPTION N001 *
REJECTED
NO MATCH ON EXPECTED MASTER FOR HEADER" 

file2.txt 的内容是:

"The confirm file received from DTCC will be in the following format:
DTS053C0 RUN DATE 10/01/11 DATATRAK SYSTEM PAGE 001
RUN TIME 12:53:32
INPUT PROGRAM TRANSMISSION STATUS REPORT
STATUS - INPUT RECEIVED BY DTCC'S DATATRAK SYSTEM
DETAIL RECORDS RECEIVED 22
HEADER RECORD RECEIVED
HDR.SSYSID.E00.CORIG.SSUBOMMDDYYYY HEADERFILEDESCRIPTION N001 *
ACCEPTED
Example"

我正在寻找一个批处理脚本来分别扫描这两个文件的内容并识别包含单词“REJECTED”的文件,然后向我的电子邮件 ID 发送一封电子邮件,如通知“此文件已被拒绝,请查看”。

  • 我正在使用 blat 发送电子邮件作为通知 *
4

1 回答 1

0

将@Stephans 建议打包成一个批次:

  • Findstr /MREJECTED仅报告与,匹配的文件名
  • for /f处理此输出并且集合将其收集在变量 Found 中。第一个条目产生一个前导逗号。
  • 当最终将此变量内容作为参数传递给子例程 :Blat 时,第一个逗号被%Found:~1%第二个 pos 的子字符串删除(从零开始)
  • 在 sub 中,您可以使用%*=(所有传递的参数)将其作为您的 blat 例程的邮件主题或正文。

:: Q:\Test\2018\06\14\SO_50858355.cmd
@Echo off & Setlocal EnableDelayedExpansion
Set "Search=REJECTED"
Set "Files=file?.txt"
Set "Found="
for /f "delims=" %%A in ('
  findstr /m /i "%Search%" %Files%
') Do set "Found=!Found!,%%A"
If Defined Found (
    Call :Blat %Found:~1%
) Else (
    Echo No files "%Files%" containing "%Search%" found
)
Pause
Goto :Eof

:Blat
Echo found Rejected in %1
If "%2" neq "" (shift & goto :Blat)

样本输出:

> SO_50858355.cmd
Yourblatcommand found Rejected in file1.txt

新样本输出:

> SO_50858355.cmd
found Rejected in file3.txt
found Rejected in file1.txt

批处理文件仅在当前文件夹中搜索并使用通配符file?.txt,因此您必须调整变量以满足您的需要或首先设置另一个工作目录。

于 2018-06-14T13:55:44.153 回答