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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

770 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