Alex Rivera | Logout

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

Asked 2012-04-25T14:02:19.860
52

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.

Edit
Report

1 Answer

7

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]
answered 2012-04-25T15:56:15.280

Your Answer