As part of a bigger process, I’m trying to read a CSV file (into a DataTable), select rows with a target string, then write out the selected rows to another file.
The obvious method is to:
Read the CSV file into a **DT**
In a ForEach loop (_for each_ **row** in **DT**)
Use _Get Row Item_ to get a **ColValue**
_If_ a row in which the **ColValue** matches a **MatchString**
Use _Add data row_ to a second DataTable, OutDT
Output OutDT to a CSV file.
When it gets to the first Add data row , I get the error:
Add data row : This row already belongs to another table.
So I put in an Assign: OutRow = row and use Add data row specifying OutRow, but get the same error message.
Then I tried another tack…
Read the CSV file into a **DT**
Copy the DT using _Assign_ **OutDt** to **DT**
In a ForEach loop (_for each_ **row** in **DT**)
Use _Get Row Item_ to get a **ColValue**
_If_ a row in which the **ColValue** does not match a **MatchString**
Use _Remove data row_ from **DT** (specifying the counter as the _RowIndex_) to delete unwanted rows from **DT**
Output OutDT to a CSV file.
This time the error is:
For each row : Collection was modified; enumeration operation might not execute.
This was not entirely unsurprising, so I copied DT, stepped through it and tried to remove unwanted rows from the new data table…
Read the CSV file into a **DT**
Copy the DT using _Assign_ **Dt** to **OutDT**
In a ForEach loop (_for each_ **row** in **DT**)
Use _Get Row Item_ to get a **ColValue**
_If_ a row in which the **ColValue** does not match a **MatchString**
Increment a counter with _Assign_
Use _Remove data row_ from **OutDT** (specifying the counter as the _RowIndex_) to delete unwanted rows from **OutDT**
Output OutDT to a CSV file.
I still get the same error, but can’t see why working on one table affects a second…
For each row : Collection was modified; enumeration operation might not execute.
I’d like to understand why neither of these approaches work. I’m thinking I’ll have to abandon what I’ve done and read the CSV file into Excel then manipulate the rows in there…
AJ