User Tools

Site Tools


script:powershell:multimedia:photo

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
script:powershell:multimedia:photo [2022/10/23 09:20]
admin
script:powershell:multimedia:photo [2022/10/23 09:35] (current)
admin [Organiser ces photos avec la lecture de l'exif]
Line 1: Line 1:
 ====== PowerShell et la photo ====== ====== PowerShell et la photo ======
 ===== Organiser ces photos avec la lecture de l'exif ===== ===== Organiser ces photos avec la lecture de l'exif =====
-L'​EXIF,​ c'est un format de fichier utilisé pour les images prises par un appareil photographique numérique, la première version de l'Exif date de 1995. C'est une mine d'or pour organiser ces photos.//+L'​EXIF,​ c'est un format de fichier utilisé pour les images prises par un appareil photographique numérique, la première version de l'Exif date de 1995.\\ **C'est une mine d'or pour organiser ces photos.**\\ Je suis tombé sur le travail de **Jon Gallant**, [[https://blog.jongallant.com/​2021/​03/​organize-photos-powershell/​|sur son blog ]], depuis je n'​utilise que ce script.\\ Attention pour les utilisateurs de la suite Adobe, on est loin des capacités d'un [[https://​helpx.adobe.com/​camera-raw/​using/​navigate-open-save-images-camera.html|Adobe Camera Raw]]. 
 +<sxh powershell>​ 
 +Param( 
 +    [string]$source,​  
 +    [string]$dest,​  
 +    [string]$format = "​yyyy/​MM_yyyy/​dd_MM_yyyy"​ 
 +)
  
 +$shell = New-Object -ComObject Shell.Application
  
 +function Get-File-Date {
 +    [CmdletBinding()]
 +    Param (
 +        $object
 +    )
  
-===== Effacer les dossiers vides ===== +    $dir $shell.NameSpace( $object.Directory.FullName ) 
-<sxh powershell>​ +    $file $dir.ParseName( $object.Name ) 
-Set to true to test the script + 
-$whatIf ​= $false +    First see if we have Date Taken, which is at index 12 
-# Remove hidden files, like thumbs.db +    $date Get-Date-Property-Value $dir $file 12 
-$removeHiddenFiles = $false + 
-Get hidden files or notDepending on removeHiddenFiles setting +    if ($null -eq $date) { 
-$getHiddelFiles ​!$removeHiddenFiles +        If we don't have Date Taken, then find the oldest date from all date properties 
-# Remove empty directories locally +        0..287 | ForEach-Object { 
-Function Delete-EmptyFolder($path) +            $name = $dir.GetDetailsof($dir.items,​ $_) 
-+ 
-    Go through each subfolder, ​ +            ​if ​( $name -match '​(date)|(created)'​) { 
-    ​Foreach ($subFolder in Get-ChildItem ​-Force -Literal ​$path -Directory +             
-    ​+                ​Only get value if date field because the GetDetailsOf call is expensive 
-        # Call the function recursively +                $tmp = Get-Date-Property-Value $dir $file $_ 
-        ​Delete-EmptyFolder -path $subFolder.FullName+                if ( ($null ​-ne $tmp) -and (($null -eq $date) -or ($tmp -lt $date))) { 
 +                    $date = $tmp 
 +                } 
 +            } 
 +        ​}
     }     }
-    ​Get all child items +    ​return $date 
-    $subItems ​= Get-ChildItem -Force:$getHiddelFiles ​-LiteralPath ​$path +
-    ​# If there are no items, then we can delete the folder + 
-    # Exluce folder: If (($subItems ​-eq $null) -and (-Not($path.contains("DfsrPrivate"))))  +function ​Get-Date-Property-Value { 
-    If ($subItems ​-eq $null +    ​[CmdletBinding()] 
-    ​+ 
-        Write-Host "Removing empty folder '${path}'​+    Param ( 
-        ​Remove-Item -Force -Recurse:$removeHiddenFiles -LiteralPath ​$Path -WhatIf:$whatIf+        ​$dir, 
 +        $file, 
 +        $index 
 +    ) 
 + 
 +    $value ​($dir.GetDetailsof($file,​ $index) -replace "​`u{200e}"​) -replace "​`u{200f}"​ 
 +    if ($value -and $value -ne ''​) { 
 +        return [DateTime]::​ParseExact($value,​ "​g",​ $null) 
 +    } 
 +    return $null 
 +
 + 
 +Get-ChildItem -Attributes !Directory ​$source ​-Recurse |  
 +Foreach-Object { 
 +    Write-Host "​Processing ​$_" 
 + 
 +    ​$date = Get-File-Date $_ 
 + 
 +    ​if ($date) { 
 +     
 +        $destinationFolder = Get-Date -Date $date -Format $format 
 +        $destinationPath = Join-Path -Path $dest -ChildPath $destinationFolder ​   
 + 
 +        ​See if the destination file exists and rename until we get a unique name 
 +        $newFullName = Join-Path -Path $destinationPath -ChildPath $_.Name 
 +        if ($_.FullName ​-eq $newFullName
 +            Write-Host "​Skipping:​ Source file and destination files are at the same location. $_" ​    
 +            return 
 +        } 
 + 
 +        $newNameIndex = 1 
 +        $newName = $_.Name 
 + 
 +        while (Test-Path -Path $newFullName) { 
 +            $newName = ($_.BaseName + "_$newNameIndex" ​+ $_.Extension)  
 +            ​$newFullName = Join-Path -Path $destinationPath -ChildPath $newName ​  
 +            $newNameIndex += 1    
 +        } 
 + 
 +        # If we have a new name, then we need to rename in current location before moving it. 
 +        if ($newNameIndex ​-gt 1) { 
 +            Rename-Item -Path $_.FullName -NewName $newName 
 +        } 
 + 
 +        Write-Host "Moving ​$_ to $newFullName" 
 + 
 +        ​# Create the destination directory if it doesn'​t exist 
 +        if (!(Test-Path $destinationPath)) { 
 +            New-Item -ItemType Directory ​-Force -Path $destinationPath 
 +        } 
 + 
 +        robocopy ​$_.DirectoryName $destinationPath ​$newName /mov
     }     }
 } }
-# Run the script 
-Delete-EmptyFolder -path "​E:​\_Backup"​ 
 </​sxh>​ </​sxh>​
  
-Lire des informations dans un fichier image+==== Lire des informations ​EXIF dans un fichier image ====
 <sxh powershell>​ <sxh powershell>​
 Function Get-Image Function Get-Image
Line 56: Line 121:
 } }
  
