locked
find string with certain extension and name RRS feed

  • Question

  • Good day all

    I would like to find a certain part in a files name where there is a "@" involved,

    after the "@"there is a certain name and the file has a certain extension.

    Any help is appreciated!!

    Tuesday, October 30, 2012 8:43 AM

Answers

  • I believe you can try something like this

                Directory.GetFiles(myPath, "*.txt")//define extension here or *.* for all
                    .Select(Path.GetFileNameWithoutExtension) //for example get filename without extension
                    .Select(p => p.Contains("@")); 


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    Tuesday, October 30, 2012 10:56 AM
  • I got the answer tx.

    string[] result = Directory.GetFiles(path, "*@*.ost");  

    • Marked as answer by Tiaan stassen Tuesday, October 30, 2012 11:12 AM
    Tuesday, October 30, 2012 11:12 AM

All replies

  • Hi,

    The easy option is to use string.Contains to see if a string contains a specific character or sequence. Your problem does however sound like it could use a Regular Expression.

    Edit:
    Have a look at the possibilities to 'query' the filesystem using LINQ. That's awesome and could help you enormous.

    Hope this helps


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick


    Tuesday, October 30, 2012 8:48 AM
  • I tried this :

     bool b = path.Contains("@");

    But returns false.

    My main problem is that i am searching a path for a certain file which has a .ost extension and has a certain string in the filename.

    Tuesday, October 30, 2012 8:55 AM
  • Hope this helps

     private void button1_Click(object sender, EventArgs e)
     {
                  String source = "test@filename.txt";
                  if (source.Contains("@"))
                  {
                      int index = source.IndexOf('@');
                      String result = source.Substring(index, source.Length-index);
                      MessageBox.Show(result);
                  }
     }


    If a post answers your question, please click "Mark As Answer" and "Mark as Helpful" on that post.

    Tuesday, October 30, 2012 9:18 AM
  • Hi,

    Have a look at my Edit (about using LINQ with the filesystem.

    Are you sure the @ is in the string? the following code returns true for me, so there must be something else wrong...

    "me@msdn.com".Contains("@")

    Hope this helps

    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    Tuesday, October 30, 2012 9:33 AM
  • If i understand you want to search inside a path, the files that contains @? If so, you should use contains but need to loop throught the path sarching in it filename.

    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    Tuesday, October 30, 2012 10:50 AM
  • I believe you can try something like this

                Directory.GetFiles(myPath, "*.txt")//define extension here or *.* for all
                    .Select(Path.GetFileNameWithoutExtension) //for example get filename without extension
                    .Select(p => p.Contains("@")); 


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    Tuesday, October 30, 2012 10:56 AM
  • I got the answer tx.

    string[] result = Directory.GetFiles(path, "*@*.ost");  

    • Marked as answer by Tiaan stassen Tuesday, October 30, 2012 11:12 AM
    Tuesday, October 30, 2012 11:12 AM