简体   繁体   English

从路径 PowerShell 中剥离文件

[英]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 .我想根据pathfilenameversion将找到的路径中的值分开。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM