123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- function Get-APImageVideoExtensions {
- param (
- [string] $sourceUrl = "https://cdn.jsdelivr.net/gh/jshttp/mime-db@v1.52.0/db.json"
- )
- $jsonData = (Invoke-RestMethod $sourceUrl).PSObject.Properties
- $extensions = @()
-
- foreach ($item in $jsonData) {
- if ($item.Name -match "^(image|video)") {
- $extensions += $item.Value.extensions
- }
- }
-
- $extensions = $extensions | Sort-Object -Unique
- return $extensions
- }
-
- function Get-FileMetaData {
- param (
- [string] $filePath,
- [int] $propertyIndex
- )
-
- $folder = (New-Object -ComObject Shell.Application).NameSpace((Split-Path -Parent $filePath))
- $folderItem = $folder.ParseName((Split-Path -Leaf $filePath))
-
- $property = $Folder.GetDetailsOf(
- $folderItem,
- $propertyIndex
- )
-
- return $property
- }
-
- function Get-APFileDateTaken {
- param (
- [string] $FilePath
- )
- #$PropIndex = 208 # Image Date Taken Property Index ; 208 = vidéo ; 11 = File type
- $CharWhiteList = '[^: \w\/]'
- $type = (Get-FileMetaData -FilePath $FilePath -PropertyIndex 11)
- if ($type -ieq "vidéo") { $PropIndex = 208 }
- if ($type -ieq "image") { $PropIndex = 12 }
-
- $DateTaken = (Get-FileMetaData -FilePath $FilePath -PropertyIndex $PropIndex) -replace $CharWhiteList
-
- $culture = Get-Culture
- $DateTaken = try { [DateTime]::Parse($DateTaken, $culture) } catch { $null }
- return $DateTaken
- }
-
- function Get-APTemporaryDirectory {
- do {
- $TempDir = Join-Path -Path $env:TEMP -ChildPath ([System.IO.Path]::GetRandomFileName())
- } while (Test-Path $TempDir)
- return $TempDir
- }
-
- function Get-APCreationDateSubDirectory {
- param (
- [string] $filePath
- )
-
- $originalDate = Get-APFileDateTaken -FilePath $filePath
-
- if ($originalDate) {
- $year = $originalDate.Year
- $month = $originalDate.Month.ToString("D2")
- $monthName = Get-Culture | ForEach-Object { $_.DateTimeFormat.GetMonthName($month) }
-
- return "$year\$month - $monthName"
- } else {
- return "Non daté"
- }
- }
-
-
- ###########################################
- #### MAIN #####
- ###########################################
-
- if (-not $args[0] -or -not $args[1]) {
- $SourceDir = Read-Host "Veuillez entrer le chemin du dossier source (ou une archive .zip)"
- $DestinationDir = Read-Host "Veuillez entrer le chemin du dossier destination"
- } else {
- $SourceDir = $args[0]
- $DestinationDir = $args[1]
- }
-
- if ($SourceDir -imatch "\.zip$") {
- $TempDir = Get-APTemporaryDirectory
-
- try {
- Expand-Archive -Path $SourceDir -DestinationPath $TempDir
- $SourceDir = $TempDir
- }catch{
- Write-Host "L'archive n'a pas pu être décompressée."
- return
- }
- }
-
- $ValidExtensions = Get-APImageVideoExtensions
- Get-ChildItem -Path $SourceDir -Recurse -File | ForEach-Object {
- $File = $_
- $FileExtension = $File.Extension.ToLower().TrimStart('.')
-
- if ($ValidExtensions -contains $FileExtension) {
- $subFolder = Get-APCreationDateSubDirectory -filePath $File.FullName
- $targetDir = Join-Path -Path $DestinationDir -ChildPath $subFolder
-
- if (-not (Test-Path $targetDir)) {
- New-Item -Path $targetDir -ItemType Directory | Out-Null
- }
-
- Copy-Item -Path $file.FullName -Destination $targetDir
- } else {
- Write-Host "Fichier ignoré: $($file.FullName)"
- }
- }
-
- if ($SourceDir -like "$env:TEMP\*") {
- Remove-Item -Path $SourceDir -Recurse -Force
- }
|