Reputation: 931
I had posted something similar to this before, but it was about dealing with the Command Prompt. As in the other instance, I'm trying to do some automated file cleanup prior to a backup in an ERP system I perform maintenance on in order to smooth out the process (as I perform maintenance on at half dozen of these systems at least twice each month). So, here are some examples of what's happening...
Here are three files names that could show up in the directory:
Of these three, the second two would be candidates for deletion, and the first would need to remain. So, initially I used the following to retrieve only the second two:
String[] wrkFileList = Directory.GetFiles(directoryPath, "??_*Wrk??*????????????.M4T");
However, for some reason, it always returns all three even though the first doesn't match the pattern. When using this pattern in Windows Explorer, it only returns the second two files, as is desired. I have developed a workaround using regular expressions, which works:
Regex wrkFileMatch = new Regex("([A-z]{2}_[A-z0-9]+Wrk[A-Z0-9]{2,3}\\d{12}.(m4t|M4T))$");
I'm not crazy about this approach, though, because it adds a loop which shouldn't be necessary because I have to loop through all results to get the correct results. Performance-wise, it doesn't seem to matter that much, but I'd like to understand why the initial pattern match fails to only return the correct matches. Is there a better method for file name filtering with GetFiles, or am I just better off with iterating through the directory results and using RegEx matches to find the correct files (like I am currently doing)?
Upvotes: 0
Views: 155
Reputation: 3117
Your initial attempt using Directory.GetFiles(...) fails because the '?' wildcard allows for either 0 or 1 characters in the indicated position. To do what you want, you will essentially have to use a regex.
Side note, you can simplify your regex down to "\w{2}_\w+Wrk\w{2,3}\d{12}.([mM]4[tT])"
Upvotes: 1
Reputation: 116805
From the documentation for Directory.GetFiles Method (String, String)
searchPattern can be a combination of literal and wildcard characters, but doesn't support regular expressions. The following wildcard specifiers are permitted in searchPattern.
*
(asterisk): Zero or more characters in that position.?
(question mark): Zero or one character in that position.
Given that, AP_AnalysisWrk.M4T
does match ??_*Wrk??*????????????.M4T
, because all those ??*????????????
characters at the end each can match the empty string.
So, you can use Directory.GetFiles()
to do a crude initial match, then filter the returns more precisely with a Regex.
Upvotes: 1