Skip to content

Commit b00d4e2

Browse files
Addition of photo organiser script
A symptom of the proliferation of digital cameras and photo formats, this script organises images based on date taken.
1 parent 1bd6ea3 commit b00d4e2

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
Param ( [Parameter(Mandatory=$True,Position=1)] [string]$psdir )
2+
$erroractionpreference = "SilentlyContinue"
3+
Clear-Host
4+
5+
<#
6+
7+
.SYNOPSIS
8+
Script is designed to organise pictures into folders using the date-taken.
9+
10+
.DESCRIPTION
11+
Script organises pictures into folders using the date the photo was taken. It requires installation of EXIFutils. The script extracts the date-taken from the EXIF properties of the picture and uses this date to create a folder. The picture is MOVED once the new folder is created.
12+
13+
EXIFutils v3.0 (non-licensed version) was used when building this script. exiflist is called for each file processed due to the limitations set for the non-licensed version of EXIFutils.
14+
15+
The author takes no responsibility for correct functioning of the script with any other versions of EXIFutils.
16+
17+
***It is STRONGLY recommended you back-up your pictures before using this script.***
18+
19+
.NOTES
20+
File Name : JT_EXIFutils_date-taken.ps1
21+
Author : Justin Townsend
22+
Prerequisite : EXIFutils v3.0
23+
24+
.LINK
25+
EXIFutils available at:
26+
http://www.hugsan.com/EXIFutils/
27+
28+
.SETTINGS
29+
Setting parameters, processing directories and other script properties.
30+
31+
1. Input Parameters (see line 1)
32+
2. Photo log file
33+
3. Logging function (fn_plog)
34+
4. Picture processing directories
35+
36+
#>
37+
38+
# 2. Photo log file.
39+
$stamp = get-date -format yyyy.MMM.dd.HH.mm.ss
40+
$plog = "myphotos_" + $stamp + ".log"
41+
new-item -path . -name $plog -type file
42+
43+
# 3. Logging function
44+
function fn_plog ([string]$msg)
45+
{
46+
$logstamp = get-date -format "yyyy.MMM.dd HH:mm:ss.fff"
47+
"$logstamp :> $msg" | out-file $plog -Append
48+
}
49+
50+
fn_plog "Photo log created."
51+
52+
# 4. Picture processing directories (source-parent, target-parent, dump)
53+
# $ptdir = $psdir
54+
# $ptdir += "\Photos"
55+
56+
$ptdir = "C:\Users\Justin\Pictures\Photos"
57+
58+
if ( test-path $ptdir ) # Target directory
59+
{
60+
fn_plog "$ptdir present."
61+
}
62+
else
63+
{
64+
new-item $ptdir -type directory
65+
fn_plog "$ptdir created."
66+
}
67+
68+
$ddir = $ptdir
69+
$ddir += "\1_UNPROCESSED"
70+
71+
if ( test-path $ddir ) #Dump directory
72+
{
73+
fn_plog "$ddir present."
74+
}
75+
else
76+
{
77+
new-item $ddir -type directory
78+
fn_plog "$ddir created."
79+
}
80+
81+
<#
82+
Picture processing.
83+
84+
1. If date-taken is <NULL> or not present, move file to dump directory.
85+
2. If date-taken is present, create folder and move picture file.
86+
87+
#>
88+
89+
foreach ($pic in get-childitem -path $psdir -include *.JPG, *.jpeg, *.jpg -recurse | get-itemproperty | select-object -ExpandProperty fullname)
90+
91+
# Pictures Date Taken
92+
# { exiflist /o l /f date-taken $pic }
93+
94+
{
95+
$PSName = $pic | get-itemproperty | select-object -ExpandProperty PSChildName
96+
$fdir = $ddir + "\" + $PSName
97+
98+
$erroractionpreference = "SilentlyContinue"
99+
$dt = exiflist /o l /f date-taken $pic
100+
$cmake = exiflist /o l /f make $pic
101+
$cmodel = exiflist /o l /f model $pic
102+
103+
$obj = new-object PSObject
104+
$obj | add-member Noteproperty Filename $PSNAME
105+
$obj | add-member Noteproperty Camera_Make $cmake
106+
$obj | add-member Noteproperty Camera_Model $cmodel
107+
$obj | add-member Noteproperty Date_Taken $dt
108+
109+
write-output "Processing..."
110+
write-output $obj
111+
$erroractionpreference = "SilentlyContinue"
112+
113+
if ( $dt )
114+
{
115+
$fn = exiflist /o l /f file-name $pic
116+
$y = $dt.substring(0,4)
117+
$d = $dt.substring(0,10) -replace ":", "_"
118+
119+
$y = $ptdir + "\" + $y
120+
$d = $y + "\" + $d
121+
122+
if ( test-path $d )
123+
{
124+
move-item $pic $d
125+
fn_plog "INFO $fn : Moved to $d."
126+
}
127+
elseif ( test-path $y )
128+
{
129+
new-item $d -type directory #test
130+
move-item $pic $d
131+
fn_plog "INFO $fn : $d created."
132+
fn_plog "INFO $fn : Moved to $d."
133+
}
134+
else
135+
{
136+
new-item $y -type directory
137+
new-item $d -type directory
138+
move-item $pic $d
139+
fn_plog "INFO $fn : $y created, $d created."
140+
fn_plog "INFO $fn : Moved to $d."
141+
}
142+
}
143+
elseif ( test-path $fdir )
144+
{
145+
$stamp = get-date -format yyyy.MMM.dd.HH.mm.ss
146+
$fdir += "_$stamp"
147+
move-item $pic $fdir
148+
fn_plog "ERROR $PSName : EXIF date-taken not available. Moving to $ddir."
149+
fn_plog "ERROR $PSName : Moved to $fdir because $PSName already exists!"
150+
}
151+
else
152+
{
153+
fn_plog "ERROR $PSName : EXIF date-taken not available. Moving to $ddir."
154+
$fdir = $ddir + "\" + $PSName
155+
move-item $pic $ddir
156+
fn_plog "ERROR $PSName : Moved to $ddir."
157+
158+
}
159+
}
160+
161+
# Success Rate
162+
$err_cnt = @(get-childitem -recurse -path $ddir).Count
163+
$all_cnt = @(get-childitem -recurse -path $ptdir).Count
164+
$succ_cnt = $all_cnt - $err_cnt
165+
$succ_rate = ($succ_cnt / $all_cnt) * 100
166+
167+
fn_plog "Successes : $succ_cnt"
168+
fn_plog "Failures : $err_cnt"
169+
fn_plog "Success Rate (%) : $succ_rate"
170+
171+
fn_plog "$err_cnt pictures couldn't be processed. You can find them in $ddir."
172+
fn_plog "Consider moving them to folders using other information (e.g. date-created, date-modified)."
173+
174+
$p_obj = new-object PSObject
175+
$p_obj | add-member Noteproperty Files_Processed $all_cnt
176+
$p_obj | add-member Noteproperty Errors $err_cnt
177+
$p_obj | add-member Noteproperty Successes $succ_cnt
178+
$p_obj | add-member Noteproperty Success_Rate $succ_rate
179+
180+
write-output "***** SUCCESS RATE *****"
181+
write-output $p_obj
182+
183+
remove-item $p_obj

0 commit comments

Comments
 (0)