![重新命名Exif.ps1](https://rvso.com/image/1465785/%E9%87%8D%E6%96%B0%E5%91%BD%E5%90%8DExif.ps1.png)
我有一個資料夾,裡面有用不同相機拍攝的照片,所有這些照片都有正確的日期時間原始EXIF 標籤集。
假設有一個這樣的文件列表:
20150831_153712.jpg
IMG_5246.JPG
IMG_5247.JPG
20150902_201425.jpg
現在我想知道如何根據以下方式重命名這些文件日期時間原始課程標籤:
001_IMG_5246.JPG
002_20150831_153712.jpg
003_IMG_5247.JPG
004_20150902_201425.jpg
基本上我想要exif工具(或如果無法直接使用 Windows 批次程式)種類整個 JPEG 資料夾日期時間原始升序和重新命名操作應該放一個櫃檯檔案名稱前面的前綴(從而保留原始檔案名稱)。
我也想知道如何做測試命令以預覽重命名在它發生之前(為了看看是否有問題,我想使用測試名稱標籤)。
答案1
您想要與 ExifTool 一起使用的項目是-FileOrder
選項和FileSequence
標籤,以及使用進階格式化選項的一點 Perl。 FileOrder 選項將根據您使用該選項列出的時間對文件進行排序。這會稍微減慢 ExifTool 的速度,因為它必須讀取每個檔案兩次,但通常仍然比其他選項更快,例如循環並為每個循環呼叫 ExifTool。 FileSequence 標籤位於 ExifTool 內部,用於追蹤目前正在處理的檔案的編號。它從0開始,所以我們必須在高級處理中給它加1。我們還將填充零,使其至少為 3 個字元。
嘗試這個指令:
ExifTool "-TestName<${FileSequence;$_=0 x(3-length($_+1)).($_+1)}_$filename" -FileOrder DateTimeOriginal DIR
如果有效,只需替換-TestName
為-FileName
:
ExifTool "-FileName<${FileSequence;$_=0 x(3-length($_+1)).($_+1)}_$filename" -FileOrder DateTimeOriginal DIR
若要變更填入零的數量,請將 3 變更3-length
為您想要的數字。若要變更起始編號,請變更 中的 1 $_+1
。
答案2
我檢查了幾個重命名工具。主要問題是,像這樣的工具重命名大師和Exif工具可以存取 EXIF 資料。但我找不到產生編號清單的方法。
所以我寫了一個 PowerShell 腳本來完成它。我評論了每一行。應該很容易理解。只需閱讀它們即可。
我要做的第一件事就是搜尋“日期時間原始”的 ID所以我們可以使用GetPropertyItem(MyExifID)
.其次,我們將給定資料夾中實際具有 DateTimeOriginal 的所有影像新增到陣列中。從這裡開始就很容易了。我們按日期列對數組進行排序,增加一個簡單的計數器並使用模式建立新檔案名稱000_oldname
。
該腳本不會修改任何內容。它只輸出類似以下內容:
用它來檢查腳本會做什麼。檢查完可能的結果後,刪除#
該行之前的註解指示符#Rename-Item $_[0].Fullname $newName
。從現在開始,腳本將相應地重命名您的檔案。
重新命名Exif.ps1
# Set image folder to process
$strDirectory = "T:\Pictures"
# Create empty array
$arrSortMe = @()
# Load Windows library to access EXIF data
[void][Reflection.Assembly]::LoadWithPartialName("System.Drawing")
# Loop through all JPGs and JPEGs in the given image folder
Get-ChildItem -Path "$strDirectory\*" -Include *.jpg,*.jpeg | ForEach {
# Load current image into Powershell as bitmap object
$img = New-Object -TypeName system.drawing.bitmap -ArgumentList $_.FullName
# Error handling for images which doesn't have the specified EXIF data
Try {
# Get EXIF data with ID 36867 - which is "DateTimeOriginal"
$intExif = [Byte[]]$img.GetPropertyItem(36867).Value
# Release image or else later we can't rename it
$img.Dispose()
# Convert EXIF data from byte array to string
$strExif = [System.Text.Encoding]::Default.GetString($intExif, 0, $intExif.Length-1)
# Convert EXIF data from string to datetime
$dateExif = [DateTime]::ParseExact($strExif, 'yyyy:MM:dd HH:mm:ss', $null)
# Add to multidimensional array: [0]=current_file and [1]=datetime
$arrSortMe += ,@($_, $dateExif)
}
Catch {}
}
Write-host "DateTimeTaken" `t`t`t "New Name" `t`t`t "Old Fullname"
# Sort array by datetime and pipe the sorted array to a foreach loop
$arrSortMe | Sort-Object @{Expression={$_[1]} } | ForEach {$i=0}{
# Increment a simple counter starting with 0, so the first image gets the "1"
$i++
# Format the counter to 3-digits, append an underscore followed by the old image name
$newName = $i.ToString("000") + "_" + $_[0].Name
# Don't rename images which already have three digits as filename start
If ($_.Name -notmatch "^[\d]{3}_") {
#Rename-Item $_[0].Fullname $newName
}
# Only for debugging
Write-host $_[1].Date `t $newName `t $_[0].Fullname
}
# Clear all variables. System variables are readonly so we don't mind using * to get all
Remove-Variable * -ErrorAction SilentlyContinue
使用資源
- http://www.awaresystems.be/imaging/tiff/tifftags/privateifd/exif.html
- http://blogs.technet.com/b/jamesone/archive/2007/07/13/exploring-photographic-exif-data-using-powershell-of-course.aspx
- http://blog.cincura.net/233463-renaming-files-based-on-exif-data-in-powershell/
- http://www.happysysadm.com/2011/01/multiDimension-arrays-in-powershell.html
- http://www.orcsweb.com/blog/james/fun-with-powershell-sorting-multiDimension-arrays-and-arraylists/
- http://blogs.technet.com/b/heyscriptingguy/archive/2008/06/02/hey-scripting-guy-how-can-i-use-leading-zeroes-when-displaying-a-value-in- windows-powershell.aspx