-2

I have a a folder with the files: File1.txt and File2.txt

The contents of file1.txt are:

"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" 

Contents of file2.txt are:

"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"

I am looking for a batch script to scan through the contents of these 2 files separately and identify the files which contain the word "REJECTED" and then send an email to my email-id like a notification saying "this file has been rejected, kindly check".

  • I am using blat to send emails as notifications *
Stephan
  • 53,940
  • 10
  • 58
  • 91
Aditya
  • 3
  • 1
  • 2
  • 1
    `findstr /m "REJECTED" file?.txt` – Stephan Jun 14 '18 at 13:18
  • just the name(s) of the file(s) which contains the string `REJECTED` – Stephan Jun 14 '18 at 13:30
  • Fine. And what is your specific programming-related question that suits this site, according to the [tour]? Please learn [ask] here! Provide a [mcve] of your coding attempts and precisely describe what you having trouble with... – aschipfl Jun 14 '18 at 15:23

1 Answers1

0

To wrap @Stephans suggestion into a batch:

  • Findstr /M reports only file names which had a match on REJECTED,
  • The for /f processes this output and the set gathers this in the variable Found. The first entry produces a leading comma.
  • When finally passing this variable content as an argument to the subroutine :Blat this first comma is removed by substring %Found:~1% from 2nd pos (zero based)
  • In the sub you can use %* = (all passed arguments) to have it as mail subject or body for your blat routine.

:: 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)

Sample output:

> SO_50858355.cmd
Yourblatcommand found Rejected in file1.txt

new sample output:

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

The batch file as is searches only in the current folder and with the wildcard file?.txt so you have to adapt the variables to fit your needs or first set anotther working dir.

  • Can u explain how this works "Call :Blat %Found:~1%" and "Echo.....in %*" – Aditya Jun 14 '18 at 15:39
  • See changed answer with some more explanations. –  Jun 14 '18 at 15:51
  • I tried to run this code, but the cmd just crashes without anything happening. – Aditya Jun 14 '18 at 17:30
  • Huh, what do mean with `crashes`? How do you run/start the batch. There are some geenral rules how to debug a batch. See this [answer from today](https://stackoverflow.com/a/50858801/6811411) the rules apply here also. And [SO] isn't a script writing service, but a site where programmers help colleages to fix a distinct problem with their code. Since it's **your** task you should be the one doing research, find bugs and remove them. –  Jun 14 '18 at 17:48
  • Hey! It works perfect. If both the files have the word REJECTED then I am getting output as "file1,file2 were rejected" How do I change that to "File 1 was rejected File 2 was rejected" ? – Aditya Jun 14 '18 at 20:32
  • The variable Found is passed to the sub unquoted with the commas in between file names. I used `%*` to have them in one piece while it are really several arguments you can process one after the other using %1 and if %2 isn't empty shift and go back to the label :blat. I'll change the answer in a minute. It's up to you how to integrate this in your blat sending routine. –  Jun 14 '18 at 20:43