Jump to content
Macro Express Forums

text files - scan each line for words and take action


randallf
 Share

Recommended Posts

Here is a macro I wrote thats basic function is to scan line by line through a text file searching for certain words and take action if it finds them. I post this because maybe someone will find this module useful.

 

I will post this in it's entire form; it is meant to search for the words 'break' or 'lunch' then find the times and create an outlook appointment for each. The initial copy is data from our workforce management program.

 

I think this might interest people because it works on a loop, rather than writing a macro that would just grab the times with whatever handy method, I used a loop so that I could create a module for scanning files and use it again later, and not just have one dumb breaks macro...

 

 

Clear All Variables: All
Clipboard Empty
Text Type: GRAB DATA INTO NOTEPAD
Clipboard Copy
Launch and Activate: "notepad.exe"
Clipboard Paste
Text Type: DATA MINER
Text Type: <ENTER><CTRLD><HOME><CTRLU>
Text Type: <SHIFTD><END><CTRLU>
Clipboard Copy
Variable Set String %T5% from Clipboard
Clipboard Empty
Repeat Until %T1% = ""
 Text Type: <HOME><SHIFTD><END><SHIFTU>
 Clipboard Copy
 Delay 0.1 Seconds
 Variable Set String %T1% from Clipboard
 If Variable %T1% contains "Break"
Text Type: <HOME><DELETE><DELETE><DELETE><DELETE><DELETE><DELETE>
Text Type: <CTRLD><SHIFTD><ARROW RIGHT><ARROW RIGHT><CTRLU><SHIFTU>
Clipboard Copy
Variable Set String %T2% from Clipboard
Text Type: <DELETE><DELETE>
Text Type: <CTRLD><SHIFTD><ARROW RIGHT><ARROW RIGHT><CTRLU><SHIFTU>
Clipboard Copy
Variable Set String %T3% from Clipboard
Activate Window: "Microsoft Outlook"
Delay 0.1 Seconds
Text Type: <ALTD>F<ALTU>wa
Text Type: BREAK
Text Type: FILL OUT APPOINTMENT
Text Type: <TAB><TAB><TAB>%T5%<TAB>%T2%<TAB>%T5%<TAB>%T3%
Delay 0.5 Seconds
Activate Window: "Notepad"
Text Type: <ARROW DOWN>
 End If
 If Variable %T1% contains "Lunch"
Text Type: <HOME><DELETE><DELETE><DELETE><DELETE><DELETE><DELETE>
Text Type: <CTRLD><SHIFTD><ARROW RIGHT><ARROW RIGHT><CTRLU><SHIFTU>
Clipboard Copy
Variable Set String %T2% from Clipboard
Text Type: <DELETE><DELETE>
Text Type: <CTRLD><SHIFTD><ARROW RIGHT><ARROW RIGHT><CTRLU><SHIFTU>
Clipboard Copy
Variable Set String %T3% from Clipboard
Activate or Launch: "Inbox - Microsoft Outlook" OR "OUTLOOK.EXE"
Text Type: <ALTD>F<ALTU>wa
Text Type: LUNCH
Text Type: FILL OUT APPOINTMENT
Text Type: <TAB><TAB><TAB>%T5%<TAB>%T2%<TAB>%T5%<TAB>%T3%
Delay 0.1 Seconds
Activate Window: "Untitled - Notepad"
Delay 0.1 Seconds
Text Type: <ARROW DOWN>
 End If
 If Variable %T1% does not contain "Lunch"
AND
 If Variable %T1% does not contain "Break"
Text Type: <ARROW DOWN>
Delay 0.1 Seconds
 End If
 Clipboard Empty
Repeat End
Text Type: <ALTD>f<ALTU>xn
Multiple Choice Menu: Breaks on Impact
If Variable %T10% = "A"
 Repeat Until %N1% = 1
If Window Title "Appointment" is running
  Activate Window: "Appointment"
  Text Type: <ALTD>f<ALTU>cy
Else
  Variable Set Integer %N1% to 1
  Delay 0.5 Seconds
End If
 Repeat End
End If
If Variable %T10% = "B"
 Repeat Until %N1% = 1
If Window Title "Appointment" is running
  Activate Window: "Appointment"
  Text Type: <ALTD>f<ALTU>cn
Else
  Variable Set Integer %N1% to 1
  Delay 0.5 Seconds
End If
 Repeat End
End If

 

