Jump to content
Macro Express Forums

Delete a line from text file


Recommended Posts

Hi All,

 

I'm sorry is this is covered somethere already.

 

I wrote a macro to select a random line from a text file. It works great (The code is really ugly but it works ;-)

 

Now, my problem is: I can't figure out how to delete that random line from the text file.

 

Can anyone help?

 

Here is my macro

 

<VARIABLE SET INTEGER Option="\x00" Destination="%N[1]%" Value="0"/>
<TEXT FILE BEGIN PROCESS Filename="C:\\Users\\Edward Aw\\Desktop\\TEST\\NoName01.txt" Start_Record="1" Process_All="TRUE" Records="0" Variable="%T[1]%"/>
<VARIABLE MODIFY INTEGER Option="\x07" Destination="%N[1]%"/>
<TEXT FILE END PROCESS/>
<VARIABLE SET INTEGER Option="\x05" Destination="%N[2]%" Minimum="0" Maximum="%N[1]%"/>
<TEXT FILE BEGIN PROCESS Filename="C:\\Users\\Edward Aw\\Desktop\\TEST\\NoName01.txt" Start_Record="%N[2]%" Process_All="FALSE" Records="1" Variable="%T[2]%"/>
<TEXT FILE END PROCESS/>
<TEXT TYPE Action="0" Text="%T[2]%<ENTER>"/>

 

I attached the macro below.

 

Thanks!

 

Ed

random line and delete.mxe

Link to comment
Share on other sites

Easy. This is how I would do it:

  1. Set a string variable to the file contents.
  2. Split the variable into an array using CRLF (End of line)
  3. Count the number of lines in the file. To do this I would get the length of the string then remove every occurrence of LineFeed from the var in step 1 and get the length again. The difference is the number of lines.
  4. Create a random number between 1 and the number of lines.
  5. Overwrite the old file by writing each element of the array from 1 to the number of lines one at a time. Use the option to append a CRLF. In this loop have a condition that it only write the line if the current counter is not the randomly generated integer.

Link to comment
Share on other sites

Hi Cory,

 

Thanks for the super fast reply!

 

Uh... you lost me at easy!

 

I actually found one of your scripts on the forum that deleted 5 lines before and 8 lines after. I spent 1.5 - 2 hours trying to just delete 1 random line and pulled out 25% of my hair! I'm not too smart.

 

Can you give me a little more guidance?

 

Thanks so much.

Link to comment
Share on other sites

I'll see if I can swing some time in the next day or two. But it's a busy time for me right now. You will see what I am suggesting is super simple and fast. It's probably the case where it's just functions you are not familiar with yet. But they are so simple Should only take me about 10 minutes.

Link to comment
Share on other sites

I'll see if I can swing some time in the next day or two. But it's a busy time for me right now. You will see what I am suggesting is super simple and fast. It's probably the case where it's just functions you are not familiar with yet. But they are so simple Should only take me about 10 minutes.

 

HI Cory,

 

Thanks so much.

 

I will look at it again. and try to figure it out.

 

Blessings,

 

Ed

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...
×
×
  • Create New...