Powershell - Splitting string into seperate components

Posted by TheD on Stack Overflow See other posts from Stack Overflow or by TheD
Published on 2012-11-18T16:41:12Z Indexed on 2012/11/18 17:00 UTC
Read the original article Hit count: 319

Filed under:

I am writing a script which will basically do the following:

Read from a text file some arguements:

DriveLetter ThreeLetterCode ServerName VolumeLetter Integer

Eg. W MSS SERVER01 C 1

These values happen to form a folder destination W:\MSS\, and a filename which works in the following naming convention:

SERVERNAME_VOLUMELETTER_VOL-b00X-iYYY.spi - Where The X is the Integer above

The value Y I need to work out later, as this happens to be the value of the incremental image (backups) and I need to work out the latest incremental.

So at the moment --> Count lines in file, and loop for this many lines.

$lines = Get-Content -Path PostBackupCheck-Textfile.txt  | Measure-Object -Line
for ($i=0; $i -le $lines.Lines; $i++)

Within this loop I need to do a Get-Content to read off the line I am currently looking at i.e. line 0, line 1, line 2, as there will be multiple lines in the format I wrote at the beginning and split the line into an array, whereby each part of the file, as seen above naming convention, is in a[0], a[1], a[2]. etc

The reason for this is because, I need to then sort the folder that contains these, find the latest file, by date, and take the _iXXX.spi part and place this into the array value a[X] so I then have a complete filename to mount. This value will replace iYYY.spi

It's a little complex because I also have to make sure when I do a Get-ChildItem with -Include before I sort it all by date, I am only including the filename that matches the arguements fed to it from the text file :

So,

SERVER01_C_VOL-b001-iYYY.spi and not anything else. 
i.e. not  SERVER01_D_VOL-b001-iYYY.spi

Then take the iYYY value from the sort on the Get-ChildItem -Include and place that into the appropriate array item.

I've literally no idea where to start, so any ideas are appreciated!

Hopefully I've explained in enough detail. I have also placed the code on Pastebin: http://pastebin.com/vtFifTW6

Thanks!

© Stack Overflow or respective owner

Related posts about powershell