Link to home
Start Free TrialLog in
Avatar of paultran00
paultran00Flag for United States of America

asked on

powershell parse path and filename

Hi,  in powershell:

1) From the right, after the first PIPE SYMBOL | , how do I get the path and filename that follows?

EXAMPLE: I want the string to the right of the word "MAIL|"

20190408|MEMBER|MAIL|\\folder\folder2\MyFile.pdf

RETURNS:
\\folder\folder2\MyFile.pdf

2) filename and extension:

RETURNS:
MyFile.pdf

3) filename only:

RETURNS:
MyFile

4) from the left after the first PIPE | symbol, pare the data

RETURNS:

MEMBER
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

$str="20190408|MEMBER|MAIL|\\folder\folder2\MyFile.pdf"

$allvalues=$str.Split('|')
 $file= $allvalues[3]
 $fileNameExt =$file.Split("\\")[4]
 $fileNameOnly =$fileNameExt.Split(".")[0]
 $member=$allvalues[1]


 Write-Host "file: $file `n

Open in new window


User generated image
The basic idea of using split is ok, but the path parsing too restrictive - it only works for paths two folders deep ;-). And the code is truncated, the write-host is incomplete.
$str="20190408|MEMBER|MAIL|\\folder\folder2\MyFile.pdf"

$member, $fullname = $str.split('|')[1,-1]
$file =$fullname.Split('\\')[-1]
$fileName =$file.Split(".")[0]

Open in new window

Avatar of paultran00

ASKER

To Qlemo:

In line 3, what does [1,-1] mean?

In line 4, what does [-1] mean?
Instead of the filename called MyFile, what if it were called MyFile_19-34MEM111  or MyFile_19-34PHY111 .  How would I further parse the split starting at "_" (from the left) , and end parsing at either "MEM" or "PHY" , then return 19-34?
[] is indexing into an array, where the first index is 0, and -1 is always the last one.
$member, $fullname = $str.split('|')[1,-1]

Open in new window

extracts the second and last fragment of the split, and assigns each to an own variable. That is
$member, $fullname = 1,2

Open in new window

lets $member be 1 and $fullname 2. In the code above you get the second and last part which are separated by pipes.

$file =$fullname.Split('\\')[-1]

Open in new window

then just takes the last part after a backslash from the full filename, which is the file name plus extension, without any path elements.
'\\' is required here because the .Split method accepts regular expressions, and a backslash is an escape character there, so we need to use double backspace.
For further spliiting the file name into parts, just continue with the pattern shown: $filename contains the file name without extension, so we can use that to split:
$number = $filename.Split('_')[1]

Open in new window

How would I further parse the split starting at "_" (from the left) , and end parsing at either "MEM" or "PHY" , then return 19-34?
You certainly changed the comment while I posted the last comment ;-).
Now we are starting with more complex parsing, and a regular expression might be the way to go:
$number = $filename -replace '.*_([0-9-]+)(?:MEM|PHY).*', '$1'

Open in new window

This basically will extract
   after arbitrary text followed by an underscore   .*_
   the digits including a hyphen    [0-9-]+
   followed by MEM or PHY and arbitrary text   (MEM|PHY).*
      which is ignored   (?: ... )
$1 stands for the first (group) pattern matched - which is the result of ([0-9-]+)

If that is too complex, you can try
$number = ($filename -split '_|MEM|PHY')[1]

Open in new window

BTW, I just noticed I've been mislead by Jose about .Split - it doesn't use regular expressions, so you can use .Split('\') instead of .Split('\\'), but it doesn't matter for this particular case.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.