Powershell - extract file name and extension

I need to extract the file name and extension from, for example. my.file.xlsx. I don’t know the file name or extension, and there may be more points in the name, so I need to look for the line on the right, and when I find the first point (or the last on the left), extract the part on the right side and the part on the left side of this point.

Maybe there is a better solution, but I have not found anything here or anywhere else. Thanks you

+84
Mar 20 2018-12-12T00:
source share
8 answers

If the file comes off the disk and, as others have claimed, use the BaseName and Extension properties:

 PS C:\> dir *.xlsx | select BaseName,Extension BaseName Extension -------- --------- StackOverflow.com Test Config .xlsx 

If you were given the file name as part of a line (say, from a text file), I would use the static methods GetFileNameWithoutExtension and GetExtension from the System.IO.Path class:

 PS C:\> [System.IO.Path]::GetFileNameWithoutExtension("Test Config.xlsx") Test Config PS H:\> [System.IO.Path]::GetExtension("Test Config.xlsx") .xlsx 
+122
Mar 20 2018-12-12T00:
source share
 PS C:\Windows\System32\WindowsPowerShell\v1.0>split-path "H:\Documents\devops\tp-mkt-SPD-38.4.10.msi" -leaf tp-mkt-SPD-38.4.10.msi PS C:\Windows\System32\WindowsPowerShell\v1.0> $psversiontable Name Value ---- ----- CLRVersion 2.0.50727.5477 BuildVersion 6.1.7601.17514 PSVersion 2.0 WSManStackVersion 2.0 PSCompatibleVersions {1.0, 2.0} SerializationVersion 1.1.0.1 PSRemotingProtocolVersion 2.1 
+18
Apr 15 '15 at 22:24
source share

If it is from a text file and the intended name file is surrounded by spaces, this is the way:

 $a = get-content c:\myfile.txt $b = $a | select-string -pattern "\s.+\..{3,4}\s" | select -ExpandProperty matches | select -ExpandProperty value $b | % {"File name:{0} - Extension:{1}" -f $_.substring(0, $_.lastindexof('.')) , $_.substring($_.lastindexof('.'), ($_.length - $_.lastindexof('.'))) } 

If this is a file, you can use something like this based on your needs:

 $a = dir .\my.file.xlsx # or $a = get-item c:\my.file.xlsx $a Directory: Microsoft.PowerShell.Core\FileSystem::C:\ps Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 25/01/10 11.51 624 my.file.xlsx $a.BaseName my.file $a.Extension .xlsx 
+11
Mar 20 2018-12-12T00:
source share

Check the BaseName and Extension properties of the FileInfo object.

+7
Mar 20 2018-12-12T00:
source share

Use separation path

 $filePath = "C:\PS\Test.Documents\myTestFile.txt"; $fileName = (Split-Path -Path $filePath -Leaf).Split(".")[0]; $extension = (Split-Path -Path $filePath -Leaf).Split(".")[1]; 
+5
Sep 28 '16 at 15:31
source share
 PS C:\Users\joshua> $file = New-Object System.IO.FileInfo('file.type') PS C:\Users\joshua> $file.BaseName, $file.Extension file .type 
+4
Dec 25 '15 at 17:23
source share

This is an adaptation if anyone is interested. I needed to check if RoboCopy successfully copied one file to several servers:

  $Comp = get-content c:\myfile.txt ForEach ($PC in $Comp) { dir "\\$PC\Folder\Share\*.*" | Select-Object $_.BaseName } 

Pleasant and simple, and it shows the directory and file inside it. If you want to specify a single file name or extension, just replace * with everything you want.

  Directory: \\SERVER\Folder\Share Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2/27/2015 5:33 PM 1458935 Test.pptx 
0
03 Mar. '15 at 23:01
source share

just do this:

 $file=Get-Item "C:\temp\file.htm" $file.Name $file.Extension 
0
Jan 29 '17 at 10:12
source share



All Articles