Solved

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now