title: Powershell - Splitting string into separate components
body: <p>I am writing a script which will basically do the following:</p>
  
  <p>Read from a text file some arguments:</p>
  
  <pre><code>DriveLetter ThreeLetterCode ServerName VolumeLetter Integer
  </code></pre>
  
  <p>Eg. <code>W MSS SERVER01 C 1</code></p>
  
  <p>These values happen to form a folder destination <code>W:\MSS\</code>, and a filename which works in the following naming convention:</p>
  
  <pre><code>SERVERNAME_VOLUMELETTER_VOL-b00X-iYYY.spi - Where The X is the Integer above
  </code></pre>
  
  <p>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. </p>
  
  <p>So at the moment --> Count lines in file, and loop for this many lines. </p>
  
  <pre><code>$lines = Get-Content -Path PostBackupCheck-Textfile.txt  | Measure-Object -Line
  for ($i=0; $i -le $lines.Lines; $i++)
  </code></pre>
  
  <p>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</p>
  
  <p>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 <code>_iXXX.spi</code> part and place this into the array value <code>a[X]</code> so I then have a complete filename to mount. This value will replace <code>iYYY.spi</code></p>
  
  <p>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 arguments fed to it from the text file :</p>
  
  <p>So,</p>
  
  <pre><code>SERVER01_C_VOL-b001-iYYY.spi and not anything else. 
  i.e. not  SERVER01_D_VOL-b001-iYYY.spi
  </code></pre>
  
  <p>Then take the iYYY value from the sort on the Get-ChildItem -Include and place that into the appropriate array item.</p>
  
  <p>I've literally no idea where to start, so any ideas are appreciated!</p>
  
  <p>Hopefully I've explained in enough detail. I have also placed the code on Pastebin: <a href="http://pastebin.com/vtFifTW6" rel="nofollow">http://pastebin.com/vtFifTW6</a></p>