powershellduplicatesfile-attributes

Get datetaken attribute on file


I am trying to write a script that will get the DATETAKEN attribute from a photo and create a folder structure based on that and move the file to this new location. I have found scripts on google that I'm trying to use but when I running it, it returns:

PS C:\Temp> C:\Temp\MovePhoto.ps1

GAC Version Location

--- ------- -------- True v4.0.30319
C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.... Move-Item : The process cannot access the file because it is being used by another process. At C:\Temp\MovePhoto.ps1:43 char:5 + Move-Item $FileFullName "$FileDirFull\$FileBaseNameNU" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (C:\NoBackup\TES...RA\IMG_1372.JPG:FileInfo) [Move- Item], IOException + FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

If I do the script without the SystemDrawing line it works. But then I can't get the DATETAKEN attribute. I just can't figure out what I am missing.

Here is the script

[reflection.assembly]::LoadWithPartialName("System.Drawing")

$FileAll = (Get-ChildItem $SourcePath -Recurse | where {!$_.psiscontainer} | Select-Object Name,Fullname,BaseName,Extension,CreationTime,LastWriteTime,Length,@{Name="MD5";Expression={Get-Md5Hash $_.fullname}} | group MD5 | Where {$_.Count -gt 1 } | %{$_.Group} | sort MD5)
foreach ($File in $FileAll) {
    $FileBaseName = $File.BaseName
    $FileExtension = $File.Extension
    $FileFullName = $File.FullName
    $FileBaseNameNu = $FileBaseName + $FileExtension
    $FileName = $File.Name
}

$foo = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $FileFullName
$date = $foo.GetPropertyItem(36867).Value[0..9]
$arYear = [Char]$date[0],[Char]$date[1],[Char]$date[2],[Char]$date[3]
$arMonth = [Char]$date[5],[Char]$date[6]
$arDay = [Char]$date[8],[Char]$date[9]
$strYear = [String]::Join("",$arYear)
$strMonth = [String]::Join("",$arMonth) 
$strDay = [String]::Join("",$arDay)
$DateTaken = $strYear + "-" + $strMonth + "-" + $strDay

$FileLastWriteTime = $File.LastWriteTime
$FileDirYear = $FileLastWriteTime.Year
$FileDirDate = $FileLastWriteTime.ToShortDateString()
$FileDirFull = "$DestinationPath\DUBLETTER\$FileDirYear\$DateTaken" 

# Create destination path
if ((Test-Path $FileDirFull) -eq $false) { 
    New-Item -Path $FileDirFull -ItemType Directory
}                                        

if (Test-Path (Join-Path $FileDirFull $File.Name)) {
    $n = 0
    while ((Test-Path (Join-Path $FileDirFull $FileBaseNameNU)) -eq $true){
        $FileBaseNameNU = $FileBaseName + "-" + ++$n + $FileExtension
    }
}
Move-Item $FileFullName  "$FileDirFull\$FileBaseNameNU"

}


Solution

  • Can you try to replace

    [reflection.assembly]::LoadWithPartialName("System.Drawing")
    

    by

    Add-Type -AssemblyName "system.drawing"
    

    Forget it, your trouble is with your file C:\NoBackup\TES...RA\IMG_1372.JPG wich can't be moved because it's open (seems to be open for usage in $foo var). Try first to copy it. You perhaps can use $foo.Dispose() before Move-Item $FileFullName "$FileDirFull\$FileBaseNameNU"