sgtaw Posted December 2, 2011 Report Share Posted December 2, 2011 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 Quote Link to comment Share on other sites More sharing options...
Cory Posted December 2, 2011 Report Share Posted December 2, 2011 Easy. This is how I would do it: Set a string variable to the file contents. Split the variable into an array using CRLF (End of line) 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. Create a random number between 1 and the number of lines. 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. Quote Link to comment Share on other sites More sharing options...
sgtaw Posted December 2, 2011 Author Report Share Posted December 2, 2011 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. Quote Link to comment Share on other sites More sharing options...
sgtaw Posted December 5, 2011 Author Report Share Posted December 5, 2011 Any help? Please? Thanks, Ed Quote Link to comment Share on other sites More sharing options...
Cory Posted December 5, 2011 Report Share Posted December 5, 2011 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. Quote Link to comment Share on other sites More sharing options...
sgtaw Posted December 5, 2011 Author Report Share Posted December 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.