AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

rename photo.ahk

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
kardus



Joined: 17 Jul 2008
Posts: 14

PostPosted: Wed Aug 06, 2008 8:41 pm    Post subject: rename photo.ahk Reply with quote

I use these script for renaming photo; for me they are invaluable, but I'm not sure if someone will find it useful ...
Code:
/*
This script to rename multiple files of photo taken from varios camera that reside in the same directory;
they are of differend kind (some camera shoot in .JPG, other .TIF), and in some case they have corresponding
RAW (e.g.: .CR2) or .WAV file to rename too.
I want to rename them keeping the original order, but with a date structure, and sequence number from 001 to 999 (max.)
This is accomplished by FIRST RENAME part of the script.
For safety the script must be run in a dir with a "date" structure; this is used to give the name to the files
(e.g.: 2007-5-30..., or 2006-a-25..., where the letter "a" means october, "b" november, "c" december)
Let's see an example:
C:\2007-5-30\IMG_15105.JPG  renamed in 2008-4-13 001.JPG
C:\2007-5-30\IMG_15106.CR2             2008-4-13 002.CR2
C:\2007-5-30\IMG_15106.JPG             2008-4-13 002.JPG
C:\2007-5-30\IMG_15107.JPG             2008-4-13 003.JPG
C:\2007-5-30\IMG_15108.JPG             2008-4-13 004.JPG
C:\2007-5-30\IMG_15109.CR2             2008-4-13 005.CR2
C:\2007-5-30\IMG_15109.JPG             2008-4-13 005.JPG
C:\2007-5-30\IMG_15110.JPG             2008-4-13 006.JPG
C:\2007-5-30\IMG_15111.JPG             2008-4-13 007.JPG
C:\2007-5-30\IMG_15111.WAV             2008-4-13 007.WAV
C:\2007-5-30\IMG_15112.JPG             2008-4-13 008.JPG

from now, if the script is run again, the SECOND RENAME part of the script will be executed;
this is to avoid any number is missing in the renamed files (in case that you discard any of them)
For example, I don't like the photo 2008-4-13 003.JPG and I delete it: now there is a gap in the number sequence (003 is missing)
and, after SECOND RENAME:
2008-4-13 001.JPG  renamed in 2008-4-13 001.JPG (no change)
2008-4-13 002.CR2             2008-4-13 002.CR2 (no change)
2008-4-13 002.JPG             2008-4-13 002.JPG (no change)
2008-4-13 004.JPG             2008-4-13 003.JPG
2008-4-13 005.CR2             2008-4-13 004.CR2
2008-4-13 005.JPG             2008-4-13 004.JPG
2008-4-13 006.JPG             2008-4-13 005.JPG
2008-4-13 007.JPG             2008-4-13 006.JPG
2008-4-13 007.WAV             2008-4-13 006.WAV
2008-4-13 008.JPG             2008-4-13 007.JPG
*/


dir_right =
elapsed_time =
file_number =
file_number_succ =
File_Quantity =
Jpg_Quantity =
New_Name =
ren_quantity =
start_time =

SetWorkingDir %A_ScriptDir%
#SingleInstance ignore         ; attempts to launch an already-running script are ignored


/*   ; test only
Loop *.*
 File_Quantity = %A_Index%
MsgBox ,,, File_Quantity %File_Quantity%, 1   ; how many files in the dir (every type: hidden, system, read-only)
Loop *.jpg
 Jpg_Quantity = %A_Index%
MsgBox ,,, Jpg_Quantity (%Jpg_Quantity%), 1   ; how many .jpg ...
*/


