Powershell – Using split() to extract Filenames

| | Comments (1) | TrackBacks (0)

Powershell

If you’re reading this blog, you know what I am.  I’m a tech.  I a tech who deeply loves what Powershell does to help enable me on my job.  Not because Microsoft says so, but because I’ve gone from one side of the Coin doing pure GUI and a pile of Command line functions, to one Interface. 

So the thing I was playing with Today was split(). I was trying to figure out how to separate a FILENAME from a DIRECTORY name in a string that just had both.  My Example?

The $PROFILE variable, The Object that contains the pathname and filename of your personal Powershell Profile. Typically it looks like

C:\Users\SomeGoofyGuy\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

So how do you pull just the filename out of that?  Well actually if you knew that the filename was always a certain size and ALWAYS at the end you could use a substring() and say something like “Give me the last 20 letters”.  But a better way would to be find out how to pull out “all the information after the end of the Directory Name”

Which is where split() comes in.  If you run

$PIECES=$PROFILE.split(“/”)

Which says “Everytime you see a "/” in that line, break that into another chunk and store the results in the $PIECES object.  This of course is a name I completely made up, you could call it $PICTURESOFLILLY if you so liked ;)

What $PIECES will look like will be

C:
Users
SomeGoofyGuy
Documents
WindowsPowerShell
Microsoft.PowerShell_profile.ps1

Now here is where it starts to get easy.   The last Line will ALWAYS be the filename.  The preceding bits will always be the directory since the SPLIT() breaks it down in the ORDER it SEES it.  If you run a GET-MEMBER against $PIECES you’ll also notice there is a Property called Count.  Count is ALWAYS the number of members in that array.  Always.

$NUMBEROFPIECES=$PIECES.Count

Which is 6. So since I now have a simple list, and a way to know how many pieces are in that list, I can VERY easily say “Show me the last piece in the list which I already know is the Filename”

$FILENAME=$PIECES[$NumberOfPieces-1]

We say –1 in the Count because although you an *I* count from from 1 to 6, the computer starts at 0 for it’s numbering of the array.  So there ARE 6 pieces by the count but they are numbered from 0 to 5

So if you had to make all of this into a nice little Function here is how easy it is!

function global:GET-FILENAME ($FullPathName) {


$PIECES=$FullPathName.split(“\”)
$NUMBEROFPIECES=$PIECES.Count
$FILENAME=$PIECES[$NumberOfPieces-1]
return $FILENAME

}

If you like to return the Directory as well?  Just use the trim() Method we discovered yesterday and “Trim” the filename from the string! You can add that in one line and return both pieces like this.

function global:GET-SPLITFILENAME ($FullPathName) {


$PIECES=$FullPathName.split(“\”)
$NUMBEROFPIECES=$PIECES.Count
$FILENAME=$PIECES[$NumberOfPieces-1]
$DIRECTORYPATH=$FullPathName.Trim($FILENAME)

return $FILENAME, $DIRECTORYPATH

}

Now wasn’t that easy?

Powershell.  The Shell of Champions

Sean
The Energized Tech

0 TrackBacks

Listed below are links to blogs that reference this entry: Powershell – Using split() to extract Filenames.

TrackBack URL for this entry: http://www.energizedtech.com/cgi-sys/cgiwrap/jolyrogr/managed-mt/mt-tb.cgi/296

1 Comments

The methods of .NET's Path class ( http://msdn.microsoft.com/en-us/library/system.io.path.aspx ) can be used to accomplish this even more easily:

PS C:\Users\UserName> $PROFILE
C:\Users\UserName\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
PS C:\Users\UserName> [System.IO.Path]::GetFileName($PROFILE)
Microsoft.PowerShell_profile.ps1
PS C:\Users\UserName> [System.IO.Path]::GetDirectoryName($PROFILE)
C:\Users\UserName\Documents\WindowsPowerShell