Extract only certain characters from a file

Hi

How can i convert a non string variable to a string variable so i can do substring.

I have the follwoing:

$serverone = Select-String -Path “C:\Users\spate\Documents\sample*.txt” -Pattern ‘server one’

Get me:

C:\Users\spate\Documents\SampleFile.txt:1:Server one
C:\Users\spate\Documents\SampleFile10.txt:1:Server one
C:\Users\spate\Documents\SampleFile3.txt:1:Server one
C:\Users\spate\Documents\SampleFile4.txt:1:Server one

But then I want to extract only “SampleFile*” to another variable

to only get:

SampleFile.txt

SampleFile10.txt

SampleFile4.txt

SampleFile3.txt

Thanks in Adavance.

Please ingnore

“How can i convert a non string variable to a string variable so i can do substring.”

Thanks

 

There might be a cleaner or simpler way, but this should work assuming the string (path) always follows the same pattern.

$String = 'C:\Users\spate\Documents\SampleFile10.txt:1:Server one'

$String.Substring($String.LastIndexOf('\') + 1 ,($String.LastIndexOf(':') - $String.LastIndexOf('\') - 3))

Thank you Fer!

Couple of ways using regex after you have your strings. You could probably incorporate in the original select-string but I’m not sure what’s going on before then.

$str = @'
C:\Users\spate\Documents\SampleFile.txt:1:Server one
C:\Users\spate\Documents\SampleFile10.txt:1:Server one
C:\Users\spate\Documents\SampleFile3.txt:1:Server one
C:\Users\spate\Documents\SampleFile4.txt:1:Server one
'@

$str -replace '(?m)^.+\\|:.+$'

Output

SampleFile.txt
SampleFile10.txt
SampleFile3.txt
SampleFile4.txt

Hi Fer,

I have been mulling and pondering on your logic for:

 

$String = 'C:\Users\spate\Documents\SampleFile10.txt:1:Server one'
$String.Substring($String.LastIndexOf('\') + 1 ,($String.LastIndexOf(':') - $String.LastIndexOf('\') - 3))
I undertand upto $String.Substring($String.LastIndexOf('\') + 1 ,
but not getting the logic behind
($String.LastIndexOf(':') - $String.LastIndexOf('\') - 3))
For my future refernence i want to be able to understand what you did do i can apply. Can you please explain your logic.
I really appreciate it.
Thanks
Sunny
 

I really don’t understand why you stubbornly refuse to use an available working solution …

http://powershell.org/forums/topic/getting-error-in-following-serverone-foreach/#post-292153

The pure Powershell way is more robust, less error prone and easier to read and to understand. Stick with it. It makes your life easier.

And BTW: To extract a file name from a file path is quite easy with a pure Powershell cmdlet: Split-Path. :wink:

I appreciate your desire to understand. I hate to steal Fer’s thunder but I can explain it for you. Having said that Olaf is right about pure PowerShell although Split-Path isn’t needed for the data you desire. Select-String returns a “MatchInfo” object that has not only a Path property but also a Filename property which is what you are looking for.

Select-String -Path “C:\Users\spate\Documents\sample*.txt” -Pattern ‘server one’ |
    Select-Object -ExpandProperty Filename

The key here is PowerShell is an object oriented scripting language. Just because a cmdlet returns “default” information to the screen based on the object type, that is NOT the full object. If you pipe the results to Get-Member, you’ll see all the available properties and methods.

Fer used the substring method of the string object. The substring method can take 1 or 2 arguments. In this case he passed two arguments. The first is the starting index, the second is how many characters to return.

For the starting index, he used:

$String.LastIndexOf('\') + 1

This is calling the LastIndexOf method which does exactly what the name implies, returns the last index of the character specified. In this case, it is returning the index (position) of the last \ then adding 1 to it so it starts at the very next character.

For how many characters to return (2nd argument of the SubString method), he used:

($String.LastIndexOf(':') - $String.LastIndexOf('\') - 3)

The same LastIndexOf method is used here to determine how long the filename is. The flaw in this is that it expects the results to always have a single character following the first : but if your files have more than 9 lines of text and the search finds your criteria on line 10, then this would be off.