I am using SharpDevelop to write a C# program (not console). I want to delete files within a specified directory, but be able to EXCLUDE files beginning, ending, or containing certain words.
TO completely delete ALL files in a folder I am using this :
private void clearFolder(string FolderName)
{
DirectoryInfo dir = new DirectoryInfo(FolderName);
foreach(FileInfo fi in dir.GetFiles())
{
fi.Delete();
}
foreach (DirectoryInfo di in dir.GetDirectories())
{
clearFolder(di.FullName);
di.Delete();
}
}
I use it like
ClearFolder("NameOfFolderIWantToEmpty");
Is there a way to modify this so that I can delete all files and direcotries EXCEPT those files and directories containing specific words?
Something like :
CleanFolder(FolderToEmpty,ExcludeAllFileAndDirectoriesContaingThisPhrase);
so that if I did
CleanFolder("MyTestFolder","_blink");
It would NOT delete files and directories with names like
_blinkOne (file)
Test_blineGreen (file)
Test_blink5 (directory)
_blinkTwo (file within the Text_blink5 directory)
Folder_blink (empty directory)
but WOULD delete files and directories like
test (file)
test2 (directory)
test3_file (file within test2 directory)
test4 (empty directory)
I suspect I might have to iterate through each file and directory, checking them one at a time for the matching filter and deleting it if it does not match, but I am not sure how to do that.
Something with FileInfo()
and DirectoryInfo()
perhaps?
Can somebody help by providing a working example? (modified version of the above is preferred, but if a new method is required, as long as it doesn't require an outside dll, is OK.