Posted Tue, 29 Sep 2020 18:43:36 GMT by

Hi All,

I would like to know how can I delete rows in a txt file.

my problem is:

i have a txt file (with just 1 column, separated by "New line")

I want to read the first row and then delete it, and so on, all in the same txt file


i.e.

(Myfile.txt) before the task

A000000001

A000000002

A000000003

A000000004

A000000005

A1111111111

(Myfile.txt) after the task

A000000004

A000000005

A1111111111


it not matter what row to delete, I just want to delete them sequentially

Automate Version: 11.3.10.65

Windows Version: Windows 7 Enterprise x64

 

Thank you in advance

 

Gabriele

 

Posted Thu, 01 Oct 2020 23:04:19 GMT by

Hello Gabriele,

For your request, we would need to know the number of lines that you would like to delete. Based on  your example, I hard coded the number of 3 to eliminate from the list. 

However, AutoMate will gather the data internally, manipulate it, then write back to the file. We suggest creating a temp file to write the newly edited information, then deleting the original file and renaming the temp file, or overwriting the original file altogether. Here is an example of how to accomplish your goals up to the file replacement. Please place your example file onto your desktop:

<AMVARIABLE NAME="varNew" />
<AMVARIABLE NAME="varCounter" VALUE="0" />
<AMVARIABLE NAME="varItem" />
<AMVARIABLE NAME="varRead" />
<AMFILESYSTEM ACTIVITY="read_file" FILE="%GetDesktopDirectory%\Myfile.txt" RESULTVARIABLE="varRead" />
<AMLOOP ACTIVITY="list" LIST="%varRead%" DELIMITER="new_line" RESULTVARIABLE="varItem" />
<AMIF EXPRESSION="%varCounter% = 3" />
<AMFILESYSTEM ACTIVITY="write_file" FILE="%GetDesktopDirectory%\Myfiletemp.txt">%varItem%</AMFILESYSTEM>
<AMLABEL ACTIVITY="goto" LABELNAME="write" />
<AMIF ACTIVITY="end" />
<AMTEXT ACTIVITY="replace" TEXT="%varItem%" FIND="%varItem%" REPLACE="" RESULTVARIABLE="varNew" />
<AMVARIABLE ACTIVITY="increment" RESULTVARIABLE="varCounter" />
<AMLABEL LABELNAME="write" />
<AMLOOP ACTIVITY="end" />

 

As you can see, I used a counter to let AutoMate know how many times I deleted a row. Once that number gets to 3, then it begins writing the rest to the temp file. I used a label to skip over the deletion of the unnecessary lines once the counter is at 3.

Posted Fri, 02 Oct 2020 23:22:10 GMT by

Hi Alex,

I am pretty sure you have solved my problem.

 

Many thanks for your help

 

Gabriele

You must be signed in to post in this forum.