0

I have code

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

and I get somthing that looks like this

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

and i want only some part of it = rt-1.ci.cou in my $relault .txt file

Matt
  • 45,022
  • 8
  • 78
  • 119
  • How do you decide what part of it you want? i.e. What's the rule? e.g. Is the `\\Server\path\path\Config.Current.Running.` part consistent, or can that vary / is there at least some consistent structure to it? – JohnLBevan Apr 06 '17 at 13:29
  • Is the first part of your question, where you give code for `compare-object`, etc, relevant, or is the question just: "Given a string such as `\\Server\path\path\Config.Current.Running.rt-1.ci.cou.txt` how do I reduce it to `rt-1.ci.cou`?) – JohnLBevan Apr 06 '17 at 13:30

1 Answers1

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
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178