Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-30
5
Medium Priority
?
1,046 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
  • 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 2000 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

Independent Software Vendors: 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!

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

963 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