簡體   English   中英

如何在Powershell中使用System IO獲得不帶限定符的文件夾名稱

[英]How to use System IO in powershell to get the folder name without qualifiers

我的目的是遍歷文件夾並通過“拍攝日期-文件夾名稱”重命名圖片文件。

如果使用以下命令在包含照片的目錄中運行該代碼,則該代碼有效:

[string]$newDirectory = (Split-Path $Path -Leaf)

在下面的示例中,如何僅獲得“ Windows”。 任何其他解決方案都將有所幫助。 謝謝。

Get-Item: BaseName : explorer Mode : -a--- Name : explorer.exe Length : 2871808 DirectoryName : C:\\Windows Directory : C:\\Windows IsReadOnly : False Exists : True FullName : C:\\Windows\\explorer.exe Extension : .exe CreationTime : 27.04.2011 17:02:33 CreationTimeUtc : 27.04.2011 15:02:33 LastAccessTime : 27.04.2011 17:02:33 LastAccessTimeUtc : 27.04.2011 15:02:33 LastWriteTime : 25.02.2011 07:19:30 LastWriteTimeUtc : 25.02.2011 06:19:30 Attributes : Archive

您可以直接使用PowerShell命令執行此操作-無需下拉至.NET:

Get-ChildItem *.jpg -Recurse | 
    Rename-Item -NewName {Join-Path $_.Directory "$($_.Directory.Name) - $($_.CreationTime.ToString('yyyyMMdd-HH-mm-ss'))$($_.extension)"} -WhatIf

根據您的喜好調整日期/時間格式字符串'yyyyMMdd-HH-mm-ss' ,但避免在名稱的時間部分中使用:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM