Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
76 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 52

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 2000 total points
ID: 41791252
Or simply LINQ:

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

Open in new window

0
 
LVL 52

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 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