我有一系列字符串,它们是文件的完整路径。我想只保存文件名,不包括文件扩展名和前导路径。因此从这个字符串:
c:\temp\myfile.txt
为了
myfile
实际上我并不是在遍历目录,因此不能像PowerShell的basename
属性一样使用。而是只处理字符串。
我有一系列字符串,它们是文件的完整路径。我想只保存文件名,不包括文件扩展名和前导路径。因此从这个字符串:
c:\temp\myfile.txt
为了
myfile
实际上我并不是在遍历目录,因此不能像PowerShell的basename
属性一样使用。而是只处理字符串。
解决显示完整路径、目录、文件名或文件扩展名问题比我想象中要容易得多。
## 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
其他形式:
$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)
$csr = Get-ChildItem -Path "$($domain.FullName)/*.csr"
然后 Write-Host "fileName: $($csr.Basename)"
- Scott PelakGet-Item
需要文件存在,否则会抛出错误。 - SebMa$PSCommandPath
返回当前正在运行的脚本的信息,在执行之前应该处于已保存状态。在这种情况下,Get-Item
应该始终返回有效值。 - Tony(Get-Item $PSCommandPath ).Basename
。谢谢! - undefined有一个方便的 .NET 方法可以做到这一点:
C:\PS> [io.path]::GetFileNameWithoutExtension("c:\temp\myfile.txt")
myfile
Split-Path C:\temp\myfile.txt -LeafBase
。 - Keith Hill([io.fileinfo]"c:\temp\myfile.txt").basename
或者"c:\temp\myfile.txt".split('\.')[-2]
$FileNameWoExt = $FileName.Substring(0, $FileName.Length -4)
这个命令。 - Timo你可以使用 basename 属性
PS II> ls *.ps1 | select basename
从PowerShell 6开始,您可以通过以下方式获取不带扩展名的文件名:
split-path c:\temp\myfile.txt -leafBase
@Keith,你好,这里还有一种选择:
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]
在扩展René Nyffenegger的回答时,对于那些没有访问PowerShell 6.x版本的人,我们使用Split Path,它不会测试文件是否存在:
Split-Path "C:\Folder\SubFolder\myfile.txt" -Leaf
(Split-Path "C:\Folder\SubFolder\myfile.txt" -Leaf).Split('.') | Select -First 1
(Split-Path "C:\Folder\SubFolder\myfile.txt" -Leaf).Split('.')[0]
$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)
针对任意路径字符串,System.IO.Path对象上的各种静态方法提供以下结果。
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
以下是生成上述输出的代码。
[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 ) );
这是一个没有括号的例子
[io.fileinfo] 'c:\temp\myfile.txt' | % basename