Solved

How to do wildcards (*.txt) in C#.net

Posted on 2012-04-05
14
575 Views
Last Modified: 2012-04-05
Can anyone tell me how to use a wildcard to select all txt files in a given path?  I'm using the code shown below.  The first line, commented out, fails saying the asterik is an invalid character.  The second line appears to be fine, but causes all the rest of my code to appear invalid.  (See the screenshot for what I'm talking about.)

The code works perfectly when I use the commented out version and specify a filename, but I need a version that I can tell to grab all text files in a given directory.

 
static void Main(string[] args)
        {
            //var filePath = @"F:\groups\billing\837Pupload\*.txt";

            var filePath = Directory.GetFiles(@"F:\groups\billing\837Pupload\", "*.txt");


            File.WriteAllText(
              filePath,
              Regex.Replace(File.ReadAllText(filePath), @"~", "~\n", RegexOptions.Multiline)
              );

            File.WriteAllText(
              filePath,
              Regex.Replace(File.ReadAllText(filePath), @"HL\*(1\d+|[2-9]\d*)\*\*20\*1~(\s+.+~){6}\r?\n?", "", RegexOptions.Multiline)
              );
        }

Open in new window

code.jpg
0
Comment
Question by:fcsIT
  • 7
  • 5
  • 2
14 Comments
 
LVL 14

Expert Comment

by:athomsfere
ID: 37811598
What is the exact error message you are getting?

I know I can use "*" as a wild card in directory searcher...

else if (userName.Contains(" "))
                {
                    string[] names = userName.Split(' ');
                    foreach (string N in names)

                    names[0] += "*";
                    names[1] += "*";

                    userName = names[0] + " " + names[1];

                    searchAtt = "CN";
                }

Open in new window


Maybe your issue is the class does not support it?
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 37811624
http://www.csharp-examples.net/get-files-from-directory/


string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.txt");
0
 

Author Comment

by:fcsIT
ID: 37811628
Please see the attached screenshot for build errors.
build-errors.jpg
0
 

Author Comment

by:fcsIT
ID: 37811637
plusone3055, that's the exact code I copied and am trying.
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 37811649
0
 

Author Comment

by:fcsIT
ID: 37811654
plusone3055, I copied that code from that site yesterday, I already have that url, but it's failing.
0
 
LVL 14

Expert Comment

by:athomsfere
ID: 37811724
Does it run if you declare var as var[] instead, or even better as string[]

var[] filePath = Directory.GetFiles(@"F:\groups\billing\837Pupload\", "*.txt");

Open in new window

0
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.

 

Author Comment

by:fcsIT
ID: 37811733
No.  I was hoping for the same easy trick.

I get the same build errors when using this:

string[] filePath = Directory.GetFiles(@"F:\groups\billing\837Pupload\", "*.txt");
0
 

Author Comment

by:fcsIT
ID: 37811742
It's complaining about the parameters (filepath) in each error, which is of course the code that I copied from the csharp-examples site.
0
 
LVL 14

Expert Comment

by:athomsfere
ID: 37811786
Are you trying to write all of the files to another text document?
0
 

Author Comment

by:fcsIT
ID: 37811792
No.  I'm scanning each file for a certain character string, then if found, removing it and the next six lines.  The files are saved over themselves, not written elsewhere.
0
 
LVL 14

Accepted Solution

by:
athomsfere earned 500 total points
ID: 37811902
OK, maybe try doing something like this?

static void Main(string[] args)
        {

            string[] filePath = Directory.GetFiles(@"F:\groups\billing\837Pupload\", "*.txt");

            foreach (string file in filePath)
            {
            File.WriteAllText(
              file,
              Regex.Replace(File.ReadAllText(file), @"~", "~\n", RegexOptions.Multiline)
              );

            File.WriteAllText(
              file,
              Regex.Replace(File.ReadAllText(file), @"HL\*(1\d+|[2-9]\d*)\*\*20\*1~(\s+.+~){6}\r?\n?", "", RegexOptions.Multiline)
              );
            }
        }

Open in new window

0
 

Author Comment

by:fcsIT
ID: 37811924
You ROCK!!  That worked perfectly!  So I needed to bring apart file and filepath??  I would have never guessed that!

Awesome job, thanks so much!!!!
0
 
LVL 14

Expert Comment

by:athomsfere
ID: 37812096
Awesome!

Essentially the problem was that you were telling your regex to look at what should have been an array.

So once you broke it down and basically told it it to:

give me the files in directoryX as an Array

For each file found, run the Regex

Open in new window

it then knew how to handle the files (Not as one giant collection all at once)

That's also why you were getting the error message about cannot convert string[] to string, C# saw that you were trying to run a method against a file (they way you called the method), but what you actually had stored was potentially dozens or hundreds of files.
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

Suggested Solutions

Title # Comments Views Activity
Hangfire / asp.net sample 3 50
Server error in 'XXXXXXXX' application. 12 69
ASP.net VB.net Email  a webpage 3 30
Error on Add method 1 37
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

912 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

21 Experts available now in Live!

Get 1:1 Help Now