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
46 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 49

Expert Comment

by:Rgonzo1971
ID: 41790721
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 75

Accepted Solution

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

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

Open in new window

0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 41791254
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

16 Experts available now in Live!

Get 1:1 Help Now