2017-06-05 64 views
0

我要拆分的路径,只是保存文件名test.xls在一个新的变量获取从一个文件的完整路径只有文件名

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

'$ newVariable =拆分路径$ namearray -Leaf' – gms0ulman

+1

过得好到'$ namearray'?如果你使用'Get-ChildItem',你可以从中得到文件名... – Nick

+0

@ gms0ulman你应该发表你的建议作为答案,因为它是最好的方法。 –

回答

6

推荐使用内置Split-Path

$newVariable = Split-Path $namearray -Leaf 
1

您还可以使用.NET实现

[System.IO.Path]分路小命令

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

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

性能比较:50000项

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

Split-Path       Average: 113,537884 
+0

这是一个很糟糕的方式来做到这一点,使用'分离路径'效率更高。 –

+0

我改变了我的帖子。你能删除你的评论吗? – k7s5a

+0

'Split-Path'支持多个PowerShell提供程序;它不仅分割文件系统名称,而且分割多个路径。这将解释为什么它(毫无意义,除非你每秒划分数千个名字)比.NET实现慢。 –

相关问题