How to use Clear Collection on an Array

I am trying to clear a collection of files from an array. It returns the following error:
Remove from collection : Collection was of a fixed size.

I have set up an assign activity with the following:
assign = AttachPDFFiles = Directory.GetFiles(folderPath, “*.pdf”)

Then I am emailing these files. The problem I am running into is that it stores all the files in the array, so every time I run the job it keeps adding more files.

I have tried searching for help in the community forum and on google and cannot find anything that works.

Hi.

Yep, an array is fixed. But, there are ways to manipulate your array, like setting it to a new array with different items, or filtering it down.

The issue that I feel you have is that you email the files out, then more files end up in folderPath, therefore, it adds those files to the pool of files in folderPath. What you would want to do probably is move the emailed files out to a different folder, so when you use .GetFiles() it won’t see them anymore in folderPath.

Alternatively, you can take all the files in folderPath and filter them by filename or date.
an example of this would look like: Directory.GetFiles(folderPath, “*.pdf”).Where(Function(f) Directory.GetCreationTime(f) > Today).ToArray

Hopefully, I understood you correctly.

Regards.

Thank you for your quick response.
I misspoke in my last post, the error I am receiving is: Clear Collection: Collection is read-only

To give more detail, I am removing all of the files from the folder each time after the job is run, which is why I am confused that when the email goes out, it still has the old files.

I will attempt the filter by date you suggest. The issue I see with this is I am running this process multiple times a day.

I will suggest you to use move file activity after sending each email like

String filename = "<U+202A>C:\Temp\New Text Document.txt"

source: fileName

Destination: Path.Combine(Path.GetDirectoryName(filename),path.GetFileNameWithoutExtension(filename) & "_sent" & path.GetExtension(filename))

and when next you loop on files just use if condition like below
if(Not filename.contains("_sent")){do email sending part}

If you are sending it as an attachment you will have to dispose of the object before moving or deleting. The easiest way to do this is to run the garbage collector using invoke method. This has to be done after sending the email and before moving or deleting the file

@workwithit to clear array you can use assign activity like below
FileArray = {}

1 Like

Thank you for your help! I was able to figure it out.

Assign=> AttachPDFFiles = {“”}

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.