Solved

File.Exists - Don't know name

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

740 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