sud
sud

Reputation: 57

Get only filename from full path of a file

I want to split the path and just save the file name test.xls in a new variable

$namearray = "C:\Users\z003m\Desktop\Service_Tickets\automationscript\vbs\Newfolder\test.xls"

Upvotes: 4

Views: 13120

Answers (2)

k7s5a
k7s5a

Reputation: 1377

You can also use the .Net implementation

[System.IO.Path] is 10 times faster than the split-path cmdlet

[System.IO.Path]::GetFileName('c:\myFile.txt')
# result myFile.txt

[System.IO.Path]::GetFileNameWithoutExtension('c:\myFile.txt') 
# result myFile

Performance comparision: 50.000 items

[System.IO.Path]::GetFileName(...)    Average: 12,84143 

Split-Path                            Average: 113,537884

Upvotes: 4

G42
G42

Reputation: 10019

Recommend using the built-in Split-Path:

$newVariable = Split-Path $namearray -Leaf

Upvotes: 10

Related Questions