问题描述
我对Powershell脚本有疑问。我想重命名文件夹中的一堆照片。我有一个.csv文件,其中包含旧名称和新名称。这是该文件的一部分:
OldFile NewFile {5858AA5A-DB1B-475A-808E-0BFF0B885E5B}.jpeg 975NNNN-AGUIRRESUGaraSSOCSTACK-Notes-20200828.jpeg {FA1E4CEE-0AD8-4B40-A5AD-4BB22C0EE4F0}.jpeg 975NNNN-AGUIRRESUGaraSSOCSTACK-Other-20200828.jpeg {FD20FA44-B3D2-4A6A-B73D-F3BADC2DDE71}.jpeg 975NNNN-AGUIRRESUGaraSSOCSTACK-Vicinity-20200831.jpeg {E0DDA4CD-7783-417C-9BE0-705FFA08CD17}.jpeg 975NNNN-AGUIRRESUGaraSSOCSTACK-Vicinity-20200831.jpeg {76DC6315-942D-444C-BA04-92FC9B9FF1A5}.jpeg 975NNNN-AGUIRRESUGaraSSOCSTACK-Vicinity-20200831.jpeg {3C853453-0A0D-40B5-B3B7-B0F84F92D512}.jpeg 975NNNN-AGUIRRESUGaraSSOCSTACK-Vicinity-20200831.jpeg
许多新文件名将重复。对于这些文件,我想在名称中间的确切位置添加一个字母(A,B,C等)。
例如,如果文件975NNNN-AGUIRRESUGaraSSOCSTACK-Vicinity-20200831.jpeg是重复的,我想在“ Vicinity”之后添加“ A”,因此该文件称为975NNNN-AGUIRRESUGaraSSOCSTACK-VicinityA-20200831 .jpeg。该字母将始终位于相同的位置(紧接在第三个-之前)。
这是我到目前为止的脚本。我知道这是不对的,而且我什至无法尝试在脚本中添加字母。 (我是Powershell的新手。)
$filesToRename = Import-CSV C:\Users\clair\OneDrive\Documents\JOA\batch_photos\Rename_Central_Aguirre.csv
foreach ($file In $filesToRename) {
if (Test-Path $file.NewFile) {
$letter = -begin { $count= 1 } -Process { Rename-Item $file.OldFile
"file-$([char](96 + $count)).jpeg"; $count++}
} else {
Rename-Item $file.OldFile $file.NewFile
}
}
谢谢!
解决方法
使用字母中的字符重命名文件时,意味着您只有26个选项。如果这足以满足您的需求,则可以执行以下操作:
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$folderPath = 'D:\Test'
$filesToRename = Import-CSV C:\Users\clair\OneDrive\Documents\JOA\batch_photos\Rename_Central_Aguirre.csv
foreach ($file In $filesToRename) {
$oldFile = Join-Path -Path $folderPath -ChildPath $file.OldFile
if (Test-Path $oldFile -PathType Leaf) {
# split the new filename into workable parts
$newName = $file.NewFile
$extension = [System.IO.Path]::GetExtension($newName)
$parts = [System.IO.Path]::GetFileNameWithoutExtension($newName) -split '-'
$suffix = $parts[-1]
$prefix = $parts[0..($parts.Count -2)] -join '-'
$charToAppend = 0 # counter to go through the characters in the alphabet. 0..25
while (Test-Path (Join-Path -Path $folderPath -ChildPath $newName) -PathType Leaf) {
if ($charToAppend -gt 25) {
# bail out if al characters have been used up
throw "Cannot rename file '$($file.OldFile)',because all characters A-Z are already used"
}
$newName = '{0}{1}-{2}{3}' -f $prefix,$alphabet[$charToAppend++],$suffix,$extension
}
Rename-Item -Path $oldFile -NewName $newName
}
else {
Write-Warning "File '$($file.OldFile)' not found"
}
}
之前:
D:\TEST
{3C853453-0A0D-40B5-B3B7-B0F84F92D512}.jpeg
{5858AA5A-DB1B-475A-808E-0BFF0B885E5B}.jpeg
{76DC6315-942D-444C-BA04-92FC9B9FF1A5}.jpeg
{E0DDA4CD-7783-417C-9BE0-705FFA08CD17}.jpeg
{FA1E4CEE-0AD8-4B40-A5AD-4BB22C0EE4F0}.jpeg
{FD20FA44-B3D2-4A6A-B73D-F3BADC2DDE71}.jpeg
之后:
D:\TEST
975NNNN-AGUIRRESUGARASSOCSTACK-Notes-20200828.jpeg
975NNNN-AGUIRRESUGARASSOCSTACK-Other-20200828.jpeg
975NNNN-AGUIRRESUGARASSOCSTACK-Vicinity-20200831.jpeg
975NNNN-AGUIRRESUGARASSOCSTACK-VicinityA-20200831.jpeg
975NNNN-AGUIRRESUGARASSOCSTACK-VicinityB-20200831.jpeg
975NNNN-AGUIRRESUGARASSOCSTACK-VicinityC-20200831.jpeg
,
我认为您需要使用method.Insert()。这是一个如何工作的小例子:
我已经在C:\ Test中创建了一个名为975NNNN-AGUIRRERM1-Vicinity-20200829.txt的文本,仅用于测试目的,在您的示例中,第一行代码是标识重复项
#Code to identify duplicates (insert your code instead of mine)
$files=Get-ChildItem -Path C:\Test -File -Name
#The following line indentifies the location of the last "-" (I understand you always have 3 "-" right?)
$DashPos=($files).LastIndexOf("-")
#This inserts on the position $DashPos,the letter "A")
$files.Insert($DashPos,"A")