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.
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…
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
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.
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.