Extract the filename from a path

PowershellPowershell 2.0

Powershell Problem Overview


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)

Powershell Solutions


Solution 1 - Powershell

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

Solution 2 - Powershell

Use [tag:.NET]:

[System.IO.Path]::GetFileName("c:\foo.txt") returns foo.txt. [System.IO.Path]::GetFileNameWithoutExtension("c:\foo.txt") returns foo

Solution 3 - Powershell

Using the BaseName in Get-ChildItem displays the name of the file and and using Name displays the file name with the extension.

$filepath = Get-ChildItem "E:\Test\Basic-English-Grammar-1.pdf"

$filepath.BaseName

Basic-English-Grammar-1

$filepath.Name

Basic-English-Grammar-1.pdf

Solution 4 - Powershell

You could get the result you want like this.

$file = "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv"
$a = $file.Split("\")
$index = $a.count - 1
$a.GetValue($index)

If you use "Get-ChildItem" to get the "fullname", you could also use "name" to just get the name of the file.

Solution 5 - Powershell

$(Split-Path "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv" -leaf)

Solution 6 - Powershell

Get-ChildItem "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv"
|Select-Object -ExpandProperty Name

Solution 7 - Powershell

You can try this:

[System.IO.FileInfo]$path = "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv"
# Returns name and extension
$path.Name
# Returns just name
$path.BaseName

Solution 8 - Powershell

Find a file using a wildcard and getting the filename:

Resolve-Path "Package.1.0.191.*.zip" | Split-Path -leaf

Solution 9 - Powershell

Just to complete the answer above that use .Net.

In this code the path is stored in the %1 argument (which is written in the registry under quote that are escaped: \"%1\" ). To retrieve it, we need the $arg (inbuilt arg). Don't forget the quote around $FilePath.

# Get the File path:  
$FilePath = $args
Write-Host "FilePath: " $FilePath
           
# Get the complete file name:
$file_name_complete = [System.IO.Path]::GetFileName("$FilePath")
Write-Host "fileNameFull :" $file_name_complete

# Get File Name Without Extension:
$fileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension("$FilePath")
Write-Host "fileNameOnly :" $fileNameOnly

# Get the Extension:
$fileExtensionOnly = [System.IO.Path]::GetExtension("$FilePath")
Write-Host "fileExtensionOnly :" $fileExtensionOnly

Solution 10 - Powershell

$file = Get-Item -Path "c:/foo/foobar.txt"
$file.Name

Works with both relative and absolute paths

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser664481View Question on Stackoverflow
Solution 1 - PowershellGordonView Answer on Stackoverflow
Solution 2 - PowershellangularsenView Answer on Stackoverflow
Solution 3 - PowershellSudhir TharayilView Answer on Stackoverflow
Solution 4 - PowershellDavidView Answer on Stackoverflow
Solution 5 - PowershellIainView Answer on Stackoverflow
Solution 6 - PowershellIan KempView Answer on Stackoverflow
Solution 7 - PowershellWasifView Answer on Stackoverflow
Solution 8 - PowershellLuis HernandezView Answer on Stackoverflow
Solution 9 - PowershellJ. DoesView Answer on Stackoverflow
Solution 10 - PowershellravisilvaView Answer on Stackoverflow