-Get-ChildItem -Path "F:\Deploy\Scripts\Misc\SG - SetWallpaper\Source\Wallpaper"​ -Filter *.jpg | ForEach-Object {+Get-ChildItem -Path "​F:​\Wallpaper"​ -Filter *.jpg | ForEach-Object {
     $image = $_ | Get-Image     $image = $_ | Get-Image
     New-Object PSObject -Property ​ @{      New-Object PSObject -Property ​ @{ 
Line 66: Line 131:
 } }
 </​sxh>​ </​sxh>​
 +===== Effacer les dossiers vides =====
 +
 +<sxh powershell>​
 +# Set to true to test the script
 +$whatIf = $false
 +# Remove hidden files, like thumbs.db
 +$removeHiddenFiles = $false
 +# Get hidden files or not. Depending on removeHiddenFiles setting
 +$getHiddelFiles = !$removeHiddenFiles
 +# Remove empty directories locally
 +Function Delete-EmptyFolder($path)
 +{
 +    # Go through each subfolder, ​
 +    Foreach ($subFolder in Get-ChildItem -Force -Literal $path -Directory) ​
 +    {
 +        # Call the function recursively
 +        Delete-EmptyFolder -path $subFolder.FullName
 +    }
 +    # Get all child items
 +    $subItems = Get-ChildItem -Force:​$getHiddelFiles -LiteralPath $path
 +    # If there are no items, then we can delete the folder
 +    # Exluce folder: If (($subItems -eq $null) -and (-Not($path.contains("​DfsrPrivate"​)))) ​
 +    If ($subItems -eq $null) ​
 +    {
 +        Write-Host "​Removing empty folder '​${path}'"​
 +        Remove-Item -Force -Recurse:​$removeHiddenFiles -LiteralPath $Path -WhatIf:​$whatIf
 +    }
 +}
 +# Run the script
 +Delete-EmptyFolder -path "​E:​\_Backup"​
 +</​sxh>​
 +
 +
  
  
script/powershell/multimedia/photo.txt · Last modified: 2022/10/23 09:35 by admin