我使用powershell,我需要获取name文件夹的一部分并将其设置为变量。例如,我有名为“test-123”的文件夹,目录为c:\ tmp \ test-123。
我需要将变量设置为仅仅“-123”并使用它来插入.txt文件(替换其他一些文本)。有什么办法吗?
答案 0 :(得分:3)
内置字符串操作非常简单。
$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);
$FolderNameFromDashToEnd
现在应该具有值-123
。
答案 1 :(得分:0)
或者也许:
$TestPath = "c:\tmp\test-123";
$TestPath.Split('-') | % { $Wanted = "-" + $_ }
$Wanted