-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort-Images.ps1
54 lines (47 loc) · 1.9 KB
/
Sort-Images.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
. .\Get-Image.ps1
. .\Get-ImageProperty.ps1
$SourceFolder = "E:\Test\Input\"
$TargetFolder = "E:\Test\Output\"
Get-ChildItem -File -Recurse -Path $SourceFolder -Include *.JPG | ForEach-Object {
$orig_path = $_.FullName
$img_prop = Get-Image $_.FullName | Get-ImageProperty
if ($img_prop.dt) {
$date = $img_prop.dt.ToString("yyyy-MM")
$target_part = (Join-Path $TargetFolder $date)
# Make folder is not exist
if (-not (Test-Path $target_part)) {
New-Item -ItemType Directory -Path $target_part
}
# Check file already exist
$full_file_path = Join-Path $target_part $_.Name
if (-not (Test-Path $full_file_path)) {
}
else {
$add = Get-Random -Maximum 10000 # or Get-Date -Format "ddMMyyyyHHmmss"
$new_filename = $_.BaseName + "-" + $add + $_.Extension
$target_part = Join-Path (Join-Path $TargetFolder $date) $new_filename
}
"Move image '$orig_path' to '$target_part'"
Move-Item -Path $_.FullName -Destination $target_part
}
else {
Write-Host "No date for $orig_path"
$target_folder = Join-Path $TargetFolder "UnknowDate"
if (-not (Test-Path $target_folder)) {
New-Item -ItemType Directory -Path $target_folder
}
$full_file_path = Join-Path $target_folder $_.Name
if (Test-Path $full_file_path) {
$add = Get-Random -Maximum 10000
$new_filename = $_.BaseName + "-" + $add + $_.Extension
$target_path = Join-Path $target_folder $new_filename
Move-Item -Path $_.FullName -Destination $target_path
"Move image '$orig_path' to '$target_path'"
}
else {
Move-Item -Path $_.FullName -Destination $target_folder
"Move image '$orig_path' to '$target_folder'"
}
}
Clear-Variable -Name img_prop
}