Hi Guys,

Another quick question which I cannot find the solution for.........

I need to load a directory into an array and then delete any file which has the name of an integer. for example 1.jpg, 500.jpg...these files will be deleted.

I already implemented a similar function elsewhere in my code which does a similar thing, this is my way of giving something back to the forum...

DeleteThis = "frame";

foreach (string file in dirs)
                        try
                        {
                            if (file.ToUpper().Contains(DeleteThis.ToUpper()))
                            {
                                File.Delete(file);
                            }

The above code will delete all files which contain the string "frame".

So how do I do this for anyfile name which is of an integer?


Thank you in advance, bruno.

I hope I understand correcty. You want to delete all files that have an integer as the file name without the extension?

If so I put this code together for you:

string[] fileList = Directory.GetFiles("C:\\Test", "*.jpg", SearchOption.TopDirectoryOnly);
string fileName;
int test;

foreach(string file in fileList)
{
    fileName = Path.GetFileNameWithoutExtension(file);

    if (int.TryParse(fileName, out test))
    {
        File.Delete(fileName);
    }
}

This code assumes a "using System.IO;" statement is included in your class.
Let me know if this doesn't do what you need.

Thanks,
Kenny

This question has already been answered. Start a new discussion instead.