Solved

File.Exists - Don't know name

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

23 Experts available now in Live!

Get 1:1 Help Now