.net powershell - 提取文件名和扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9788492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:19:20  来源:igfitidea点击:

powershell - extract file name and extension

.netpowershellpowershell-2.0

提问by culter

I need to extract file name and extension from e.g. my.file.xlsx. I don't know the name of file or extension and there may be more dots in the name, so I need to search the string from the right and when I find first dot (or last from the left), extract the part on the right side and the part on the left side from that dot.

我需要从例如 my.file.xlsx 中提取文件名和扩展名。我不知道文件或扩展名的名称,名称中可能有更多的点,所以我需要从右边搜索字符串,当我找到第一个点(或从左边开始)时,提取部分右侧和该点左侧的部分。

Maybe there is better solution, but I did'n find anything here or anywhere else. Thank you

也许有更好的解决方案,但我在这里或其他任何地方都没有找到任何东西。谢谢

回答by Goyuix

If the file is coming off the disk and as others have stated, use the BaseNameand Extensionproperties:

如果文件从磁盘上脱落并且如其他人所述,请使用BaseNameExtension属性:

PS C:\> dir *.xlsx | select BaseName,Extension

BaseName                                Extension
--------                                ---------
StackOverflow.com Test Config           .xlsx  

If you are given the file name as part of string (say coming from a text file), I would use the GetFileNameWithoutExtensionand GetExtensionstatic methods from the System.IO.Pathclass:

如果您将文件名作为字符串的一部分(比如来自文本文件),我将使用System.IO.Path类中的GetFileNameWithoutExtensionGetExtension静态方法:

PS C:\> [System.IO.Path]::GetFileNameWithoutExtension("Test Config.xlsx")
Test Config
PS H:\> [System.IO.Path]::GetExtension("Test Config.xlsx")
.xlsx

回答by Straff

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

回答by CB.

If is from a text file and and presuming name file are surrounded by white spaces this is a 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 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

回答by Jaqueline Vanek

PS C:\Users\joshua> $file = New-Object System.IO.FileInfo('file.type')
PS C:\Users\joshua> $file.BaseName, $file.Extension
file
.type

回答by Shay Levy

Check the BaseName and Extension properties of the FileInfo object.

检查 FileInfo 对象的 BaseName 和 Extension 属性。

回答by Ernest Correale

Use Split-Path

使用分割路径

$filePath = "C:\PS\Test.Documents\myTestFile.txt";
$fileName = (Split-Path -Path $filePath -Leaf).Split(".")[0];
$extension = (Split-Path -Path $filePath -Leaf).Split(".")[1];

回答by Esperento57

just do it:

去做就对了:

$file=Get-Item "C:\temp\file.htm"
$file.Basename 
$file.Extension

回答by hans57sauc

This is an adaptation, if anyone is curious. I needed to test whether RoboCopy successfully copied one file to multiple servers for its integrity:

这是一种改编,如果有人好奇的话。我需要测试 RoboCopy 是否成功将一个文件复制到多个服务器以确保其完整性:

   $Comp = get-content c:\myfile.txt

ForEach ($PC in $Comp) {
    dir "\$PC\Folder\Share\*.*" | Select-Object $_.BaseName
}

Nice and simple, and it shows the directory and the file inside it. If you want to specify one file name or extension, just replace the *'s with whatever you want.

很好很简单,它显示了目录和里面的文件。如果您想指定一个文件名或扩展名,只需将 * 替换为您想要的任何内容。

    Directory: \SERVER\Folder\Share

Mode                LastWriteTime     Length Name                                                                                                                                             
----                -------------     ------ ----                                                                                                                                             
-a---         2/27/2015   5:33 PM    1458935 Test.pptx