Section 4.3. Use Wildcards to Define a Set of Items


4.3. Use Wildcards to Define a Set of Items

Wildcard matching is one of the great shortcuts provided by almost all command shells available today. Instead of having to enumerate a list of files one by one, we can use some special characters that translate to "anything." In this section, we'll look at some of the evolutionary changes in MSH with regard to wildcard matching, and we'll see how some of the new syntax can be used to list sets of files for processing more easily.

4.3.1. How Do I Do That?

When it comes to wildcards, MSH supports the familiar wildcard syntax using the ? and * characters to represent any character and any sequence of characters, respectively. For a quick refresher, let's look at a few commands that make use of these wildcard characters :

     MSH D:\MshScripts> get-childitem *.msh         Directory: FileSystem::D:\MshScripts     Mode    LastWriteTime     Length Name     ----    -------------     ------ ----     -a---   Mar 20 21:55        491  createfiles.msh     -a---   Mar 20 18:15        118  updatefiles.msh     -a---   Mar 23 00:50        117  unittest1.msh     -a---   Mar 28 22:04        243  unittest2.msh     -a---   Mar 22 23:36         49  unittest4.msh     -a---   Mar 22 23:36         88  unittestA.msh     MSH D:\MshScripts> get-childitem *files.msh         Directory: FileSystem::D:\MshScripts     Mode    LastWriteTime     Length Name     ----    -------------     ------ ----     -a---   Mar 20 21:55        491  createfiles.msh     -a---   Mar 20 18:15        118  updatefiles.msh     MSH D:\MshScripts> get-childitem unittest?.msh         Directory: FileSystem::D:\MshScripts     Mode    LastWriteTime     Length Name     ----    -------------     ------ ----     -a---   Mar 23 00:50        117  unittest1.msh     -a---   Mar 28 22:04        243  unittest2.msh     -a---   Mar 22 23:36         49  unittest4.msh

MSH offers several other wildcard characters for more flexibility in wildcard matching. Square brackets ([]) can be used to specify a set of characters, any one of which can be used to make a match. In other words, square brackets behave somewhat like the question mark (?) but, instead of matching any character, they match just those characters that appear inside the brackets. Sometimes, instead of having to write out every possible character, it's more convenient to define a range; the hyphen character (-) can be used between a start and an end character to indicate that anything in between is also valid:

     MSH D:\MshScripts> get-childitem unittest[14].msh         Directory: FileSystem::D:\MshScripts     Mode    LastWriteTime     Length Name     ----    -------------     ------ ----     -a---   Mar 23 00:50        117  unittest1.msh     -a---   Mar 22 23:36         49  unittest4.msh     MSH D:\MshScripts> get-childitem unittest[a-z].msh         Directory: FileSystem::D:\MshScripts     Mode    LastWriteTime     Length Name     ----    -------------     ------ ----     -a---   Mar 22 23:36         88  unittestA.msh

4.3.2. What About...

... Using a wildcard character in your filenames? Although most common filesystems prohibit the use of the ? and * in file and folder names, the [ and ] characters are generally available. If you have a file called default[12].htm, the command get-childitem default[12].htm won't find it because the wildcard rules tell the cmdlet to look for default1.htm and default2.htm. In this case, the escape character can be used to tell MSH not to expand the wildcard syntax; get-childitem default'[12'].htm will work well.

4.3.3. Where Can I Learn More?

The help page for the topic about_Wildcard contains more information about the changes in wildcard matches and introduces a couple of other special match characters, including ^ and $, which refer to the start and end of the filename, respectively.

With this flexibility at our fingertips when matching against filenames, it seems unjust that so far we've only been able to use the -eq comparison operator to test whether two strings are identical. It's time to see how wildcards can be applied to text strings in comparisons.




Monad Jumpstart
Monad Jumpstart
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 117

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net