我使用powershell需要获取文件夹名称的一部分并将其设置为变量。例如,我有一个名为"test-123"的文件夹,路径是c:\tmp\test-123。
我只需要将"-123"设置为变量,并将其用于插入到.txt文件中(替换其他文本)。是否有任何方法可以做到这一点?
我使用powershell需要获取文件夹名称的一部分并将其设置为变量。例如,我有一个名为"test-123"的文件夹,路径是c:\tmp\test-123。
我只需要将"-123"设置为变量,并将其用于插入到.txt文件中(替换其他文本)。是否有任何方法可以做到这一点?
$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
。
或者:
$TestPath = "c:\tmp\test-123";
$TestPath.Split('-') | % { $Wanted = "-" + $_ }
$Wanted