Powershell - 如何提取子串

时间:2017-11-29 06:19:07

标签: powershell path

我是学习PowerShell的新手。尝试从我的Powershell脚本中的Azure数据湖存储路径\data\sb\published\Juned\Testfile.txt中提取“Testfile.txt”。我正在使用("\data\sb\published\Juned\Testfile.txt" -split '\')[0]

这显示错误

parsing "\" - Illegal \ at end of pattern.
At line:1 char:1
+ ("\data\sb\published\Juned\Testfile.txt" -split '\')[0]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException

1 个答案:

答案 0 :(得分:0)

这两种方法对我有用:

split-path "H:\Documents\devops\tp-mkt-SPD-38.4.10.msi" -leaf

TP-MKT-SPD-38.4.10.msi

[System.IO.Path]::GetFileName("C:\abc\xyz\test.txt")

的test.txt

[System.IO.Path]::GetFileNameWithoutExtension("C:\abc\xyz\test.txt")

测试

相关问题