135

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)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user664481
  • 2,141
  • 9
  • 25
  • 31

10 Answers10

239

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
Gordon
  • 6,257
  • 6
  • 36
  • 89
  • 8
    This is a great answer. It solves the problem in the most "built-in" way. However, for my needs, I needed both the filename with extension and the base filename, so I used @angularsen's answer. There's another parameter, -leafbase, but it's only supported on PowerShell Core 6+. – Jamie Aug 23 '18 at 20:01
92

Use .NET:

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
angularsen
  • 8,160
  • 1
  • 69
  • 83
39

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

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

$filepath.BaseName

Output:

Basic-English-Grammar-1


$filepath.Name

Output:

Basic-English-Grammar-1.pdf

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sudhir Tharayil
  • 391
  • 3
  • 2
6

Find a file using a wildcard and get the filename:

Resolve-Path "Package.1.0.191.*.zip" | Split-Path -leaf
Luis Hernandez
  • 453
  • 5
  • 8
5
$(Split-Path "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv" -leaf)
phuclv
  • 37,963
  • 15
  • 156
  • 475
Iain
  • 123
  • 1
  • 6
5
Get-ChildItem "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv"
|Select-Object -ExpandProperty Name
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David
  • 603
  • 2
  • 6
  • 15
4

Just to complete angularsen's answer that uses .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 the extension:
$fileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension("$FilePath")
Write-Host "fileNameOnly :" $fileNameOnly

# Get the file extension:
$fileExtensionOnly = [System.IO.Path]::GetExtension("$FilePath")
Write-Host "fileExtensionOnly :" $fileExtensionOnly
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
J. Does
  • 785
  • 3
  • 10
  • 23
4

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
Wasif
  • 14,755
  • 3
  • 14
  • 34
2
$file = Get-Item -Path "c:/foo/foobar.txt"
$file.Name

Works with both relative and absolute paths

ravisilva
  • 259
  • 3
  • 10