Why am I getting "Could not find file" error?

I am trying to delete all files in a directory, except pdf files, but I am getting this error “Could not find file ‘XXXXXXX’” and it does not make sense, because it does exist!

I have a folder from which I want to delete all files and all folders in it unless it is a .pdf file. I am retrieving all file/folder paths, and using Delete activity, but it is throwing an exception and it doesn’t make sense to me.
Below is my workflow.

@Create variable di of type System.IO.DirectoryInfo
@Assign: di = new DirectoryInfo(“folder path come here”)
@For Each file (of type FileInfo) in di.GetFiles {
… … If (Not file.Name.Contains(“.pdf”) Then {
… … … … Delete: property → Path: file.Name
… … }
… }

Why does this return the exception such as
Delete: Could not find file “C:\XXXX\XXXX\XXXX\XXXX.txt”
etc?

The exception says any non-PDF files or folders don’t exist, and hence they cannot delete it. but it is there! I mean, it is there, that is why it is collecting all paths… but I cannot delete it. Doesn’t make sense.

@tomato25

Could you try once again. I guess it is trying to delete the same file again. Could you please take screenshot of your process flow once and show me.

Hi @tomato25

Use assign activity

String strarray= ```
Path.GetFileName( Path.GetDirectoryName( path ) );

use for each item in str array 

Check if condition (item.toString.Contains("filename.pdf")
use delete file


Thanks
Ashwin S

In the first Assign activity, I am giving it a correct path to the folder.

and no, it is not trying to delete the same file or folder again, because those files and folders never get deleted and I still see them in the folder…

hi @tomato25

before that use path exists activity and pass the file where the file exists or not

then use delete file

Thanks
Ashwin S

@tomato25

Just specify path as file instead of file. Name in Delete activity and then try again.

1 Like

Just to rule this out, add a count to your fileinfo array, then get the same array but use the .Distinct() method to get an ienumerable of only distinct values. Then do a count and make sure the counts are the same to ensure there aren’t any duplicates in your array

1 Like

This is a bit too much work because I am retrieving all file paths using di.GetFiles, so it doesn’t really make sense to check if the path exists…kind of redundant.

I gave it a try. Sure it removed the exception, but it is not deleting non-pdf files.

@tomato25

Have you tried my solution as mentioned in the above my post ?

1 Like

Since file is of type System.IO.FileInfo, I get a validation error:

Compiler error(s) encountered processing expression “file”.
Value of type “System.IO.FileInfo” cannot be converted to “String”.

@tomato25

In the assign activity, try below expressions and pass result to for each loop to iterate one by one file.

Directory.GetFiles(“FolderPath”)

1 Like

Assign: arrDistinct (of Type IEnumerable) = di.GetFiles.Distinct
Message Box: "arrDistinct.Count: " + arrDistinct.Count.ToString + System.Environment.NewLine + "di.GetFiles.Count: " + di.GetFiles.Count.ToString

The result:
image

And here is the folder content:
image

I need to remove everything but sampleX.pdf files.

I used your way, and it worked and it removed the files.
However, I am still wondering what was wrong with my original plan of using DirectoryInfo and FileInfo, and I don’t get why I was getting the exception.

Also, now my next question is, how can I remove zip folder? Sometimes. this folder gets zip folder stored, but I have to remove zip folder too.

1 Like

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