<CLEARVAR1:A:ALL><CLIPE><DIS:<TEXTTYPE:GRAB DATA INTO NOTEPAD><CLIPC><LAUNCHNO3:0:0112Untitled - Notepad<LAUNCH:C:\WINDOWS\notepad.exe><CLIPP><DIS:<TEXTTYPE:DATA MINER><TEXTTYPE:<ENTER><CTRLD><HOME><CTRLU>><TEXTTYPE:<SHIFTD><END><CTRLU>><CLIPC><TVAR2:05:03:><CLIPE><REP3:08:000001:000001:0001:0:01:><TEXTTYPE:<HOME><SHIFTD><END><SHIFTU>><CLIPC><DELAY:.1><TVAR2:01:03:><IFVAR2:1:01:7:Break><TEXTTYPE:<HOME><DELETE><DELETE><DELETE><DELETE><DELETE><DELETE>><TEXTTYPE:<CTRLD><SHIFTD><ARROW RIGHT><ARROW RIGHT><CTRLU><SHIFTU>><CLIPC><TVAR2:02:03:><TEXTTYPE:<DELETE><DELETE>><TEXTTYPE:<CTRLD><SHIFTD><ARROW RIGHT><ARROW RIGHT><CTRLU><SHIFTU>><CLIPC><TVAR2:03:03:><ACTIVATE2:Microsoft Outlook><DELAY:.1><TEXTTYPE:<ALTD>F<ALTU>wa><TEXTTYPE:BREAK><DIS:<TEXTTYPE:FILL OUT APPOINTMENT><TEXTTYPE:<TAB><TAB><TAB>%T5%<TAB>%T2%<TAB>%T5%<TAB>%T3%><DELAY:.5><ACTIVATE2:Notepad><TEXTTYPE:<ARROW DOWN>><ENDIF><IFVAR2:1:01:7:Lunch><TEXTTYPE:<HOME><DELETE><DELETE><DELETE><DELETE><DELETE><DELETE>><TEXTTYPE:<CTRLD><SHIFTD><ARROW RIGHT><ARROW RIGHT><CTRLU><SHIFTU>><CLIPC><TVAR2:02:03:><TEXTTYPE:<DELETE><DELETE>><TEXTTYPE:<CTRLD><SHIFTD><ARROW RIGHT><ARROW RIGHT><CTRLU><SHIFTU>><CLIPC><TVAR2:03:03:><LAUNCHYES3:0:0112Inbox - Microsoft Outlook<LAUNCH:C:\Program Files\Microsoft Office\OFFICE11\OUTLOOK.EXE><TEXTTYPE:<ALTD>F<ALTU>wa><TEXTTYPE:LUNCH><DIS:<TEXTTYPE:FILL OUT APPOINTMENT><TEXTTYPE:<TAB><TAB><TAB>%T5%<TAB>%T2%<TAB>%T5%<TAB>%T3%><DELAY:.1><ACTIVATE2:Untitled - Notepad><DELAY:.1><TEXTTYPE:<ARROW DOWN>><ENDIF><IFVAR2:1:01:8:Lunch><AND><IFVAR2:1:01:8:Break><TEXTTYPE:<ARROW DOWN>><DELAY:.1><ENDIF><CLIPE><ENDREP><TEXTTYPE:<ALTD>f<ALTU>xn><MENU2:2:T:10:CenterCenter:Breaks on ImpactPlease choose an option or cancel:Save ALL open appointments
Cancel ALL currently open unsaved appointments
><IFVAR2:1:10:1:A><REP3:08:000001:000002:0001:0:01:1><IFOTH:04:2:Appointment><ACTIVATE2:Appointment><TEXTTYPE:<ALTD>f<ALTU>cy><ELSE><IVAR2:01:01:1><DELAY:.5><ENDIF><ENDREP><ENDIF><IFVAR2:1:10:1:B><REP3:08:000001:000002:0001:0:01:1><IFOTH:04:2:Appointment><ACTIVATE2:Appointment><TEXTTYPE:<ALTD>f<ALTU>cn><ELSE><IVAR2:01:01:1><DELAY:.5><ENDIF><ENDREP><ENDIF>

Link to comment
Share on other sites

Hi Rand,

 

You went to a lot of work to build this macro, and it looks pretty good. I know you didn't ask for any advice or help, so I hope my reply

doesn't offend; but I have some suggestions of things you might want to experiment with to help stream-line the process (and possibly

shorten the macro a bit).

 

This is the beginning of yours (note, I've removed the disabled Text Type commands to minimize confusion):

Clear All Variables: All
Clipboard Empty
Clipboard Copy
Launch and Activate: "notepad.exe"
Clipboard Paste

 

You might consider replacing this chunk with something along the lines of:

 

Clipboard Copy
Clipboard Save Text: "testfile.txt"

After which rather than using a Repeat Loop and systematically moving through line after line searching for the text you want, you could

use a Text File Process loop which will process each line of text in the file that we just created (in my example I used the Browse button

within the Clipboard Save Text command to browse to my desktop and then typed testfile.txt - the Save Text command will create that file

automatically).

 

The Text File Process loop will go through each line individually, automatically moving on to the next line on each successive loop without

having to delete the lines as it goes. What's more, it does this all "behind the scenes" as it were. No need to have a notepad on top, and it

is generally quite a bit faster than doing things up front.

 

Text File Process and ASCII File Process commands are very powerful (and fun to learn if you're inclined to sit and play with them).

 

I didn't look into any specific ways of tweaking the code within the Loop, because within a Text File Process, you'll basically do the same

thing, just without any of the Text Type commands that delete lines (dramatically shortening the macro's length as well as speeding the

macro up a bit). Also, since I don't actually need to do what this macro does, I can't really experiment with it and find specific areas of

improvement.

 

For the most part, I think you've built a very good macro that could be simplified by using more powerful commands that you may not

have been familiar with.

 

Oh, one last thing:

 

Delete File or Files: "testfile.txt"
Empty Recycle Bin

Adding this to the very end of the macro will delete the file we created for the Text File Process, and then empty the recycle bin so that if

you run this macro often, your recycle bin doesn't hold onto it (note, I would add these commands to the end, but keep them disabled until

you've finished all your debugging).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...