/
Organize-Pictures.ps1
189 lines (129 loc) · 5.19 KB
/
Organize-Pictures.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<#
.SYNOPSIS
Organize pictures in your OneDrive camera roll folder
.DESCRIPTION
Organize pictures in your OneDrive pictures folder
Creates folders named after the year and month, then moves picture files to them
Default to your OneDrive camera roll
.NOTES
File Name : Organize-Pictures.PS1
Authors : Jose Barreto
Version : 1.0 (09/2/2016)
.LINK
To provide feedback visit https://blogs.technet.microsoft.com/josebda/
.EXAMPLE
.\Organize-Pictures.ps1
Organizes all OneDrive camera roll for the current user.
Uses folder C:\Users\<user>\OneDrive\Pictures\Camera Roll
.EXAMPLE
Organize-Pictures.ps1 -Path "C:\Pictures\Camera Roll"
Organizes all pictures in the specified folder.
.EXAMPLE
Organize-Pictures.ps1 -Path "D:\Camera Roll" -Confirm $false -RemoveEmptyFolders $false
Organizes all pictures in the specified folder.
Does not prompt for confirmation. Does not remove empty folders.
#>
[CmdletBinding(DefaultParameterSetName="Default")]
# Define parameters and defaults
param(
[parameter(ParameterSetName="Default", Position=0, Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string] $Path = $($env:userprofile + "\OneDrive\Pictures\Camera Roll"),
[parameter(ParameterSetName="Default", Position=1, Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string] $Confirm = $true,
[parameter(ParameterSetName="Default", Position=2, Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string] $RenameIfExists = $true,
[parameter(ParameterSetName="Default", Position=3, Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string] $RemoveEmptyFolders = $true
)
# Set strict mode to check typos on variable and property names
Set-StrictMode -Version Latest
# Shows error, cancels script
Function ShowError {
Param ([string] $Message)
$Message = $Message + “ – cmdlet was cancelled”
Write-Error $Message -ErrorAction Stop
}
# Checks if the Path provided is valid
if ($Path -eq "") {
ShowError("Path cannot be blank")
}
if ($Path.EndsWith("\")) {
$Path = $Path.Substring(0, $Path.Length-1)
}
if (-not (Test-Path $Path)) {
ShowError("Invalid Path - "+$Path)
}
# Asks for confirmation
if ($Confirm) {
Cls
Write-Host "This script will organize files at <$Path> and all subfolders."
Write-Host "Files will be moved to a folder named after the year and month the file was last written."
Write-Host "This operation cannot be easily undone."
Write-Host ""
if ($RenameIfExists -eq $true ) { $action = "renamed" } else { $action = "skipped" }
Write-Host "Existing files at destination will be $action."
if ($RemoveEmptyFolders -eq $true) { Write-Host "Empty folders after the operation will be removed automatically." }
Write-Host ""
Write-Host -ForegroundColor Yellow -NoNewline "Are you sure you want to proceed (Y/N)? "
$Answer = Read-Host
if ($Answer.ToUpper() -ne "Y") {
ShowError("User did not confirm the operation")
}
}
# Moves pictures to the new location
$FilesMoved = 0
$FilesSkipped = 0
$FoldersRemoved = 0
dir $Path -Recurse -File | % {
# Get file and folder information
$oldname = $_.FullName
$newsubfolder = "\"+$_.LastWriteTime.Year +"\" + $_.LastWriteTime.Month.ToString().PadLeft(2, "0")
$newname = $Path + $newsubfolder + "\" + $_.Name
# Need to move the file
if ($newname -ne $oldname) {
# Create the new folder if necessary
$newfolder = $newname.Substring(0, $newname.LastIndexOf("\"))
MD $newfolder -ErrorAction SilentlyContinue | Out-Null
# File already exists
$SkipFile = $false
if (Test-Path $newname) {
if ($RenameIfExists -eq $true) {
# Rename the file
Write-Host "File <$newname> already exists at destination. Renaming to avoid conflict."
$newname = $newname.Replace(".", "-" + (New-Guid).Guid + ".")
} else {
Write-Host "File <$newname> already exists at destination. Skipping."
$SkipFile = $true
$FilesSkipped++
}
}
# Move the file
if ($SkipFile -eq $false) {
"Moving to $newname"
$_.MoveTo($newname)
$FilesMoved++
}
}
}
# Removes empty folders
if ($RemoveEmptyFolders -eq $true) {
dir $Path -Recurse -Directory | sort FullName -Descending | % {
# if folder is empty
$dirname = $_.FullName
if ((dir $dirname -Recurse) -eq $null) {
# remove folder
Write-Host "Directory <$dirname> is empty - removing"
$_.Delete();
$FoldersRemoved++
}
}
}
# Final summary
Write-Host ""
Write-Host "$FilesMoved files moved."
if ($RenameIfExists -eq $false) { Write-Host "$FilesSkipped files skipped." }
if ($RemoveEmptyFolders -eq $true) { Write-Host "$FoldersRemoved empty folders." }