Solved

File.Exists - Don't know name

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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 …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

810 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