Solved

What statement would you code to delete all files in the following directory that have a file extension of *.TIF ?

Posted on 2016-09-08
3
39 Views
Last Modified: 2016-09-09
I am writing a C# application using VS2010 and .Net Framework 4.0.

What statement would you code to delete all files in the following directory that have a file extension of *.TIF ?

I use the following value for the directory path:

const string TARGET_DIR_IN = @"\\v\region\na\control\dev\W\data\Test1\";
0
Comment
Question by:zimmer9
  • 2
3 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
Comment Utility
Hi,
pls try

DirectoryInfo di = new DirectoryInfoTARGET_DIR_IN);
FileInfo[] files = di.GetFiles("*.TIF")
                     .Where(p => p.Extension == ".TIF").ToArray();
foreach (FileInfo file in files)
    try
    {
        file.Attributes = FileAttributes.Normal;
        File.Delete(file.FullName);
    }
    catch { }

Open in new window

Regards
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Or simply LINQ:

Directory.EnumerateFiles(TARGET_DIR_IN, "*.TIF")
         .ToList()
         .ForEach(File.Delete);

Open in new window

0
 
LVL 48

Expert Comment

by:Rgonzo1971
Comment Utility
Correct code
DirectoryInfo di = new DirectoryInfo(TARGET_DIR_IN);
FileInfo[] files = di.GetFiles("*.TIF")
                     .Where(p => p.Extension == ".TIF").ToArray();
foreach (FileInfo file in files)
    try
    {
        file.Attributes = FileAttributes.Normal;
        File.Delete(file.FullName);
    }
    catch { }

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now