Preview of PDF document textanddocmgt.pdf

Page 1 2 3 4 5 6 7 8 9 10 11 12 13

Text preview

Lecture 8 – Working with Strings and Document Mgt

Page 9 of 13

Using the split method we can separate the file paths into an array of strings, separated by a new line
character. Now we can use the pipeline Foreach-Object and use the Insert command to change the file
name. The insert command takes two parameters, the first is the insertion point, and the second is the
letter to be inserted. Notice you can only insert one character at a time. But you can chain the
commands together using dot notation to add multiple characters.
Type: $strpath | Foreach-Object{$_.Insert(31,"Q").Insert(32,"1").Insert(33,".")}

Figure 18: Using the Insert Method


Converting to a Character Array

When working with strings, there are times when it is easier to convert the string to an array which gives
you an index to reference each element. Suppose you had a list of part numbers, such as “129BX123”,
which represents the month of manufacture, 12, the plant number, 9, inventory lot number, BX123 the
item number. Restructuring of the inventory requires changing BX to BY. Converting the string to a
character array of individual characters will make the change easier.
Type: $PartID = 129BX123
Type: $PartArray = $PartID.ToCharArray()
Type: $PartArray. You
can see the each
character is an element
of an array. We can now
count from 0 and change
the element.
Figure 169: Converting to a Character Array
Type:$PartArray[4] = “Y”
Type: -join $PartArray to convert the array elements back into a string.

Regular Expressions

Everything we have discussed so far, in finding and replacing text can also be done using Regular
Expressions. Regular expressions are very powerful and very accurate in describing text patterns.
Administrators often use regular expressions to validate user input such as, date and time, IP address,