Error using delete file with 'contains' condition


I have a root folder inside which there are multiple folders with multiple files in it.
All sub folders will have multiple files but only 1 file which will have ‘Consolidated’ term in filename.
So, I wanted to delete all the files except the ones having consolidated in it.

I used Directory.GetFiles(Path,"*", searchoption.AllDirectories)

Then to check term i used the contains method inside for loop
**If (NOT file.ToString.ToLower.Contains(“consolidated”) , then delete.

Straight enough, it works perfectly, it deletes all files except consolidated ones, but shockingly sometimes it deletes all the files even the ones’ containing consolidated.

My file names remain same so it’s not that file names are changing so it deleted all, I tried several ways to replicate and figure out the issue but couldn’t find, it’s adhoc, sometimes, it works perfectly whereas at times it deletes all the files, ultimately I had to change the pattern in directory.get files method to match the files which are supposed to be deleted, this works fine.

Has any one ever faced this issue while using contains with 'Delete; file activity

hi @Faraz_Subhani

Try this and see what you see in write line
Writeline(file.tostring) +" Case True"
Writeline(file.tostring) +"Case False"

Hi Lakshay,
Thanks for you response, I had already tried this, to make sure if there is some error , however I tried again to cross verify and I as expected I got all the file names which have consolidated in it. As I had mentioned that it works perfectly sometimes and fails at times.
I understand my query seems absurd and does not make much sense, so only I came here to check if any one else has faced the same issue while using contains with delete file.

@Faraz_Subhani Never seen this happening.