WatchDirectory Forum
http://www.watchdirectory.net/cgi-bin/yabb25/YaBB.pl
watchDirectory forums >> How to... >> Find text, copy the text after, send email
http://www.watchdirectory.net/cgi-bin/yabb25/YaBB.pl?num=1459294310

Message started by Up2DateTech on Mar 30th, 2016 at 1:31am

Title: Find text, copy the text after, send email
Post by Up2DateTech on Mar 30th, 2016 at 1:31am
Hello i'm after some advice can WatchDirectory help me.

I need to monitor a folder.  When a file appears in this folder.
I need to search it for the string "PC0S1" then capture the text after it, 7 characters long.  Create an email with those 7 characters in the subject. 

Or can do rename file to those 7 characters.

Any help is really appreciated.

Title: Re: Find text, copy the text after, send email
Post by Gert on Mar 30th, 2016 at 6:56am
Hi,

I think this should be possible with a little scripting. Can you zip a few of those files and post them here so I can experiment? If they contain sensitive data, you can also email me.

Title: Re: Find text, copy the text after, send email
Post by Up2DateTech on Mar 31st, 2016 at 6:21am
wow thanks that would be great.  I've attached a copy of several files.  Thanks in advance.
http://www.watchdirectory.net/cgi-bin/yabb25/YaBB.pl?action=downloadfile;file=EPPCFILE.zip (7 KB | 89 )

Title: Re: Find text, copy the text after, send email
Post by Gert on Mar 31st, 2016 at 7:41am
I looked at the files, none of them contain the text PC0S1....


Title: Re: Find text, copy the text after, send email
Post by Up2DateTech on Apr 1st, 2016 at 7:38am
Sorry opening it in notepad it looks different
its the just OS1

Title: Re: Find text, copy the text after, send email
Post by Gert on Apr 1st, 2016 at 6:54pm
Okay,

Here is a script you can use for the email task:

Code (]rem script to get the first 7 chars after the text OS1 into the subject of an email

SET TMP=C:\temp

echo %WD_FILE%

set TMPFILE=%TMP%\%WD_CUREVT%.txt

rem find OS1 and store that complete line into a temporary file
FINDSTR OS1 "%WD_FILE%" > "%TMPFILE%"
IF %errorlevel% geq 1 GOTO :NotFound

rem read the line into a bat variable
FOR /F %%i IN (%TMPFILE%) DO SET FIT=%%i

rem get the first 7 chars after position 4
SET FOUND=%FIT:~4,7%
echo %FOUND% > "%WD_RESULTFILE%"

del "%TMPFILE%"
GOTO :EOF

:NotFound
echo OS1 not found
GOTO :EOF
[/code):

Save this script on your computer as C:\Bin\eppc.bat
Make sure the directory C:\temp exists (needed by the script for a temp file)

In your email task use something like this for the subject of your email:
OS1 Number: $CALL:C:\Bin\eppc.bat$

Please note, if OS1 is not found inside the detected file, the task will crash. If you do not want that, replace the last few lines of the script to
[code]
:NotFound
echo OS1 not found > "%WD_RESULTFILE%"
GOTO :EOF


so you will receive emails with subject
OS1 Number: OS1 not found
for those.

Only tested "lightly", but I think it should work.

Title: Re: Find text, copy the text after, send email
Post by Up2DateTech on May 5th, 2016 at 1:07am
Hello Gert that worked a treat.  Purchased the software and all going well.

Would it be possible to change the file name also with this software ?

Title: Re: Find text, copy the text after, send email
Post by Up2DateTech on May 17th, 2016 at 12:48am
bump

Title: Re: Find text, copy the text after, send email
Post by Gert on May 17th, 2016 at 7:20am
oops, sorry I missed your question.

Renaming is easy enough, but it will conflict with your current email task. Your email will try to attach the original file and when System.Mail tries to send that file will not exist anymore.

There are ways around this but it will make your setup quite complex. Do you REALLY need this rename?

Title: Re: Find text, copy the text after, send email
Post by wesman on Mar 30th, 2017 at 1:45am
Hi Gert

I am looking to do something similar to what this user was doing.

I need to read a file from a folder and look for the characters ABCD and if found then write various parts of the next 10 to 12 characters to another file...

examples of the lines that will be in the original file are:

ABCD 4/13 $100
ABCD 10/13 $180
ABCD 1/1 $122
ABCD 10/9 $155

The only parts of the text following the ABCD that can vary in length (ie number of characters) are the first 2 digits after ABCD - these are US Month values and can be 1 through to 12 (text before the /) and 1 through to 31 (text after the /)

I then need to write these date values together with the digits following the $ symbol to a single concatenated string in another file, in the format MMDDXYZ where XYZ are the 3 numbers following the $ symbol. The / and $ symbols need to be excluded in this new string and both date values written to the file need to be 2 digits long (ie padded with a leading 0 if the date value on either side of the / is a single digit, ie 1 to 9).

For example the output to the file based on the example strings provided above will be:

0413100

and

1013180

and

0101122

and

1009155


Many thanks!

Wes

Title: Re: Find text, copy the text after, send email
Post by wesman on Mar 30th, 2017 at 9:37am
Gert, actually I have worked out a simpler solution.

Now all I need Watchdirectory to do is monitor files in a folder and search them for the text "ABCD" - if it finds ABCD text in a file then it needs to write the ABCD text along with the next 50 characters to another file.

How can I do this please?

Thanks

Wes

Title: Re: Find text, copy the text after, send email
Post by Gert on Mar 30th, 2017 at 11:05am
Hello Wes,

Do I understand you correctly....
The detected file can have
- zero lines with ABCD, in this case ignore it
- 1 or more lines with ABCD, in this case process the ABCD lines
- those files can also have other lines (non-ABCD) that can be ignored
Correct?

I assume those "abcd" files are computer generated?
Can you zip and post a few of them? Do not just make them yourself, I need to see and test with the exact format.

Bit busy at the moment but when I have everything from you I can probably have something to test for you by early next week.

Gert

Title: Re: Find text, copy the text after, send email
Post by wesman on Mar 30th, 2017 at 2:36pm
Hi Gert

To answer your questions:

The detected file can have
- zero lines with ABCD, in this case ignore it - CORRECT

- 1 or more lines with ABCD, in this case process the ABCD lines - the file may have more than one line (duplicates) with ABCD in it but I only want to process the first line detected

- those files can also have other lines (non-ABCD) that can be ignored - CORRECT

I have emailed you a sample file.

Thanks

Title: Re: Find text, copy the text after, send email
Post by Gert on Mar 31st, 2017 at 8:41am
FYI... I didn't receive any email with a file from you yet (also not in spam).

WatchDirectory Forum » Powered by YaBB 2.5.2!
YaBB Forum Software © 2000-2017. All Rights Reserved.