-2

I've managed to use the WinAPI SHGetKnownFolderPath() method to get the path to the Downloads folder, but when using the following code, I can't get it to delete specific types of files:

string rootFolderPath = KnownFolders.GetPath(KnownFolder.Downloads);
**string filesToDelete = @"*Agreement, CCRPCI, SECCI, Debit*.pdf";**
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToDelete);
foreach(string file in fileList)
{
    System.IO.File.Delete(file);
}

What do I need to add / change in order to reference multiple different file names? I know for a fact the bit I've highlighted in bold is incorrect.

EDITED -

Is there a way to delete files with different file names but the same extension type? As for the reason behind why I need to do this - My automation tests download various different files which get saved into the downloads folder. As the volume of automation tests is high these documents start to take up a lot of storage. Currently I'm going in and manually deleting them. I'm very new to coding guys and to this forum, I know it's a lot to ask but please have patience with me as I'm only trying to learn. (Already had two down votes)

CDS09
  • 17
  • 4
  • 2
    `Directory.GetFiles` can take a glob, but it can't take a list of globs. You'll want to create an array of globs, and loop through them. – canton7 Mar 04 '19 at 12:00
  • 4
    Consider using a debugger to inspect the execution of your code. First question to ask yourself is, what does the call to `GetFiles` return? – David Heffernan Mar 04 '19 at 12:01
  • Consider using `SHFileOperation()` or `IFileOperation` instead. The first can delete multiple files using wildcards (but, like `GetFiles()`, only one pattern can be used at at time), the second can delete multiple files using an array/enum of `IShellItem` interfaces – Remy Lebeau Mar 04 '19 at 15:31
  • Randomly deleting Debit*.pdf does not sound like a good idea. – Anders Mar 04 '19 at 15:50
  • Thank you for all your replies - I've edited my question a bit, hopefully this might give you a better insight into what I'm trying to accomplish. – CDS09 Mar 04 '19 at 17:24
  • 1
    @CDS09 your EDIT was already answered before you even made it. You can use wildcards with `GetFiles()` and `SHFileOperation()`, just not the way you were originally trying to use them. In any case, do you have control to download all of the automation files into a subfolder underneath the Downloads folder? If so, that would make the coding much easier if you can just delete `*.*` or even just delete the entire subfolder in one go – Remy Lebeau Mar 04 '19 at 17:43
  • 1
    Can you please tell me what happened with debugging your code? – David Heffernan Mar 04 '19 at 20:36

1 Answers1

0

Is there a way to delete files with different file names but the same extension type?

You can use the shell commands, del DownloadsPath\\*.xxx and run it in your program. And here is the method to run shell commands in C#.

Or run a bat file directly.

Drake Wu
  • 6,927
  • 1
  • 7
  • 30