Sunday, May 22, 2011
powershell rename file group to sequence
I have one of those camera's that can't keep consistent file names. So I was manually incrementing file names, not fun. So I tired some powershell. Beginning with some searching the closest I came to what I wanted was on this blog http://geekswithblogs.net/tmurphy/archive/2007/07/18/Renaming-Groups-of-Files-With-PowerShell.aspx
This didn't seem to work for my version of powershell and the max file name needed the full file name instead of just a "StartsWith". So I made some modifications:
I needed the preset to rename all files that started with 00 to a 5 digit number. This number would start just over the number of the last time I did a rename. This "just over" number I have to hard code into the script file.
This didn't seem to work for my version of powershell and the max file name needed the full file name instead of just a "StartsWith". So I made some modifications:
$maxname = @(get-childitem -path $picpath | where {($_.Name.StartsWith($maxname))})[0].Name #@() turns it to an array even if it's just a single fileThat $($script:idx++;$script:idx) means run the increment then return that incremented value for display. Now the script worked great, except every time I ran the script I wasn't going to remember what all these parameters meant and didn't want to relearn. So I added a new case to the to the top to handle 0 parameters. The idea was I'd put this script in the root folder of my pictures. I'd prompt the user for the relative path, the folder name that needed the renaming. Then everything else would be already pre-set in the script.
$prefix = $Args[3]
Write-Host "renaming $minname to $maxname with $prefix#$($(@(get-childitem -path $picpath | where {($_.Name.StartsWith($maxname))})[0].Extension))"
get-childitem -path $picpath | where -filterscript {($_.Name -ge $minname) -and ($_.Name -le $maxname)} | foreach -process {ren -path $_.FullName -NewName "$prefix$($script:idx++;$script:idx)$($_.Extension)"}
I needed the preset to rename all files that started with 00 to a 5 digit number. This number would start just over the number of the last time I did a rename. This "just over" number I have to hard code into the script file.
if($Args.Length -lt 1)That did the trick for me. Naming files that were 00000.ext, 00001.ext as 00128.ext, 00129.ext. If you want to do this with files that start differently you can use the Windows 7 rename functionality to get them to start similarly. I would like the script to parse itself and update the $idx itself, but that's a small benefit for jumping into some deep water.
{
#use shawn's naming for movies from the ZS7 camera
$idx = 127 #this is exactly the last counter value, not +1. nameing starts with +1
$prefix = if ($idx -le 99) {"000"} elseif ($idx -le 999) {"00"} else {"0"}
$folder = Read-Host "Movies will start at $($idx + 1). Enter folder name where movies are stored"
$picpath = ".\$folder"
$minname = "00"
$allfiles = @(get-childitem -path $picpath | where {($_.Name.StartsWith($minname)) -and ($_.Name -le "$prefix$($idx - 10)")}) #don't select the last 10 renamed files. the -10 isn't safe from idx 100 to 109 due to $prefix not matching on the -10 value.
$maxname = $allfiles[$($allfiles.Length - 1)].Name #get the last alphabetic file that matches $minname
get-childitem -path $picpath | where -filterscript {($_.Name -ge $startwith) -and ($_.Name -le $maxname)} | foreach -process {ren -path $_.FullName -NewName "$prefix$($script:idx++;$script:idx)$($_.Extension)"}
}
Labels: powershell, rename
Subscribe to Posts [Atom]