Solved

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

Posted on 2012-04-05
14
580 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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
 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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