Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-04-05
14
Medium Priority
?
606 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

705 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