Solved

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

Posted on 2012-04-05
14
594 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
[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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:fcsIT
ID: 37811637
plusone3055, that's the exact code I copied and am trying.
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

729 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