[英]Strip file from path PowerShell
I have a PowerShell Script that finds all log4j jar files on a system.我有一个 PowerShell 脚本,可以找到系统上的所有 log4j jar 文件。 I want to separate the values out of the found paths based on just
path
, filename
, and version
.我想根据
path
、 filename
和version
将找到的路径中的值分开。
For example, if I have a returned path of C:\Users\Administrator\AppData\Roaming\.minecraft\libraries\org\apache\logging\log4j\log4j-core\2.14.1\log4j-core-2.14.1.jar
, how can I get:例如,如果我返回的路径为
C:\Users\Administrator\AppData\Roaming\.minecraft\libraries\org\apache\logging\log4j\log4j-core\2.14.1\log4j-core-2.14.1.jar
,我怎样才能得到:
Just the path - C:\Users\Administrator\AppData\Roaming\.minecraft\libraries\org\apache\logging\log4j\log4j-core\2.14.1\
只是路径 -
C:\Users\Administrator\AppData\Roaming\.minecraft\libraries\org\apache\logging\log4j\log4j-core\2.14.1\
Just the file - log4j-core-2.14.1.jar
只是文件 -
log4j-core-2.14.1.jar
Just the version - 2.14.1
只是版本 -
2.14.1
I would simply use Get-ChildItem
for that.我会简单地使用
Get-ChildItem
。
$Item = Get-ChildItem -Path 'C:\Users\Administrator\AppData\Roaming\.minecraft\libraries\org\apache\logging\log4j\log4j-core\2.14.1\log4j-core-2.14.1.jar'
# Folder containing the file
$Item.PSParentPath
# Filename
$Item.Name
# Version extracted from the Parent path
# As string
($ITem.PSParentPath.Split('\'))[-1]
# As System.Version object
[Version]($ITem.PSParentPath.Split('\'))[-1]
Or, using Split-Path
或者,使用
Split-Path
$FullPath = 'C:\Users\Administrator\AppData\Roaming\.minecraft\libraries\org\apache\logging\log4j\log4j-core\2.14.1\log4j-core-2.14.1.jar'
$Filename = Split-Path -Path $FullPath -Leaf
$ParentPath = Split-Path -Path $FullPath -Parent
# Version from Parent path
$Version = $ParentPath.Split('\')[-1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.