Manipulating File System Paths (Part 3)

by Sep 6, 2013

In previous tips we illustrated how you can turn file system paths into arrays and then create new paths by changing or excluding parts of the array.

This can become even more convenient when you cast the array to an ArrayList type. Now, it is very easy to exclude existing or add new path elements.

This example renames the first subfolder, excludes the second subfolder and adds new subfolder after the forth existing subfolder:

$path = 'C:\users\Tobias\Desktop\functions.ps1'

[System.Collections.ArrayList]$array = $path -split '\\'
$array[1] = 'MyUsers'
$array.Insert(3, 'NewSubFolder')
$array.Insert(4, 'AnotherNewSubFolder')
$array -join '\' 

The resulting path is:

Twitter This Tip! ReTweet this Tip!