Skip to content

Instantly share code, notes, and snippets.

@philippgitpush
Last active June 5, 2024 18:57
Show Gist options
  • Save philippgitpush/0f7a5c2613d7c364b6baf7b3352bf820 to your computer and use it in GitHub Desktop.
Save philippgitpush/0f7a5c2613d7c364b6baf7b3352bf820 to your computer and use it in GitHub Desktop.
PowerShell script for organizing JPG files into corresponding folders within the 'JPG' directory, based on the presence of matching DNG files in the 'RAW' directory
# Path to the current directory
$baseFolder = Get-Location
# Path to the JPG folder
$jpgFolder = Join-Path $baseFolder "JPG"
# Path to the RAW folder
$rawFolder = Join-Path $baseFolder "RAW"
# Iterate through all .jpg files in the JPG folder
foreach ($jpgFile in Get-ChildItem "$jpgFolder\*.jpg") {
# Extract the file name without extension
$jpgFileName = $jpgFile.BaseName
# Check if a .dng file with the same name (excluding the file path) exists in the RAW folder
$rawDngFile = Get-ChildItem "$rawFolder\*\*.dng" | Where-Object { $_.BaseName -eq $jpgFileName }
if ($rawDngFile) {
# Extract the subfolder in the RAW folder where the .dng file is located
$rawSubFolder = $rawDngFile.Directory.FullName.Replace($rawFolder, "")
# Create paths for the destination folders
$destinationFolder = Join-Path $jpgFolder $rawSubFolder
$destinationPath = Join-Path $destinationFolder "$jpgFileName.jpg"
# Ensure that the destination folder exists
if (-not (Test-Path -Path $destinationFolder)) {
New-Item -ItemType Directory -Path $destinationFolder | Out-Null
}
# Move the .jpg file to the corresponding folder in the JPG folder
Move-Item $jpgFile.FullName $destinationPath
Write-Host "Moved: $($jpgFile.FullName) -> $($destinationPath)"
}
else {
Write-Host "No matching .dng file found for $($jpgFile.FullName)"
}
}
@philippgitpush
Copy link
Author

Example input

Base Folder
├── JPG
│   ├── image1.jpg
│   ├── image2.jpg
│   └── image3.jpg
└── RAW
    ├── SubFolder1
    │   └── image1.dng
    └── SubFolder2
        └── image3.dng

Example output

Base Folder
├── JPG
│   ├── SubFolder1
│   │   └── image1.jpg
│   ├── SubFolder2
│   │   └── image3.jpg
│   └── image2.jpg  (remains here as there is no matching .dng file)
└── RAW
    ├── SubFolder1
    │   └── image1.dng
    └── SubFolder2
        └── image3.dng

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment