Solved

File.Exists - Don't know name

Posted on 2006-06-13
2
246 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

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

11 Experts available now in Live!

Get 1:1 Help Now