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)
                            if (file.ToUpper().Contains(DeleteThis.ToUpper()))

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))

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 that works great!


Excellent! Glad to hear it worked out :)


Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.