Author Posts

March 6, 2017 at 7:31 am

I want just the last folder name. Example
Folder1->Folder2->file.txt
Or
C:\Folder1\Folder2\file.txt

I want just Folder2 as output result.

What i'm currently doing is

$names = Get-ChildItem -recurse -Filter "*.txt" -Name
Foreach(name in names){
$fileBaseName = [System.IO.Path]::GetDirectoryName("$name")
}

This code returns result as
Folder1\Folder2

I just want Folder1

March 6, 2017 at 8:22 am

Split-Path -Path 'Your Folder' -Parent

or

Get-Item -Path 'Your Folder' | Select-Object Parent

March 6, 2017 at 8:36 am

Split-Path -Path 'Your Folder' -Parent

and

Get-Item -Path 'Your Folder' | Select-Object Parent

giving same result as

$fileBaseName = [System.IO.Path]::GetDirectoryName("$name")

I just want the current folder name.

March 6, 2017 at 8:38 am

This Code

Split-Path (Split-Path "$name" -Parent) -Leaf

Worked for me. Thanks