; _______________________________________________ check directory validity ___________________________________
; the date portion (e.g. 2007-5-30) of the directory is extractd (only 9 character that are date)
dir_right := SubStr(A_ScriptDir, InStr(A_ScriptDir, "\", false, 0)+1, 9)

; safety: if the dir is not of kind "2007-5-30 ...", that is 5° and 7° character "-", 8° and 9° between 1 and 31
; the script terminate immediatly
if SubStr(dir_right, 5, 1) <> "-" or SubStr(dir_right, 7, 1) <> "-" or SubStr(dir_right, 8 , 2)*1 < 1 or SubStr(dir_right, 8 , 2)*1 > 31
 {MsgBox 64,, %dir_right%`nNOT a valid directory, 1.5
  ExitApp
 }

; _______________________________________________ FIRST RENAME _______________________________________________
; this is to give to the file that are photo, .JPG .TIF RAW (.CR2) the structure of the directory that, as
; checked above, has a date structure (e.g. 2007-5-30 001.jpg, 2007-5-30 002.jpg, ...)
; the FIRST RENAME is executed only if NONE file in the dir already has that date structure (safety); then the script terminate
; if instead there are files with the date structure, the SECOND_RENAME is executed and the script terminate
IfExist %dir_right%*.*
 {
  ; at least one file already has the structure: FIRST RENAME is by-passed and SECOND_RENAME is executed
  start_time := A_TickCount   ; variabile to calcolate elapsed_time when the sript terminate
  gosub WAIT
  goto SECOND_RENAME
 }
; no file has the structure. FIRST RENAME is executed and the script terminate
MsgBox 4,, Would you like to start FIRST RENAME?`n(press Yes or No)
IfMsgBox No
 ExitApp
start_time := A_TickCount     ; variabile to calcolate elapsed_time when the sript terminate
gosub WAIT
Loop *.*
if A_LoopFileAttrib contains H,R,S   ;if any file that is H (Hidden), R (Read-only), or S (System): stop
 {
  BlockInput off
  MsgBox  64,, (FIRST RENAME) STOP`n%A_LoopFileName%`nattribute %A_LoopFileAttrib%
  ExitApp
 }
; with Loop (files & folders) create a list (variable FileList) of the files to rename (base only, no extension)
; then Sort to ensure that that list is in alphabetical order and without duplicate
; NOTE: add loop below if you have photo with others extension, lower or upper case it is the same
; (e.g.: Loop *.CRW or Loop *.crw)
FileList =
Loop *.cr2
 FileList := FileList . SubStr(A_LoopFileName, 1, InStr(A_LoopFileName, ".", false, 0)-1) . "`n"
Loop *.jpg
 FileList := FileList . SubStr(A_LoopFileName, 1, InStr(A_LoopFileName, ".", false, 0)-1) . "`n"
Loop *.tif
 FileList := FileList . SubStr(A_LoopFileName, 1, InStr(A_LoopFileName, ".", false, 0)-1) . "`n"
;Loop *.crw   ; ADD OTHER EXTENSION HERE
; FileList := FileList . SubStr(A_LoopFileName, 1, InStr(A_LoopFileName, ".", false, 0)-1) . "`n"
Sort FileList, U
;FileDelete test.txt
Loop, parse, FileList, `n
 {
  if A_Index = 1000
   {
    BlockInput off
    MsgBox  64,, (FIRST RENAME) STOP`nA_Index = 1000
    ExitApp
   }
  file_number = 00%A_Index%
  StringRight file_number, file_number, 3
  ;FileAppend %A_LoopField% = %dir_right% %file_number%`n, test.txt
  FileMove %A_LoopField%.*, %dir_right% %file_number%.*
  IfExist %dir_right% %file_number%.*
    ren_quantity = %A_Index%
 }
FileList =
elapsed_time := Round((A_TickCount - start_time) / 1000, 1)
;MsgBox  64,, `n(FIRST RENAME) ExitApp`n`nrenamed (%ren_quantity%)`nin %elapsed_time% sec., 2
Progress zh0 fs12, (FIRST RENAME) ExitApp`nrenamed (%ren_quantity%)`nin %elapsed_time% sec.
sleep 2000
ExitApp



SECOND_RENAME:
Loop ; ___________________ to check if is there a gap (eg.: 004) in the number sequence of the files _________
{
 file_number = 00%A_Index%
 StringRight file_number, file_number, 3
 ;IfNotExist ????????%file_number%*.*                  ; (dismissed) for old date style (07-5-30 001.jpg)
 IfNotExist %dir_right% %file_number%*.*               ; this is for new date style (2007-5-30 001.jpg)
  {
   ;msgbox ,,, file %file_number% does not exist       ; the missing number (test only)
   Gosub RENAME
  }
} ; _____________________________________________
RENAME:
Loop                                                   ; search for the next number (after the gap) that exist
{
 file_number_succ := 00(file_number + A_Index)
 StringRight file_number_succ, file_number_succ, 3
 ;msgbox file_number %file_number%  file_number_succ %file_number_succ%  ; test only
 If (file_number + A_Index) = 1000
 {
  elapsed_time := Round((A_TickCount - start_time) / 1000, 1)
  ;MsgBox  64,, `nExitApp (SECOND_RENAME)`n`nrenamed (%ren_quantity%)`nin %elapsed_time% sec., 0.9
  Progress zh0 fs12, ExitApp (SECOND_RENAME)`nrenamed (%ren_quantity%)`nin %elapsed_time% sec.
  sleep 1000
  ExitApp
 }
 IfExist %dir_right% %file_number_succ%*.*
   break
}
;msgbox ,,, renaming!   ; test only
ren_quantity += 1
Loop %dir_right% %file_number_succ%*.*                     ; rename of the file successive to the gap
{
 StringReplace, New_Name, A_LoopFileName, %dir_right% %file_number_succ%, %dir_right% %file_number%
 ;msgbox New_Name %New_Name% A_LoopFileName %A_LoopFileName%`nfile_number_succ %file_number_succ% file_number %file_number%
 FileMove %A_LoopFileName%, %New_Name%
}
return


WAIT: ;......................................................................................................
BlockInput on
CoordMode ToolTip
ToolTip `n wait... RENAMING `n`n (BlockInput)`n, (A_ScreenWidth/4), (A_ScreenHeight/4), 3
return
Back to top
View user's profile Send private message
Guest






PostPosted: Sun Oct 26, 2008 1:26 am    Post subject: Reply with quote

it would be neat to wrap delete raw.ahk with this script or vice-versa.
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group