canon camera 백업 스크립트(년, 월, 일)
페이지 정보

본문
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
function Get-ExifDate($filePath) {
try {
$shell = New-Object -ComObject Shell.Application
$folder = $shell.Namespace((Split-Path $filePath))
$item = $folder.ParseName((Split-Path $filePath -Leaf))
$dateTaken = $folder.GetDetailsOf($item, 12)
if ($dateTaken -and $dateTaken -match "\d{4}[-/]\d{2}[-/]\d{2}") {
return [datetime]::Parse($dateTaken)
}
} catch {}
return (Get-Item $filePath).CreationTime
}
$extensions = @(".jpg", ".cr2", ".mov", ".mp4")
$destRoot = "D:\canon"
mkdir $destRoot -Force | Out-Null
$shell = New-Object -ComObject Shell.Application
$camera = $shell.Namespace(17).Items() | Where-Object { $_.Name -eq "Canon EOS 50D" }
$cf = $camera.GetFolder.Items() | Where-Object { $_.Name -eq "CF" } |
ForEach-Object { $_.GetFolder.Items() | Where-Object { $_.Name -eq "DCIM" } } |
ForEach-Object { $_.GetFolder.Items() | Where-Object { $_.Name -eq "101CANON" } } |
ForEach-Object { $_.GetFolder }
# 1) 먼저 카메라의 모든 대상 파일 리스트를 만듦
$allItems = $cf.Items() | Where-Object { !$_.IsFolder -and $extensions -contains (($_.Name) -replace ".*(\..+)$", '$1').ToLower() }
# 2) 임시 복사 없이, 백업 대상인지 판단하기 위해
# 각 파일의 날짜와 예상 백업 경로 계산
$filesToCopy = @()
foreach ($item in $allItems) {
# 임시경로 없이 우선 날짜 계산 (임시복사 후 정확한 날짜 계산 가능하니 여기서는 대략 처리)
# EXIF 읽으려면 임시 복사 필요하므로 CreationTime으로 먼저 판단
# 정확한 날짜는 복사 중 재계산 가능
$fileName = $item.Name
# 임시로 CreationTime 기준 날짜 가정 (정확한 EXIF는 복사 후 재계산)
$dateGuess = (Get-Date).Date # 기본 오늘 날짜, 안전을 위해
# 실제 정확한 날짜 산출은 복사 중 처리
# 대상 폴더 예측 (하위 날짜 폴더는 임시로 오늘 날짜로 지정)
$targetDirGuess = Join-Path $destRoot "$($dateGuess.Year)\$("{0:D2}" -f $dateGuess.Month)\$("{0:D2}" -f $dateGuess.Day)"
$targetPathGuess = Join-Path $targetDirGuess $fileName
# 백업 폴더 내에 동일 이름 파일 존재하면 제외
if (-not (Test-Path $targetPathGuess)) {
$filesToCopy += $item
}
}
# 3) GUI 세팅
$font = New-Object System.Drawing.Font("Malgun Gothic", 12, [System.Drawing.FontStyle]::Regular)
$form = New-Object Windows.Forms.Form
$form.Text = "Canon Photo Backup"
$form.Width = 500
$form.Height = 150
$form.StartPosition = "CenterScreen"
$form.Font = $font
$label = New-Object Windows.Forms.Label
$label.Text = "Preparing files..."
$label.AutoSize = $true
$label.Location = New-Object Drawing.Point(20, 20)
$label.Font = $font
$form.Controls.Add($label)
$progress = New-Object Windows.Forms.ProgressBar
$progress.Location = New-Object Drawing.Point(20, 60)
$progress.Width = 440
$progress.Style = 'Continuous'
$form.Controls.Add($progress)
$form.Show()
[System.Windows.Forms.Application]::DoEvents()
# 4) 실제 복사 시작 - 정확한 EXIF 날짜로 다시 판단하며 복사 진행
$progress.Maximum = $filesToCopy.Count
$index = 0
foreach ($item in $filesToCopy) {
$index++
$label.Text = "Processing: $($item.Name) ($index of $($filesToCopy.Count))"
$progress.Value = $index
[System.Windows.Forms.Application]::DoEvents()
$tmpPath = "$env:TEMP\$($item.Name)"
$shell.Namespace($env:TEMP).CopyHere($item)
while (-not (Test-Path $tmpPath)) {
Start-Sleep -Milliseconds 200
}
try {
$ext = [System.IO.Path]::GetExtension($tmpPath).ToLower()
if ($ext -in @(".jpg", ".cr2")) {
$date = Get-ExifDate $tmpPath
} else {
$date = (Get-Item $tmpPath).CreationTime
}
$targetDir = Join-Path $destRoot "$($date.Year)\$("{0:D2}" -f $date.Month)\$("{0:D2}" -f $date.Day)"
mkdir $targetDir -Force | Out-Null
$targetPath = Join-Path $targetDir $item.Name
if (-not (Test-Path $targetPath)) {
Move-Item $tmpPath $targetPath
} else {
Remove-Item $tmpPath
}
} catch {
Write-Warning "Error: $($_.Exception.Message)"
if (Test-Path $tmpPath) { Remove-Item $tmpPath -ErrorAction SilentlyContinue }
}
}
$label.Text = "✅ Backup complete! You can close this window."
$progress.Value = $progress.Maximum
[System.Windows.Forms.Application]::DoEvents()
Add-Type -AssemblyName System.Drawing
function Get-ExifDate($filePath) {
try {
$shell = New-Object -ComObject Shell.Application
$folder = $shell.Namespace((Split-Path $filePath))
$item = $folder.ParseName((Split-Path $filePath -Leaf))
$dateTaken = $folder.GetDetailsOf($item, 12)
if ($dateTaken -and $dateTaken -match "\d{4}[-/]\d{2}[-/]\d{2}") {
return [datetime]::Parse($dateTaken)
}
} catch {}
return (Get-Item $filePath).CreationTime
}
$extensions = @(".jpg", ".cr2", ".mov", ".mp4")
$destRoot = "D:\canon"
mkdir $destRoot -Force | Out-Null
$shell = New-Object -ComObject Shell.Application
$camera = $shell.Namespace(17).Items() | Where-Object { $_.Name -eq "Canon EOS 50D" }
$cf = $camera.GetFolder.Items() | Where-Object { $_.Name -eq "CF" } |
ForEach-Object { $_.GetFolder.Items() | Where-Object { $_.Name -eq "DCIM" } } |
ForEach-Object { $_.GetFolder.Items() | Where-Object { $_.Name -eq "101CANON" } } |
ForEach-Object { $_.GetFolder }
# 1) 먼저 카메라의 모든 대상 파일 리스트를 만듦
$allItems = $cf.Items() | Where-Object { !$_.IsFolder -and $extensions -contains (($_.Name) -replace ".*(\..+)$", '$1').ToLower() }
# 2) 임시 복사 없이, 백업 대상인지 판단하기 위해
# 각 파일의 날짜와 예상 백업 경로 계산
$filesToCopy = @()
foreach ($item in $allItems) {
# 임시경로 없이 우선 날짜 계산 (임시복사 후 정확한 날짜 계산 가능하니 여기서는 대략 처리)
# EXIF 읽으려면 임시 복사 필요하므로 CreationTime으로 먼저 판단
# 정확한 날짜는 복사 중 재계산 가능
$fileName = $item.Name
# 임시로 CreationTime 기준 날짜 가정 (정확한 EXIF는 복사 후 재계산)
$dateGuess = (Get-Date).Date # 기본 오늘 날짜, 안전을 위해
# 실제 정확한 날짜 산출은 복사 중 처리
# 대상 폴더 예측 (하위 날짜 폴더는 임시로 오늘 날짜로 지정)
$targetDirGuess = Join-Path $destRoot "$($dateGuess.Year)\$("{0:D2}" -f $dateGuess.Month)\$("{0:D2}" -f $dateGuess.Day)"
$targetPathGuess = Join-Path $targetDirGuess $fileName
# 백업 폴더 내에 동일 이름 파일 존재하면 제외
if (-not (Test-Path $targetPathGuess)) {
$filesToCopy += $item
}
}
# 3) GUI 세팅
$font = New-Object System.Drawing.Font("Malgun Gothic", 12, [System.Drawing.FontStyle]::Regular)
$form = New-Object Windows.Forms.Form
$form.Text = "Canon Photo Backup"
$form.Width = 500
$form.Height = 150
$form.StartPosition = "CenterScreen"
$form.Font = $font
$label = New-Object Windows.Forms.Label
$label.Text = "Preparing files..."
$label.AutoSize = $true
$label.Location = New-Object Drawing.Point(20, 20)
$label.Font = $font
$form.Controls.Add($label)
$progress = New-Object Windows.Forms.ProgressBar
$progress.Location = New-Object Drawing.Point(20, 60)
$progress.Width = 440
$progress.Style = 'Continuous'
$form.Controls.Add($progress)
$form.Show()
[System.Windows.Forms.Application]::DoEvents()
# 4) 실제 복사 시작 - 정확한 EXIF 날짜로 다시 판단하며 복사 진행
$progress.Maximum = $filesToCopy.Count
$index = 0
foreach ($item in $filesToCopy) {
$index++
$label.Text = "Processing: $($item.Name) ($index of $($filesToCopy.Count))"
$progress.Value = $index
[System.Windows.Forms.Application]::DoEvents()
$tmpPath = "$env:TEMP\$($item.Name)"
$shell.Namespace($env:TEMP).CopyHere($item)
while (-not (Test-Path $tmpPath)) {
Start-Sleep -Milliseconds 200
}
try {
$ext = [System.IO.Path]::GetExtension($tmpPath).ToLower()
if ($ext -in @(".jpg", ".cr2")) {
$date = Get-ExifDate $tmpPath
} else {
$date = (Get-Item $tmpPath).CreationTime
}
$targetDir = Join-Path $destRoot "$($date.Year)\$("{0:D2}" -f $date.Month)\$("{0:D2}" -f $date.Day)"
mkdir $targetDir -Force | Out-Null
$targetPath = Join-Path $targetDir $item.Name
if (-not (Test-Path $targetPath)) {
Move-Item $tmpPath $targetPath
} else {
Remove-Item $tmpPath
}
} catch {
Write-Warning "Error: $($_.Exception.Message)"
if (Test-Path $tmpPath) { Remove-Item $tmpPath -ErrorAction SilentlyContinue }
}
}
$label.Text = "✅ Backup complete! You can close this window."
$progress.Value = $progress.Maximum
[System.Windows.Forms.Application]::DoEvents()
댓글목록

최고관리자님의 댓글
최고관리자 아이피 (192.♡.0.1) 작성일
@echo off
powershell -NoProfile -ExecutionPolicy RemoteSigned -File "C:\Users\assdd\Desktop\canon_backup.ps1"
pause
