I want to extract filename from below path:
D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv
Now I wrote this code to get filename. This working fine as long as the folder level didn't change. But in case the folder level has been changed, this code need to rewrite. I looking a way to make it more flexible such as the code can always extract filename regardless of the folder level.
($outputFile).split('\')[9].substring(0)
split() method in Python is used to Split the path name into a pair head and tail. Here, tail is the last path name component and head is everything leading up to that. In the above example 'file. txt' component of path name is tail and '/home/User/Desktop/' is head.
If you are ok with including the extension this should do what you want.
$outputPath = "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv" $outputFile = Split-Path $outputPath -leaf
Use .net:
[System.IO.Path]::GetFileName("c:\foo.txt")
returns foo.txt
. [System.IO.Path]::GetFileNameWithoutExtension("c:\foo.txt")
returns foo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With