Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

File.Exists - Don't know name

Posted on 2006-06-13
2
Medium Priority
?
259 Views
Last Modified: 2010-04-16
Is it possible to copy a file that matches a certain NAME pattern - not extension!

for example, lets say I wanted to copy all files from c:\temp whose name starts with "FOO"

So here is my c:\temp directory:
FOO.txt
FOOBAR.pdf
FORGET_IT.doc


So in this case, I would need to get back: FOO.txt and FOOBAR.pdf

How can I do this?

thanks!!
0
Comment
Question by:cdfllc
  • 2
2 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 1000 total points
ID: 16896174
Hi, you can have something like the following

using System.IO;
...

      string[] files = Directory.GetFiles(@"c:\temp", "FOO*");

      foreach (string filePath in files)
      {
            File.Copy(...); // use filePath here
      }

GetFiles() returns a string array of filepaths that match the pattern (specified in second parameter)
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16896478
Alternatively, you can also instantiate a FileInfo object based on a filepath and do some tests on its useful properties to get the files you want:

      string[] files = Directory.GetFiles(@"c:\temp");
      FileInfo fi;

      foreach (string filePath in files)
      {
            fi = new FileInfo(filePath);
      
            if (fi.Name.StartsWith("FOO") && fi.Name.EndsWith("txt"))
            {
                  File.Copy(fi.FullName, "<destinarion path here>");
            }
      }

      fi = null;
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

876 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