使用循环通过批处理脚本重命名文件名

Using loops to rename filenames with batch script

我有一个包含以下文件的文件夹: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"

我正在寻找一个批处理脚本来分别扫描这 2 个文件的内容并识别包含单词 "REJECTED" 的文件,然后将电子邮件发送到我的电子邮件 ID,就像通知说 "this file has been rejected, kindly check".

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

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

:: Q:\Test18\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 因此您必须调整变量以满足您的需要或首先设置另一个工作目录。