Answers:
解决我显示完整路径,目录,文件名或文件扩展名的问题比我想象的要容易得多。
$PSCommandPath
(Get-Item $PSCommandPath ).Extension
(Get-Item $PSCommandPath ).Basename
(Get-Item $PSCommandPath ).Name
(Get-Item $PSCommandPath ).DirectoryName
(Get-Item $PSCommandPath ).FullName
$ConfigINI = (Get-Item $PSCommandPath ).DirectoryName+"\"+(Get-Item $PSCommandPath ).BaseName+".ini"
$ConfigINI
其他形式:
$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)"
要么
([io.fileinfo]"c:\temp\myfile.txt").basename
要么
"c:\temp\myfile.txt".split('\.')[-2]
给定任意路径字符串,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 ) );
编写和测试生成上述脚本的脚本发现了一些有关PowerShell与C#,C,C ++,Windows NT命令脚本语言以及与我有任何经验的其他事物的区别的怪癖。
从PowerShell 6开始,您将获得不带扩展名的文件名,如下所示:
split-path c:\temp\myfile.txt -leafBase
扩展RenéNyffenegger的答案,对于那些无法访问PowerShell版本6.x的用户,我们使用Split Path,它不测试文件是否存在:
Split-Path "C:\Folder\SubFolder\myfile.txt" -Leaf
这将返回“ myfile.txt ”。如果我们知道文件名中没有句点,则可以拆分字符串并采用第一部分:
(Split-Path "C:\Folder\SubFolder\myfile.txt" -Leaf).Split('.') | Select -First 1
要么
(Split-Path "C:\Folder\SubFolder\myfile.txt" -Leaf).Split('.')[0]
这将返回“ myfile ”。为了安全起见,如果文件名可能包含句点,我们可以使用以下命令:
$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)
这将返回“ myfile.txt.config ”。在这里,我更喜欢使用Substring()而不是Replace(),因为带扩展名的句点也可以是名称的一部分,如我的示例所示。通过使用Substring,我们将按要求返回不带扩展名的文件名。
这可以通过将字符串拆分几次来完成。
#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
这是没有注释的代码:
$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