0

我有代码比较-对象中删除resault的部分

Compare-Object $str $str1 | foreach InputObject | ft -auto | out-file "$resault" -width 5000 

,我得到的财产以后,看起来像这样

\\Server\path\path\Config.Current.Running.rt-1.ci.cou.txt 

,我想它只是某些部分= rt-1.ci .cou在我的$relault .txt文件中

+0

你如何决定你想要的部分?即什么是规则?例如\\ Server \ path \ path \ Config.Current.Running.'部分是否一致,或者可以改变/是否至少有一些一致的结构? – JohnLBevan

+0

是你的问题的第一部分,你提供'compare-object'等代码,或者只是问题:'给定一个字符串,例如\\ Server \ path \ path \ Config.Current.Running .rt-1.ci.cou.txt'我怎么把它减少到'rt-1.ci.cou'?) – JohnLBevan

回答

0
#Set a variable, Path, with the full string you're looking at 
[string]$Path = '\\Server\path\path\Config.Current.Running.rt-1.ci.cou.txt' 

#Remove the directory so we're left with just the filename 
[string]$Filename = (Split-Path -Path $Path -Leaf) 
#$FileName: Config.Current.Running.rt-1.ci.cou.txt 

#use regular expressions to capture the text that appears after the 3rd period (`.`) and before the last period. 
$Filename -replace '^[^.]*\.[^.]*\.[^.]*\.(.*)\.[^.]*$', '$1' 
#Output: rt-1.ci.cou