read text file

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
klm67097
Newbie
Posts: 5
Joined: Mon May 15, 2006 6:11 pm

read text file

Post by klm67097 » Mon May 15, 2006 6:18 pm

hi

I have some problem reading a txt file

I don't now how to do that.
it is very simple

it should read the contents of a text file and where the words MISREF is mentioned it should send me a email (this MISREF contains always with have a number for example MISREF132525)
it is also possible that this MISREF is mentioned twice or more so there should be a loop in it.

is this possible to do via macro scheduler?

please advise
Kisoen

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Tue May 16, 2006 4:36 am

Try something like this:

let>Row=0
Label>READFILE
readln>c:\myfile.txt,%Row%,TextLine
If>TextLine=##EOF##
goto>ProgramEnd
endif>
rem> now check for you MISREF string
Pos>MISREF,%TextLine%,1,FoundIt
if>FoundIt>0
SMTPSendMail>look in the help files for the settings for SMTPSendMail
endif>
goto>READFILE
Label>ProgramEnd

Let us know if you don't understand any of this code and we will help you.

klm67097
Newbie
Posts: 5
Joined: Mon May 15, 2006 6:11 pm

read text file

Post by klm67097 » Wed May 17, 2006 5:17 am

hi
when testing retrieving an error

can find label programend...

please advise
kisoen

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed May 17, 2006 5:25 am

Labels are case sensitive so be sure you have exactly the same characters in the Goto> statement and the Label> statement.

Also, (and more likely) make sure you have no trailing spaces in the script especially in the label> and goto> statements.

Hope this helps,
Dick

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Wed May 17, 2006 1:19 pm

JRL probably is correct about the trailing spaces. When you cut/paste code from this forum you will need to "Remove Trailing Spaces" from the Edit menu.

sorry if I caused any confusion.

klm67097
Newbie
Posts: 5
Joined: Mon May 15, 2006 6:11 pm

read text file

Post by klm67097 » Thu May 18, 2006 8:23 am

this is my coding
GetDate>date
Let>CF_OVERWRITE=1
Let>msg=The Date Is :
ConCat>msg,date
year>YY
month>MM
day>dd
rem Message>%mm%%dd%

IfFileExists>n:\Receive\Logs\EDI_import_06%mm%%dd%.txt,next
goto>end
label>next
CopyFile>n:\Receive\Logs\EDI_import_06%mm%%dd%.txt,c:\temp\EDI_import_06%mm%%dd%.txt

let>Row=0
Label>READFILE
readln>c:\temp\EDI_import_06%mm%%dd%.txt,%Row%,TextLine
If>TextLine=##EOF##
goto>ProgramEnd
endif>
rem> now check for you MESREF string
Pos>MESREF,%TextLine%,1,FoundIt
if>FoundIt>0
goto>READFILE
Label>ProgramEnd

Let>SENDMAIL_STATUS=1
Let>subject=error edi message
Let>me=[email protected]
Let>myname=Mr Smith
Let>recipients=[email protected];[email protected]
rem Input>body,Enter your message:
SMTPSendMail>recipients,mailgate.klm.nl,me,myname,subject,This email contains EDI error APPZ Trips,;c:\temp\EDI_import_06%mm%%dd%.txt
rem Message>Result of SendMail: %SMTP_RESULT%
endif>


as far i can see and receive is the attachment via email
I want that the message MESREF with a number will be in the email

thx for the hint for removing trailer spaces (it works)

but... how can i only receive the mesref message?

the textfile look like this
[Import operation] started on 18/05/06 05:13:51
Process started
Loading EDI messages from file i.060518050028
[CARDIT 1] MesRef=MESREF37346, Component=Cardit1_1ReceptacleInfo, Item=SESTOCEGCAIBAEN60009001100016, Error=Invalid (Receptacle-handling-class)
[CARDIT 1] MesRef=MESREF37346, Component=Cardit1_1Info, Item=SE6015118658, Error=Invalid
*Err* The message body could not be read
Process stopped


as you can see the MesRef=MESREF37346 this is what i want via email

please advise
kisoen

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu May 18, 2006 11:19 am

Hi,

This reads the text file line by line and if it finds MesRef=MESREFnnnnnn, it will extract the MESREFnnnnnn bit. It works by looking for MesRef= and if it finds it extracts from just after MesRef= to the next comma.

Change the first line of your script to reflect the path to your input file and run the script as is and it will pop up a message for each MESREF. All you need to do is replace that with an STMPSendMail command.

Let>comma=,
Let>input=c:\temp\input.txt

Let>k=1
Label>readloop
ReadLn>input,k,line
If>line=##EOF##,doneread

Length>line,ln
Position>MesRef=,line,1,p
If>p>0
Let>p=p+7
MidStr>line,p,ln,line
Position>comma,line,1,p
Let>p=p-1
MidStr>line,1,p,line
MessageModal>line
//SMTPSendMail>.....
Endif

Let>k=k+1
Goto>readloop
Label>doneread
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

klm67097
Newbie
Posts: 5
Joined: Mon May 15, 2006 6:11 pm

read text file

Post by klm67097 » Thu May 18, 2006 11:44 am

hi
I get the following error
on line If>p>0

please advise what to do or to change

error - subroutine/label p>0 not found

I hope this will help me fixing the code

gr. kisoen

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu May 18, 2006 11:49 am

Which version of Macro Scheduler are you using? Please check under Help/About.

You must have an earlier version. In which case try this:

Let>comma=,
Let>input=c:\temp\input.txt

Let>k=1
Label>readloop
ReadLn>input,k,line
If>line=##EOF##,doneread

Length>line,ln
Position>MesRef=,line,1,p
If>p=0,noextract
Let>p=p+7
MidStr>line,p,ln,line
Position>comma,line,1,p
Let>p=p-1
MidStr>line,1,p,line
MessageModal>line
//SMTPSendMail>.....
Label>noextract

Let>k=k+1
Goto>readloop
Label>doneread
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

klm67097
Newbie
Posts: 5
Joined: Mon May 15, 2006 6:11 pm

read text file

Post by klm67097 » Thu May 18, 2006 3:10 pm

hi,

sometime script is working perfectly
en sometimes still getting error in position command
can this be fixed??

also have so difficulties with the loop
it seems like the macro is hanging.....

please advise
kisoen

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu May 18, 2006 3:13 pm

Try this

Code: Select all

Let>comma=,
Let>input=c:\temp\input.txt

Let>k=1
Label>readloop
  ReadLn>input,k,line
  If>line=##EOF##,doneread
  
  Length>line,ln
  If>ln<7,noextract
  Position>MesRef=,line,1,p
  If>p=0,noextract
  	 Let>p=p+7
         MidStr>line,p,ln,line
	 Position>comma,line,1,p
	 Let>p=p-1
	 MidStr>line,1,p,line
	 MessageModal>line
	 //SMTPSendMail>.....
  Label>noextract
  
  Let>k=k+1
Goto>readloop
Label>doneread
BTW - We could do ALL your work for you but at some point it might be a good idea if you spent a bit of time understanding exactly what the above code does and try stepping through with the debugger to see exactly what causes the errors you refer to and why they happen. It will save us all a lot of time.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts