Solved

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

Posted on 2008-10-30
5
1,040 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

713 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