Extract the filename from a path - powershell

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)

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

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

Find a file using a wildcard and get the filename:
Resolve-Path "Package.1.0.191.*.zip" | Split-Path -leaf

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

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

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.

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

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

$file = Get-Item -Path "c:/foo/foobar.txt"
$file.Name
Works with both relative and absolute paths

Related

Get a specific folder name from path

How to get the 4th folder name and store it in a variable while looping through the files stored in a parent folder. For example, if the path is
C:\ParentFolder\Subfolder1\subfolder2\subfolder3\file.extension
C:\ParentFolder\Subfolder1\subfolder2\subfolder4\file.extension
C:\ParentFolder\Subfolder1\subfolder2\subfolder5\file.extension
then subfolder2 name should be stored in a variable. Can any one please help me on this?
get-childitem $DirectorNane -Recurse | Where-Object{!($_.PSIsContainer)} | % {
$filePath = $_.FullName
#Get file name
$fileName = Split-Path -Path $filePath -Leaf
} $FileI = Split-Path -Path $filePath -Leaf
Thanks in advance!
You can use the -split operator on the $filePath variable to get what you want.
$split = $filePath -split '\\'
$myvar = $split[3]
We use the double backslash to split with, because the first slash escapes the slash character to split the path by. Then, we can reference the part of the path we want in the array that gets generated in the "split" variable.
Additionally, you can solve this with a one liner using the following code:
$myvar = $filepath.split('\')[3]
This would ensure that you're always getting the fourth element in the array, but is a little less flexible since you can't specify what exactly you want based on conditions with additional scripting.
If you are asking how to get the parent directory of the directory containing a file, you can call Split-Path twice. Example:
$filePath = "C:\ParentFolder\Subfolder1\subfolder2\subfolder3\file.extension"
$parentOfParent = Split-Path (Split-Path $filePath)
# $parentOfParent now contains "C:\ParentFolder\Subfolder1\subfolder2"

Convert a part of a string into upper case

I am automatically obtaining directories from an application but I can't seem to get the actual directories with the correct case of letters.
For example I get $a='C:\test\dir\log\wqerst' but the actual directory is C:\test\dir\log\WQERST.
What I want is to uppercase only wqerst so it would show C:\test\dir\log\WQERST
I've already tried using substring but I don't know how I would be able to connect it to the whole directory once it is uppercase.
Windows system is not case sensitive, but if you want really this result, you can do it :
$a='C:\test\dir\log\wqerst'
$parentpath=Split-Path -Path $a
$file=(Split-Path -Path $a -Leaf).ToUpper()
$result=Join-Path $parentpath $file
$result
As James C. and vonPryz already wrote, there is not much point to get the case sensitive folder path. However you can use this helper method:
function Get-CaseSensitiveFilePath
{
Param
(
[string]$FilePath
)
$parent = Split-Path $FilePath
$leaf = Split-Path -Leaf $FilePath
$result = Get-ChildItem $parent | where { $_ -like $leaf }
$result.FullName
}
usage:
Get-CaseSensitiveFilePath -FilePath 'C:\test\dir\log\WQERST'
This will give you the case sensitive folder name but the directory must exist on the computer you execute the script...

Get file name only from a given path

I have a file which will have full path of files. For example:
servername\xyz\abc.txt
servername\pqr\ab1.txt
I need to get only file name (abc.txt and ab1.txt) using PowerShell.
Instead of using the resources getting the file you can use Split-Path on each line item. -Leaf is the switch needed to return just the file name.
PS M:\Scripts> split-path "servername\pqr\ab1.txt" -Leaf
ab1.txt
If you had this information in a file you could iterate through all the entires
$fileName = Get-Content fullfilepaths.txt | ForEach-Object{split-path $_ -Leaf}
Another
A simple one that would also work ( Not sure if the performance is different ) would be to use split and return the last element.
("servername\pqr\ab1.txt".Split("\"))[-1]
PSH$ $file = Get-ChildItem "servername\pqr\ab1.txt"
PSH$ $file.Name
PSH$ $file.Name
should return ab1.txt

In PowerShell, how can I test if a user-supplied string represents a full absolute path? [duplicate]

I'm trying to process a list of files that may or may not be up to date and may or may not yet exist. In doing so, I need to resolve the full path of an item, even though the item may be specified with relative paths. However, Resolve-Path prints an error when used with a non-existant file.
For example, What's the simplest, cleanest way to resolve ".\newdir\newfile.txt" to "C:\Current\Working\Directory\newdir\newfile.txt" in Powershell?
Note that System.IO.Path's static method use with the process's working directory - which isn't the powershell current location.
You want:
c:\path\exists\> $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(".\nonexist\foo.txt")
returns:
c:\path\exists\nonexists\foo.txt
This has the advantage of working with PSPaths, not native filesystem paths. A PSPath may not map 1-1 to a filesystem path, for example if you mount a psdrive with a multi-letter drive name.
What's a pspath?
ps c:\> new-psdrive temp filesystem c:\temp
...
ps c:\> cd temp:
ps temp:\>
temp:\ is a drive-qualified pspath that maps to a win32 (native) path of c:\temp.
-Oisin
When Resolve-Path fails due to the file not existing, the fully resolved path is accessible from the thrown error object.
You can use a function like the following to fix Resolve-Path and make it work like you expect.
function Force-Resolve-Path {
<#
.SYNOPSIS
Calls Resolve-Path but works for files that don't exist.
.REMARKS
From http://devhawk.net/blog/2010/1/22/fixing-powershells-busted-resolve-path-cmdlet
#>
param (
[string] $FileName
)
$FileName = Resolve-Path $FileName -ErrorAction SilentlyContinue `
-ErrorVariable _frperror
if (-not($FileName)) {
$FileName = $_frperror[0].TargetObject
}
return $FileName
}
I think you're on the right path. Just use [Environment]::CurrentDirectory to set .NET's notion of the process's current dir e.g.:
[Environment]::CurrentDirectory = $pwd
[IO.Path]::GetFullPath(".\xyz")
Join-Path (Resolve-Path .) newdir\newfile.txt
This has the advantage of not having to set the CLR Environment's current directory:
[IO.Path]::Combine($pwd,"non\existing\path")
NOTE
This is not functionally equivalent to x0n's answer. System.IO.Path.Combine only combines string path segments. Its main utility is keeping the developer from having to worry about slashes. GetUnresolvedProviderPathFromPSPath will traverse the input path relative to the present working directory, according to the .'s and ..'s.
I've found that the following works well enough.
$workingDirectory = Convert-Path (Resolve-Path -path ".")
$newFile = "newDir\newFile.txt"
Do-Something-With "$workingDirectory\$newFile"
Convert-Path can be used to get the path as a string, although this is not always the case. See this entry on COnvert-Path for more details.
function Get-FullName()
{
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline = $True)] [object[]] $Path
)
Begin{
$Path = #($Path);
}
Process{
foreach($p in $Path)
{
if($p -eq $null -or $p -match '^\s*$'){$p = [IO.Path]::GetFullPath(".");}
elseif($p -is [System.IO.FileInfo]){$p = $p.FullName;}
else{$p = [IO.Path]::GetFullPath($p);}
$p;
}
}
}
I ended up with this code in my case. I needed to create a file later in the the script, so this code presumes you have write access to the target folder.
$File = ".\newdir\newfile.txt"
If (Test-Path $File) {
$Resolved = (Resolve-Path $File).Path
} else {
New-Item $File -ItemType File | Out-Null
$Resolved = (Resolve-Path $File).Path
Remove-Item $File
}
I also enclosed New-Item in try..catch block, but that goes out of this question.
I had a similar issue where I needed to find the folder 3 levels up from a folder that does not exist yet to determine the name for a new folder I wanted to create... It's complicated. Anyway, this is what I ended up doing:
($path -split "\\" | select -SkipLast 3) -join "\\"
You can just set the -errorAction to "SilentlyContinue" and use Resolve-Path
5 > (Resolve-Path .\AllFilerData.xml -ea 0).Path
C:\Users\Andy.Schneider\Documents\WindowsPowerShell\Scripts\AllFilerData.xml
6 > (Resolve-Path .\DoesNotExist -ea 0).Path
7 >
There is an accepted answer here, but it is quite lengthy and there is a simpler alternative available.
In any recent version of Powershell, you can use Test-Path -IsValid -Path 'C:\Probably Fake\Path.txt'
This simply verifies that there are no illegal characters in the path and that the path could be used to store a file. If the target doesn't exist, Test-Path won't care in this instance -- it's only being asked to test if the provided path is potentially valid.
Both most popular answers don't work correctly on paths on not existing drives.
function NormalizePath($filename)
{
$filename += '\'
$filename = $filename -replace '\\(\.?\\)+','\'
while ($filename -match '\\([^\\.]|\.[^\\.]|\.\.[^\\])[^\\]*\\\.\.\\') {
$filename = $filename -replace '\\([^\\.]|\.[^\\.]|\.\.[^\\])[^\\]*\\\.\.\\','\'
}
return $filename.TrimEnd('\')
}
Check if the file exists before resolving:
if(Test-Path .\newdir\newfile.txt) { (Resolve-Path .\newdir\newfile.txt).Path }

Removing path and extension from filename in PowerShell

I have a series of strings which are full paths to files. I'd like to save just the filename, without the file extension and the leading path. So from this:
c:\temp\myfile.txt
to
myfile
I'm not actually iterating through a directory, in which case something like PowerShell's basename property could be used, but rather I'm dealing with strings alone.
Way easier than I thought to address the issue of displaying the full path, directory, file name or file extension.
## Output:
$PSCommandPath ## C:\Users\user\Documents\code\ps\test.ps1
(Get-Item $PSCommandPath ).Extension ## .ps1
(Get-Item $PSCommandPath ).Basename ## test
(Get-Item $PSCommandPath ).Name ## test.ps1
(Get-Item $PSCommandPath ).DirectoryName ## C:\Users\user\Documents\code\ps
(Get-Item $PSCommandPath ).FullName ## C:\Users\user\Documents\code\ps\test.ps1
$ConfigINI = (Get-Item $PSCommandPath ).DirectoryName+"\"+(Get-Item $PSCommandPath ).BaseName+".ini"
$ConfigINI ## C:\Users\user\Documents\code\ps\test.ini
Other forms:
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
split-path -parent $PSCommandPath
Split-Path $script:MyInvocation.MyCommand.Path
split-path -parent $MyInvocation.MyCommand.Definition
[io.path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name)
There's a handy .NET method for that:
C:\PS> [io.path]::GetFileNameWithoutExtension("c:\temp\myfile.txt")
myfile
Inspired by an answer of #walid2mi:
(Get-Item 'c:\temp\myfile.txt').Basename
Please note: this only works if the given file really exists.
or
([io.fileinfo]"c:\temp\myfile.txt").basename
or
"c:\temp\myfile.txt".split('\.')[-2]
you can use basename property
PS II> ls *.ps1 | select basename
Starting with PowerShell 6, you get the filename without extension like so:
split-path c:\temp\myfile.txt -leafBase
#Keith,
here another option:
PS II> $f="C:\Downloads\ReSharperSetup.7.0.97.60.msi"
PS II> $f.split('\')[-1] -replace '\.\w+$'
PS II> $f.Substring(0,$f.LastIndexOf('.')).split('\')[-1]
Expanding on René Nyffenegger's answer, for those who do not have access to PowerShell version 6.x, we use Split Path, which doesn't test for file existence:
Split-Path "C:\Folder\SubFolder\myfile.txt" -Leaf
This returns "myfile.txt". If we know that the file name doesn't have periods in it, we can split the string and take the first part:
(Split-Path "C:\Folder\SubFolder\myfile.txt" -Leaf).Split('.') | Select -First 1
or
(Split-Path "C:\Folder\SubFolder\myfile.txt" -Leaf).Split('.')[0]
This returns "myfile".
If the file name might include periods, to be safe, we could use the following:
$FileName = Split-Path "C:\Folder\SubFolder\myfile.txt.config.txt" -Leaf
$Extension = $FileName.Split('.') | Select -Last 1
$FileNameWoExt = $FileName.Substring(0, $FileName.Length - $Extension.Length - 1)
This returns "myfile.txt.config".
Here I prefer to use Substring() instead of Replace() because the extension preceded by a period could also be part of the name, as in my example. By using Substring we return the filename without the extension as requested.
Given any arbitrary path string, various static methods on the System.IO.Path object give the following results.
strTestPath = C:\Users\DAG\Documents\Articles_2018\NTFS_File_Times_in_CMD\PathStringInfo.ps1
GetDirectoryName = C:\Users\DAG\Documents\Articles_2018\NTFS_File_Times_in_CMD
GetFileName = PathStringInfo.ps1
GetExtension = .ps1
GetFileNameWithoutExtension = PathStringInfo
Following is the code that generated the above output.
[console]::Writeline( "strTestPath = {0}{1}" ,
$strTestPath , [Environment]::NewLine );
[console]::Writeline( "GetDirectoryName = {0}" ,
[IO.Path]::GetDirectoryName( $strTestPath ) );
[console]::Writeline( "GetFileName = {0}" ,
[IO.Path]::GetFileName( $strTestPath ) );
[console]::Writeline( "GetExtension = {0}" ,
[IO.Path]::GetExtension( $strTestPath ) );
[console]::Writeline( "GetFileNameWithoutExtension = {0}" ,
[IO.Path]::GetFileNameWithoutExtension( $strTestPath ) );
Writing and testing the script that generated the above uncovered some quirks about how PowerShell differs from C#, C, C++, the Windows NT command scripting language, and just about everything else with which I have any experience.
Here is one without parentheses
[io.fileinfo] 'c:\temp\myfile.txt' | % basename
This script searches in a folder and sub folders and rename files by removing their extension
Get-ChildItem -Path "C:/" -Recurse -Filter *.wctc |
Foreach-Object {
rename-item $_.fullname -newname $_.basename
}
This can be done by splitting the string a couple of times.
#Path
$Link = "http://some.url/some/path/file.name"
#Split path on "/"
#Results of split will look like this :
# http:
#
# some.url
# some
# path
# file.name
$Split = $Link.Split("/")
#Count how many Split strings there are
#There are 6 strings that have been split in my example
$SplitCount = $Split.Count
#Select the last string
#Result of this selection :
# file.name
$FilenameWithExtension = $Split[$SplitCount -1]
#Split filename on "."
#Result of this split :
# file
# name
$FilenameWithExtensionSplit = $FilenameWithExtension.Split(".")
#Select the first half
#Result of this selection :
# file
$FilenameWithoutExtension = $FilenameWithExtensionSplit[0]
#The filename without extension is in this variable now
# file
$FilenameWithoutExtension
Here is the code without comments :
$Link = "http://some.url/some/path/file.name"
$Split = $Link.Split("/")
$SplitCount = $Split.Count
$FilenameWithExtension = $Split[$SplitCount -1]
$FilenameWithExtensionSplit = $FilenameWithExtension.Split(".")
$FilenameWithoutExtension = $FilenameWithExtensionSplit[0]
$FilenameWithoutExtension
The command below will store in a variable all the file in your folder, matchting the extension ".txt":
$allfiles=Get-ChildItem -Path C:\temp\*" -Include *.txt
foreach ($file in $allfiles) {
Write-Host $file
Write-Host $file.name
Write-Host $file.basename
}
$file gives the file with path, name and extension: c:\temp\myfile.txt
$file.name gives file name & extension: myfile.txt
$file.basename gives only filename: myfile
Here are a couple PowerShell 5.1 one-liner options that put the path at the start of the line.
'c:\temp\myfile.txt' |%{[io.fileinfo]$_ |% basename}
OR
"c:\temp\myfile.txt" | Split-Path -Leaf | %{$_ -replace '\.\w+$'}