1

How can I make, in File.Exist method put some filename that contains some numbers? E.g "file1.abc", "file2.abc", "file3.abc" etc. without using Regex?

Rob
  • 26,989
  • 16
  • 82
  • 98
Saint
  • 5,397
  • 22
  • 63
  • 107

5 Answers5

4

Are you trying to determine if various files match the pattern fileN.abc where N is any number? Because File.Exists can't do this. Use Directory.EnumerateFiles instead to get a list of files that match a specific pattern.

  • The EnumerateFiles() method only exists in version 4 of the framework. For earlier versions, [Directory.GetFiles()](http://msdn.microsoft.com/en-us/library/07wt70x2(v=VS.100).aspx) will return the entire list of files which can then be examined for matches. (Though that may get back to the regex concern the OP wished to avoid.) – ThatBlairGuy May 04 '11 at 14:10
  • I used Directory.GetFiles because I use VS2008 but thank you Matthew for your attention to early name mapping to a specific number. – Saint May 04 '11 at 14:17
  • 1
    @ThatBlairGuy: [`Directory.GetFiles`](http://msdn.microsoft.com/en-us/library/wz42302f.aspx) has an overload that accepts a search pattern. – Daniel Hilgarth May 04 '11 at 14:18
1

Do you mean something like

for (int i = 1; i < 4; i++)
{
    string fileName = "file" + i.ToString() + ".abc";
    if (File.Exists(fileName))
    {
        // ...
    }
}
Jens
  • 25,229
  • 9
  • 75
  • 117
0
new DirectoryInfo(dir).EnumerateFiles("file*.abc").Any();

or

Directory.EnumerateFiles(dir, "file*.abc").Any();
Nappy
  • 3,016
  • 27
  • 39
0

In the unix world, it is called globbing. Maybe you can find a .NET library for that? As a starting point, check out this post: glob pattern matching in .NET

Community
  • 1
  • 1
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
-2

Below is the code snap which will return all files with name prefix "file" with any digits whose format looks like "fileN.abc", even it wont return with file name "file.abc" or "fileX.abc" etc.

List<string> str = 
    Directory.EnumerateFiles(Server.MapPath("~/"), "file*.abc")
      .Where((file => (!string.IsNullOrWhiteSpace( 
             Path.GetFileNameWithoutExtension(file).Substring( 
             Path.GetFileNameWithoutExtension(file).IndexOf(
              "file") + "file".Length))) 
            &&  
            (int.TryParse(Path.GetFileNameWithoutExtension(file).Substring( 
                Path.GetFileNameWithoutExtension(file).IndexOf("file") + "file".Length), 
                out result) == true))).ToList();

Hope this would be very helpful, thanks for your time.

Elias Hossain
  • 4,410
  • 1
  • 19
  • 33
  • whats a "code snap"? You mean snippet? Also this is a rather hefty expression. What advantages do you see with this compared to the accepted answer? – vidstige Nov 21 '11 at 18:06
  • Actually, I meant "code snap" to "code block", my given code is executing and working 100%, Also with the accepted answer it allows file.abc file which is not desired( given file format is fileN.abc), but my code doesn't allow file.abc, it only allows fileN.abc where N is a number. – Elias Hossain Nov 21 '11 at 18:15
  • Hello @vidstige, may I know why you've down voted me though my answer is correct accordingly? – Elias Hossain Nov 21 '11 at 18:25
  • It's a very late answer to this question and the expression is very long and complicated when there are simpler solutions to this problem. – vidstige Nov 21 '11 at 18:28
  • @vidstige, I've answered as the accepted answer is wrong! Also, my answer is not complected at all, since filtering condition is a bit longer but works correctly, would you please review your vote. Thanks for your time. – Elias Hossain Nov 22 '11 at 00:18
  • are you 100% sure it is wrong? Perhaps the asker was not able to phrase his question perfectly...? The beauty is in the eye of the beholder. – vidstige Nov 22 '11 at 01:03
  • Hello @vidstige, I'm giving asker question here once again: "How can I make, in File.Exist method put some **filename that contains some numbers**? E.g "file1.abc", "file2.abc", "file3.abc" etc. **without using Regex**?" According to the question, I'm 100% sure that the accepted answer is wrong, it allows any files with format fileX.abc where X is any valid Character/SetOfCharacter for file name, but it is supposed to accept only fileN.abc, where N is number only and in this case my answer is 100% correct. Once again, please review your vote here. Thanks for your time. – Elias Hossain Nov 22 '11 at 03:20
  • Again, the accepted answer allows the file with name **file.abc** which is not desired! Thanks for your review – Elias Hossain Nov 22 '11 at 03:23
  • Your missing the point. I understand that given the way the question is phrased your answer is more correct. But set your mind free and think about this - What if the question is not correct? Ok? Remeber - The accepted answer was accepted by the asker. – vidstige Nov 22 '11 at 11:13
  • My concern is what the asker is asking not what is in the mind of asker. Its his fault, not mine. I'm always correct! The asker could modify his question with EDIT section? – Elias Hossain Nov 22 '11 at 11:51
  • I've checked the accepted answer is incorrect according to the asked question and consequently I've answered, is it my fault? – Elias Hossain Nov 22 '11 at 11:53
  • Hello @vidstige, I'm looking for your vote review, thanks for your time. – Elias Hossain Nov 22 '11 at 15:30
  • You're a smart guy. I'm just trying to help you get lot's of points here on stackoverflow. The best way to do so is by focusing on helping people. – vidstige Nov 22 '11 at 15:44