|
-
February 23rd, 2006, 04:39 AM
#1
Compare file Names to string and delete files that don't match
Hi,
I need to compare file names(regardless of their extensions) with strings and delete those files where the names don't match. Any ideas?
-
February 23rd, 2006, 04:57 AM
#2
Re: Compare file Names to string and delete files that don't match
 Originally Posted by JakeyCakes
Hi,
I need to compare file names(regardless of their extensions) with strings and delete those files where the names don't match. Any ideas?
Here is an easy way of doing it. Use the System.IO namespace. Get all the files matching the specific pattern using GetFiles method of Directory class and then loop through the Values returned and delete the files.
Code:
string[] Files = System.IO.Directory.GetFiles(@"C:\","SearchPattern.*");
foreach (string fileName in Files)
System.IO.File.Delete(fileName);
-
February 23rd, 2006, 05:09 AM
#3
Re: Compare file Names to string and delete files that don't match
Could this be used more specifically e.g.
a) be case insensitive and
b)
keep files called
IB01
IB04
IB06
IB07
IB08
SA01
and delete files that do not have the above names?
-
February 23rd, 2006, 05:49 AM
#4
Re: Compare file Names to string and delete files that don't match
Well, to make a case insensitive search use the String.ToLower() or String.ToUpper() methods. But the filesystem is caseinsensitive by default, so you do not have to make anything by yourself.
Additional you can use the solution from Shuja Ali. You only have to invert to result. That you can easy by adding all filenames to an arraylist and after that you remove all filenames that was found by the searchpattern. All filenames remaining in the arraylist you can delete.
That's it, very simple.
Useful or not? Rate my posting. Thanks.
-
February 23rd, 2006, 06:13 AM
#5
Re: Compare file Names to string and delete files that don't match
-
February 23rd, 2006, 06:21 AM
#6
Re: Compare file Names to string and delete files that don't match
 Originally Posted by JakeyCakes
be case insensitive
As already said, the filesystem itself is case-insensitive and the GetFiles function is also case-insensitive.
 Originally Posted by JakeyCakes
keep files called
IB01
IB04
IB06
IB07
IB08
SA01
and delete files that do not have the above names?
You can do it by going through the array and checking if the filenames match. Somewhat similar to this
Code:
//get all the files in the directory
string[] Files = System.IO.Directory.GetFiles(@"C:\Temp1","*.*");
//array of filenames that are not to be deleted
string[] checkPattern = new string[5] {"IB01", "IB06", "IB07", "IB08", "SA01"};
bool fileMatch = false;
//loop through all the files
foreach (string fileName in Files)
{
fileMatch = false;
//check if the filename matches the files that are not to be deleted
foreach (string temp in checkPattern)
{
if (fileName.IndexOf(temp) > 0)
{
fileMatch = true;
break;
}
}
//if there was no match then delete the file
if (!fileMatch)
System.IO.File.Delete(fileName);}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|