Thursday, March 3, 2011

Moving photos from a camera card with AutoHotkey

Just wanted to share a somewhat rough script I wrote using AutoHotkey (since my Python skills are weak and Windows-only is fine) for moving files from a camera SD card to folders organized by month.

I also set up an AutoRun.inf to run the compiled script when the card is inserted, similar to these instructions.


; AutoHotkey Version: 1.x
; Language:       English
;
; Script Function:
; Moving files from camera card (E:\DCIM\{etc}) to photos folder (H:\Photos\{date}) and then deleting them
; You may want to change H:\Photos\ throughout the code to something that actually exists on your computer.
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.

; Since the script is running from the camera card, we can use the variable A_ScriptDir to tell which drive letter the card got mounted as
SetWorkingDir %A_ScriptDir%\DCIM\

IfNotExist %DriveLetter%:\DCIM\
{
 MsgBox, 1, PhotoMover, Something is wrong, perhaps the camera card is not in the card reader.
 IfMsgBox, Cancel
  Return
}

; show a (sort of fake) progress bar and set the initial variables to zero
Progress, b,, Moving Photos, Moving Photos
Progress, 0
PhotoProgress = 0
VideoProgress = 0

; move the JPG files
; the 0 means don't include folder names, the 1 means recurse into subdirectories
Loop, *.JPG, 0, 1
{
; trim the date variable to the format YYYY-MM
StringLeft, FileDateYear, A_LoopFileTimeCreated, 4
FileDateMonth := SubStr(A_LoopFileTimeCreated, 5, 2)
FileDateYearDashMonth = %FileDateYear%-%FileDateMonth%

; make a month folder, if it doesn't already exist
IfNotExist H:\Photos\%FileDateYearDashMonth%\
 FileCreateDir H:\Photos\%FileDateYearDashMonth%\

; move the file to the appropriate folder in H:\Photos
FileMove, %A_LoopFileFullPath%, H:\Photos\%FileDateYearDashMonth%\

; Show (and increment) a progress bar
PhotoProgress+=1 ; increment the PhotoProgress variable
Progress, %PhotoProgress%
}

Progress, Off
if ErrorLevel
MsgBox, Well that didn't work... %ErrorCount% photos were not moved.

; and now move the MOV files
; first the progress bar
Progress, b,, Moving Videos, Moving Videos
Progress, 0

; the 0 means don't include folder names, the 1 means recurse into subdirectories
; we should probably do this for *.AVI files too
Loop, *.MOV, 0, 1
{
; trim the date variable to the format YYYY-MM
StringLeft, FileDateYear, A_LoopFileTimeCreated, 4
FileDateMonth := SubStr(A_LoopFileTimeCreated, 5, 2)
FileDateYearDashMonth = %FileDateYear%-%FileDateMonth%

; move the file to the appropriate folder in H:\Photos
FileMove, %A_LoopFileFullPath%, H:\Photos\%FileDateYearDashMonth%\

; Show (and increment) an approximate progress bar
VideoProgress+=1 ; increment the VideoProgress variable
Progress, %VideoProgress%
}

Progress, Off
if ErrorLevel
MsgBox, Well that didn't work... %ErrorCount% movies were not moved.

No comments: