The process cannot access the file because it is being used by another process. "the same process that is running it"

2.2k views Asked by At

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.

1

There are 1 answers

0
Andri Sveinsson On

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)
}