Reputation: 18147
My text file contents similar to following lines
xcopy Source Destination /y /r /Q
xcopy Source Destination /y /r
I am trying to remove all the characters after Desination.
I was trying with Indexof method and remove method. But i did not find a right answer
I tried the find the third occurence of Whitespace and remove from it. but it doesn't works.
$index=$line.IndexOf(" ",3)
$line=$line.RemoveAt($index)
Can some one helps me to achieve this using powershell
Upvotes: 0
Views: 1851
Reputation: 52609
I'm guessing your actual file will have paths such as this:
xcopy "C:\Folder with space" "C:\Folder 2 with space" /y /r /Q
So you can remove the xcopy arguments with a search/replace regular expression such as this:
'xcopy "C:\Folder with space" "C:\Folder 2 with space" /y /r /Q' -replace '/[\w]' , ''
The result is:
xcopy "C:\Folder with space" "C:\Folder 2 with space"
This way you don't have to worry about how many spaces are before the xcopy parameters. The parameters will be removed regardless of how many spaces came before them.
The regular expression matches and removes text with a forward slash followed by a character class representing a word character.
Upvotes: 2
Reputation: 6578
[Regex]::Replace("xcopy Source Destination /y /r /Q","^(xcopy) +([^ ]+) +([^ ]+).*$","`$1 `$2 `$3")
should do the trick.
Upvotes: 0