Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

688 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