I want to fetch only the UNC path from a text file. The UNC path always ends with a character combined with number like P100, R101, E000, etc.
Sometimes the UNC path ends with some characters or symbols like
\\Localhost\apps\data\logs\P100###
I have tried different ways to fetch string starts with \\ and ends with P100 0R R101 etc. But, unable to achieve this task through PowerShell. Kindly help.
If the UNC path is \\Localhost\apps\data\logs\P100### or \\Localhost\apps\data\logs\P100Target, then I only need \\Localhost\apps\data\logs\P100 through my script. Whatever extra character or symbols must be eliminated.
CodePudding user response:
Use a -replace operation:
'\\Localhost\apps\data\logs\O100',
'\\Localhost\apps\data\logs\P101###',
'\\Localhost\apps\data\logs\Q102Target' -replace '(?<=\\[A-Z]\d{3})[^\\] $'
To apply the above to file input (the individual lines from a file), use something like
(Get-Content file.txt) -replace '...'
The above yields:
\\Localhost\apps\data\logs\O100
\\Localhost\apps\data\logs\P101
\\Localhost\apps\data\logs\Q102
For an explanation of the regex passed to -replace and the ability to experiment with it, see this regex101.com page.
CodePudding user response:
If you have a path to each of your text files you could use Split-Path.
Split-Path -Path "\\UNC\Folder\Folder\P100\Example.txt" -Parent
