Move and rename files with increasing sequence number

I am saving mail attachments, reading, sorting, moving and renaming the files into categories based on its contents.

For each file that has Condition X I move and save the file as filename condition X
My problem is that my loop gets an error when it tries to save the second file with condition X in the folder. Error msg says It cant create a new file, when the file already exists. (same name). So I want each file til be named Condition X 1, Condition X 2, Condition X 3 etc.

How do i add the (+1) to the destination filename?

Welcome to the forum

maybe you can give some more details on how you do loop over the files.

In case of you are working with a for each activity have a look on the Index output

this will provide the information on the current interation and you can use it with e.g. “YourfileName_” + IndexVar.toString

1 Like


How about the following?

fileName = "CondirionX"+(System.IO.Directory.GetFiles(yourPath,"ConditionX*").Count()+1).ToString()+".ext"


1 Like

For each file i read text file
IF text contains S9
IF text contains SU12
Move file to destination \ “S9_SU12 “+ToDaysDate+”.txt”

1 Like

That is working. Thank you!

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