Solved

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

Posted on 2012-04-05
14
571 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

17 Experts available now in Live!

Get 1:1 Help Now