当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell:分割路径示例

作者:精品下载站 日期:2024-12-14 07:36:42 浏览:11 分类:玩电脑

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)

希望这篇文章对您有所帮助!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