PowerShell:分割路径示例
cmdlet Split-Path 可用于拆分卷、文件夹和文件路径名。在这篇博文中,我将为您提供一些可以借鉴的示例。享受阅读代码示例的乐趣,以了解有关 Split-Path 强大功能的更多信息。
代码示例
请参考以下代码示例及其注释。
# Split Paths
# Show file path
Split-Path $home\documents\test.txt -Parent
# Show last element
Split-Path $home\documents\text.txt -Leaf
Split-Path $home\documents -Leaf
# Show Drive Letter
Split-Path $home\documents\text.txt -Qualifier
# Path without Drive Letter
Split-Path $home\documents\text.txt -NoQualifier
# Resolve Name (Show full path)
Split-Path $home\documents -Resolve
# Show Sub-Folder (dir3)
Split-Path (Split-Path c:\dir1\dir2\dir3\file.txt -Parent) -Leaf
# Show Sub-Folder (dir2)
Split-Path c:\dir1\dir2\dir3\file.txt -Parent |
# Show Sub-Folder (dir2)
Split-Path c:\dir1\dir2\dir3\file.txt -Parent | Split-Path -Leaf
# Or ...
('c:\dir1\dir2\dir3\file.txt' -split '\\')[2]
# Displays all word documents file names
Set-Location C:\
Split-Path -Path .\temp\*.docx -Resolve -Leaf
# Is this an absolute path ?
Split-Path $home\documents\text.txt -IsAbsolute
Split-Path .\Windows -IsAbsolute
# Combination ?
(Split-Path C:\Windows\system32) + '\'
+ (Split-Path $home\documents\test.txt -Leaf)
希望这篇文章对您有所帮助!