0

ok I know what the issue is, but not how to solve it. basically the process cannot access the file because it is being used by the same code previously. idk just need some way to run this code or something that does the same without the error preventing me from running it.

[reflection.assembly]::LoadWithPartialName("System.Drawing");
foreach ($file in Get-ChildItem)
{
  $filePath = "$pwd\$file"
  $picDate = $pic.GetPropertyItem(36867).value[0..9]
  $pic=New-Object System.Drawing.Bitmap $filePath
  
  $strYear = [String][Char]$picdate[0]+[String][Char]$picdate[1]+[String][Char]$picdate[2]+[String][Char]$picdate[3]
  $strMonth = [String][Char]$picdate[5]+[String][Char]$picdate[6]
  $strDay = [String][Char]$picdate[8]+[String][Char]$picdate[9]
  $DateTaken = $strDay + "/" + $strMonth + "/" + $strYear

  $date1 = [datetime]::ParseExact($dateTaken,"dd/MM/yyyy",$Null)

  (Get-Item $filePath).CreationTime=($date1)
}

incase you were wondering wtf it's doing. Basically for each file in directory it replaces creationTime with Date taken.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • loop thru the list & gather all the info FIRST, run garbage collection to release the file[s], ... then loop thru the list again and make your changes. – Lee_Dailey Feb 05 '22 at 03:20
  • 4
    Have you tried `$pic.Dispose()` before trying to amend the creation date on the file? – Ash Feb 05 '22 at 09:20

1 Answers1

1

Ok I later solved my own issue. turns out one line was in the wrong place.

$picDate = $pic.GetPropertyItem(36867).value[0..9]
$pic=New-Object System.Drawing.Bitmap $filePath

they should be switched and voila, everything works.

[reflection.assembly]::LoadWithPartialName("System.Drawing");
foreach ($file in Get-ChildItem)
{
  $filePath = "$pwd\$file"
  
  $pic=New-Object System.Drawing.Bitmap $filePath
  $picDate = $pic.GetPropertyItem(36867).value[0..9]
  
  
  $strYear = [String][Char]$picdate[0]+[String][Char]$picdate[1]+[String][Char]$picdate[2]+[String][Char]$picdate[3]
  $strMonth = [String][Char]$picdate[5]+[String][Char]$picdate[6]
  $strDay = [String][Char]$picdate[8]+[String][Char]$picdate[9]
  $DateTaken = $strDay + "/" + $strMonth + "/" + $strYear

  $date1 = [datetime]::ParseExact($dateTaken,"dd/MM/yyyy",$Null)
  $pic.Dispose();
  (Get-Item $filePath).CreationTime=($date1)
}