paultran00
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|\\fol der\folder 2\MyFile.p df
RETURNS:
\\folder\folder2\MyFile.pd f
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
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|\\fol
RETURNS:
\\folder\folder2\MyFile.pd
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
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]
ASKER
To Qlemo:
In line 3, what does [1,-1] mean?
In line 4, what does [-1] mean?
In line 3, what does [1,-1] mean?
In line 4, what does [-1] mean?
ASKER
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.
'\\' 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.
$member, $fullname = $str.split('|')[1,-1]
extracts the second and last fragment of the split, and assigns each to an own variable. That is$member, $fullname = 1,2
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]
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]
ASKER
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:
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
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'
This basically will extractafter 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]
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 TRIALMembers 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.
Open in new window