使用循环使用批处理脚本

时间:2018-06-14 13:09:43

标签: batch-file for-loop if-statement findstr blat

我有一个包含文件的文件夹: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”的文件,然后发送一封电子邮件到我的email-id,就像通知说“这个文件已经被拒绝,请检查“。

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

1 个答案:

答案 0 :(得分:0)

将@Stephans建议包装成批处理:

  • Findstr /M仅报告与REJECTED
  • 匹配的文件名
  • for /f处理此输出,集合收集此内容 变量找到了。第一个条目产生一个主要逗号。
  • 最后将此变量内容作为参数传递给子例程 :Blat第一个逗号被第2个子串%Found:~1%删除 pos(零基础)
  • 在子目录中,您可以使用%* =(所有传递的参数)将其作为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,因此您必须调整变量以满足您的需要或首先设置其他工作目录。