Delete text till found value

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
stbede77
Newbie
Posts: 18
Joined: Wed Oct 25, 2006 7:04 pm

Delete text till found value

Post by stbede77 » Mon Dec 18, 2006 6:54 pm

I have a a file that will always be a little different so I would like to delete every line till it gets to the line that says "Macro Man"... any ideas?


Code: Select all

Data exam.


stuff...
stuff...stuff...stuff...
stuff...stuff...
stuff...
stuff...stuff...stuff...
Macro Man            <------------------
bla..bla..bla..
bla..bla..
bla..bla..
bla..bla..bla..
bla..bla..
bla..
bla..
bla..bla..bla..



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

Post by Marcus Tettmar » Mon Dec 18, 2006 7:28 pm

...

Code: Select all

Let>theFile=c:\somefile.txt
Let>workFile=c:\temp.txt

Let>DoWrite=0
Let>k=0
Label>FileLoop
 Let>k=k+1
 ReadLn>theFile,k,line
 If>line=##EOF##,Done
 If>line=Macro Man
    Let>DoWrite=1
 Else
    If>DoWrite=1
       WriteLn>workFile,res,line
    Endif
 Endif
Label>Done

If>DoWrite=1
  DeleteFile>theFile
  RenameFile>workFile,theFile
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

stbede77
Newbie
Posts: 18
Joined: Wed Oct 25, 2006 7:04 pm

Post by stbede77 » Mon Dec 18, 2006 7:49 pm

I have tried the code your provided but it doesnt work... it doesnt change one thing in the doc

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

Post by Marcus Tettmar » Mon Dec 18, 2006 7:53 pm

Doc? You mean a .doc file? You're not reading a text file then? Ok, my script only works on plain text files. You do of course also need to change the filenames at the top.

If you're working on .doc files then good luck. Depending on which version of Word you're using, the Microsoft .doc file is a proprietary binary file format, though some I think are just .rtf. So forget the approach suggested by me, instead you'll need to drive Word via VBScript. In fact you may be better off writing a Word VBA macro.

BTW it would help in future if you could provide more detailed requirements. For some reason I thought you wanted to read through a plain .txt file.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

stbede77
Newbie
Posts: 18
Joined: Wed Oct 25, 2006 7:04 pm

Post by stbede77 » Tue Jan 30, 2007 8:56 am

Sorry I ment .txt file... and still your code doesnt work... I used the following code so that I could locate the txt file


Input>filename,Text test file
Let>theFile=%filename%
Let>workFile=C:\Documents and Settings\Admin\Desktop\temp.txt

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

Post by JRL » Tue Jan 30, 2007 1:47 pm

The script above also assumes that you are looking for a line that contains nothing except "Macro Man". I've altered the script to make it find the text "Macro Man" anywhere in the text file line. I like to use the Separate> function to locate text. Separate returns a count in the variable (in this script) var_count. If var_count is greater than 1, then the text string "Macro Man" was found in the evaluated line of text.

Code: Select all

Let>theFile=c:\somefile.txt
Let>workFile=c:\temp.txt

Let>DoWrite=0
Let>k=0
Label>FileLoop
 Let>k=k+1
 ReadLn>theFile,k,line
 If>line=##EOF##,Done
 Separate>line,Macro Man,var
 If>var_count>1
    Let>DoWrite=1
 Else
    If>DoWrite=1
       WriteLn>workFile,res,line
    Endif
 Endif
Goto>FileLoop
Label>Done

If>DoWrite=1
  DeleteFile>theFile
  RenameFile>workFile,theFile
Endif

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

Post by pgriffin » Tue Jan 30, 2007 2:58 pm

My two cents worth:

ReadFile>TheFile,FileText
sep>FileText,Macro Man,Text
writeln>NewFile,r,Text_2

if you also want the phrase Macro Man in the file add this

let>Text_2=Macro Man %Text_2%

before you write the file

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

Post by JRL » Tue Jan 30, 2007 3:24 pm

Nicely streamlined.

One possible problem would be if the searched text, in this case "Macro Man" occurs more than once in the script. Separate> would then create multiple variables. This could be handled by checking the count and looping to make sure all of the script is written. but Marcus' script handles that situation and might be simpler.

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

Post by pgriffin » Tue Jan 30, 2007 3:53 pm

I didn't write for a case that wasn't presented. Very easy to add what to do if Macro Man occurred multiple times.

Jmac2501
Pro Scripter
Posts: 76
Joined: Tue Nov 15, 2005 8:11 pm

Post by Jmac2501 » Tue Feb 27, 2007 2:01 pm

mtettmar wrote:...

Code: Select all

Let>theFile=c:\somefile.txt
Let>workFile=c:\temp.txt

Let>DoWrite=0
Let>k=0
Label>FileLoop
 Let>k=k+1
 ReadLn>theFile,k,line
 If>line=##EOF##,Done
 If>line=Macro Man
    Let>DoWrite=1
[color=red]Else
    If>DoWrite=1
       WriteLn>workFile,res,line[/color]
    Endif
 Endif
Label>Done

If>DoWrite=1
  DeleteFile>theFile
  RenameFile>workFile,theFile
Endif
I have found a problem with this example in the Highlighted area. My problem was that it would send the value of the next line after the searched for value.

so to fix this i moved the else and endif like this

If>var_count>1
Let>DoWrite=1
else
endif
If>DoWrite=1
WriteLn>WorkFile,res,line
Else
endif
label>Done

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