DeleteLn or equivilant

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Dominic_Fichera
Pro Scripter
Posts: 82
Joined: Mon Mar 24, 2014 12:15 pm

DeleteLn or equivilant

Post by Dominic_Fichera » Thu Aug 14, 2014 5:49 am

Hey everyone,

Is there a code which will remove a line of text from a file the way that WriteLn adds text to a file?

I know I can create a file like this:

Code: Select all

WriteLn>C:\Users\%USER_NAME%\Desktop\test.txt,nWLNRes,Line 1
WriteLn>C:\Users\%USER_NAME%\Desktop\test.txt,nWLNRes,Line 2
WriteLn>C:\Users\%USER_NAME%\Desktop\test.txt,nWLNRes,Line 3
WriteLn>C:\Users\%USER_NAME%\Desktop\test.txt,nWLNRes,Line 4
WriteLn>C:\Users\%USER_NAME%\Desktop\test.txt,nWLNRes,Line 5
I can read a specific line like this:

Code: Select all

ReadLn>C:\Users\%USER_NAME%\Desktop\test.txt,2,varLine
MessageModal>varLine
How would I say "I want to remove line 2 and leave everything else"?

Thank you in advance,

Dominic Fichera

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

Re: DeleteLn or equivilant

Post by Marcus Tettmar » Thu Aug 14, 2014 7:10 am

You can't remove lines from text files. What you have to do is loop through, write all lines OTHER than the ones you want "deleted" to a new file. At the end you can delete the source file, and rename the new file to the source file. End result is what you want.

Potentially you could also read everything in with ReadFile in one go, then use RegEx or other string parsing to remove the line(s), then write back .. Either to a new file and then do the delete/rename, or delete source and then write to source.

Example pseudocode:

Code: Select all

ReadFile>sourcefile,text

//do string manipulation or loop to remove lines here 

DeleteFile>sourcefile
WriteLn>sourcefile,res,text



Sent from my iPad using Tapatalk
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
Sign up to our newsletter for free automation tips, tricks & discounts