Say you have the variable $source = "C:\temp\one\two\three"
and you want to set $destination
equal to $destination = "C:\temp\one\two"
programmatically, how might you do it?
The best idea I've had would be to trim that, but is there a better way?
Perhaps something like
$source = "C:\temp\one\two\three"
$dest = "..$source"
Getting the parent from a DirectoryInfo object, like Lee suggests, will certainly work. Alternately, if you prefer to use more PowerShellesque commands as opposed to direct .NET Framework calls, you can use PowerShell's built-in Split-Path cmdlet like this:
$source = "C:\temp\one\two\three"
$destination = Split-Path -Path $source -Parent
# $destination will be a string with the value "C:\temp\one\two"
$source = "C:\temp\one\two\three"
$dest = (new-object system.io.directoryinfo $source).parent.fullname
EDIT: You can get a DirectoryInfo
for a directory using get-item
so you can instead do:
$dest = (get-item $source).parent.fullname
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With