Solved

How to compare filename patterns like *.* with a string

Posted on 2008-10-30
5
1,043 Views
Last Modified: 2013-12-17
Hi,

I'm trying to compare one String like "prueba.txt" with a pattern filename like "*.txt".

I know that I can do it by using regular expressions but I think that Microsoft Framework must to offer this functionallity but I don't know where it is.

Can anyone help me?

Thanks in advance
0
Comment
Question by:VegasCid
[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
  • 3
5 Comments
 
LVL 8

Expert Comment

by:dampsey
ID: 22839682
you can use FileInfo class to check the extension of a file

                System.IO.FileInfo x = new System.IO.FileInfo("filenamehere");
                if (x.Extension == "txt") SaveTheWorld();

or if you wish, you can use the following.

if ("myfile.txt".EndsWith(".txt")) SaveTheWorld();
0
 
LVL 1

Author Comment

by:VegasCid
ID: 22839722
Thank for your answer but It isn't my solution.

I'm looking to compare any string like "example.txt" with any other string like "exa*.txt".
If the string much pattern then return true or else if not.

It's exactly the way to find files in windows search.

I think Microsoft must offer this funcionality in some class!!!!
0
 
LVL 1

Author Comment

by:VegasCid
ID: 22840082
I need the solution. Point value to 500.

Maybe is more difficult that I think it
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 22840216
Matching Strings

The Like operator determines whether a string matches a given pattern. The operand on the left is the string being matched, the operand on the right is the pattern to match against. The result of the expression

string value Like string value  

is the Boolean value True or False depending on the success of the match.

The default behavior of the Like operator is to evaluate the expression as an exact match. Therefore, the statements,

Dim String_A As String = "This is a string."
Dim String_B As String = "This is a string."
Dim Matched As Boolean

Matched = String_A Like String_B

result in the Boolean value True being assigned to variable Matched. The match is on a character-by-character, left-to-right basis; plus, the match is case sensitive. The string "THIS IS A STRING." does not match string "This is a string.".

Wildcard characters can be used in the pattern for partial matches. These characters, along with special characters lists, are described in the following table.

Pattern Meaning
? Matches any single character at the position. For example, the expression "ABCDE" Like "AB?DE" returns True because it matches any character in the third position of the string as long as the remaining characters match exactly.
* Matches zero or more characters at the position. For example, the expression "ABCDE" Like "*CDE" returns True because it matches any characters at the beginning of the string followed by "CDE" at the end of the string. Likewise, "ABCDE" Like "AB*" returns True since the two strings begin with "AB" irrespective of any following characters. The expression "ABCDE" Like "*D*" returns True because the string contains "D" preceded and followed by any number of characters.
# Matches any single digit (0 - 9) at the position. For example, the expressions "12345" Like "12#45" and "12845" Like "12#45" return True because they match any decimal digit in the third position of the string as long as the remaining characters match exactly.
[char list] Matches any single character in [char list]. For example, the expression "C" Like "[ABCDE]" returns True because the character "C" is found in the list of characters "ABCDE".
[!char list] Matches any single character not in [char list]. For example, the expression "M" Like "[!ABCDE]" returns True because the character "M" is not found in the list of characters "ABCDE".

When matching against a [char list] the list can be specified as an ascending range of characters. To match against the upper-case alphabet, for instance, the character list is "[A-Z]"; to match against the decimal digits, the character list is "[0-9]".

The [char list] can be matched only against a single character. Thus, you cannot match a word to see if all its characters are included in the character list. You would need to compare each character individually with the character list by using a program loop

Source: http://www.maconstateit.net/tutorials/VBNET/VBNET01/vbnet01-04.aspx
        Dim String_A As String = "example.txt"
        Dim String_B As String = "exa*.txt"
        Dim Matched As Boolean
 
        Matched = String_A Like String_B

Open in new window

0
 
LVL 1

Author Closing Comment

by:VegasCid
ID: 31511608
Thanks a lot.

Best Regards
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

622 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