powershell - 获取名称文件夹的一部分

cha*_*les 3 powershell

我使用powershell,我需要获取名称文件夹的一部分并将其设置为变量。例如,我有一个名为“test-123”的文件夹,目录是 c:\tmp\test-123 。

我只需要将变量设置为“-123”并使用它来插入 .txt 文件(替换一些其他文本)。有什么办法吗?

Bac*_*its 5

非常简单的内置字符串操作。

$FullFolderPath = 'C:\tmp\test-123';

#Depending on PowerShell version, you may need ToString() here.
$FolderName = (Split-Path $FullFolderPath -Leaf).ToString();

#Gets the index of the first dash in the file name.  If you know you
#need the last dash, use LastIndexOf('-') instead.
$DashIndex = $FolderName.IndexOf('-');

#Return a substring from a starting position to the end of the string
$FolderNameFromDashToEnd = $FolderName.SubString($DashIndex);
Run Code Online (Sandbox Code Playgroud)

$FolderNameFromDashToEnd现在应该有值了-123