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