Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting just the lowest-level directory name for a file from split-path using PowerShell

Tags:

powershell

I need to get just the last part of the path name for a file.

Example:

c:\dir1\dir2\dir3\file.txt

I need to get dir3 into a variable.

I have been trying with Split-Path, but it gives me the whole path.

like image 951
Travis Avatar asked Apr 25 '12 14:04

Travis


People also ask

How do I split a file name in PowerShell?

The Split-Path cmdlet returns only the specified part of a path, such as the parent folder, a subfolder, or a file name. It can also get items that are referenced by the split path and tell whether the path is relative or absolute. You can use this cmdlet to get or submit only a selected part of a path.

How do I get the directory of a file in PowerShell?

Use Get-ChildItem to Get the Full Path of the Files in PowerShell. The Get-ChildItem cmdlet displays a list of files and directories on the specified location. You can use the -Recurse parameter to list all files and directories recursively. It also shows the sub-directories and their files.

How do I list only directories in PowerShell?

To get folder name only in PowerShell, use Get-ChildItem – Directory parameter and select Name property to list folder name only on PowerShell console.

How do I get a list of files in a directory in PowerShell?

To display the directory content, Get-ChildItem cmdlet is used. You need to provide the path of the directory or if you are in the same directory, you need to use only Get-ChildItem directly. In the below example, we need to display the contents of the D:\temp directory.


5 Answers

This takes two invocations of Split-Path AFAICT:

PS> Split-Path (Split-Path c:\dir1\dir2\dir3\file.txt -Parent) -Leaf
dir3
like image 167
Keith Hill Avatar answered Oct 03 '22 02:10

Keith Hill


This question is specifically asking for split-path it seems, but some other ways are:

If the file exists, I find it is much nicer to do:

(Get-Item c:\dir1\dir2\dir3\file.txt).Directory.Name

If the file does not exist, this won't work. Another way in that case is to use the .NET API, for example:

$path = [System.IO.Path];
$path::GetFileName($path::GetDirectoryName("c:\dir1\dir2\dir3\file.txt"))
like image 37
Marcus Avatar answered Oct 03 '22 00:10

Marcus


If you want to keep it simple and the path is going to be in normal form, you can use String.Split():

"c:\dir1\dir2\dir3\file.txt".split("\")[-2]
like image 29
manojlds Avatar answered Oct 03 '22 01:10

manojlds


Another option using System.Uri:

PS> ([uri]"c:\dir1\dir2\dir3\file.txt").segments[-2].trim('/')
dir3

And if the file exists on disk:

PS> (dir c:\dir1\dir2\dir3\file.txt).directory.name
like image 22
Shay Levy Avatar answered Oct 03 '22 00:10

Shay Levy


In general, if you want the name of the directory you are in I used this (thanks to Shay Levi for the original idea):

PS> (dir).directory.name[0]

like image 45
Mikkel Avatar answered Oct 03 '22 02:10

Mikkel