"Error renaming (...)"

Started by Valentin, March 30, 2022, 06:44:41 AM

Previous topic - Next topic

Valentin

No question, just documenting my own stupidity to make it easier for the next poor soul.

I have a small PowerShell script that checks a folder for pictures, resizes them if too large, renames based on certain standards and of course uses ExifTool to remove all and set a few relevant attributes. Everything worked great but I noticed that some pictures didn't have their attributes changed, once I noticed that my try / catch had a bug, I realized the error was with ExifTool, I could even reproduce it outside of my script:

PS C:\tmp\foo> ..\exiftool.exe -All= *                     
Error renaming My Picture Name.jpg


I couldn't find much on that but went through the following process of elimination:

  • "IsReadOnly" was false for the files
  • no permission issues, gave EVERYONE full modify and even ran it elevated
  • when checking with Sysinternals ProcMon, I saw "SHARING VIOLATION"
  • I couldn't find any open handle on the image file (via ProcExp) so suspected ExifTool ...
  • still, disabled antivirus / unloaded filter drivers, moved the files to non synced storage, no change in behavior

It was not fully reproducible though and sometimes worked and other times not? I could copy an affected file and it would work but then again not? I realized that whether pictures were affected depended on if I ran the script before testing it separately with ExifTool ... Then I realized that I _could_ find a handle for images without whitespaces in their filename and that file handle was held by powershell_ise.exe (which I was using to eliminate VS Code) ... It turns out that I introduced this issue when I changed the script flow ... I'm using:

$imageProperties = [System.Drawing.Image]::FromFile($image.FullName)

which requires:

$imageProperties.dispose()

which was now after (instead of before) the call to ExifTool ... yeah.

Anyhow, I hope there are enough keywords in here to help someone else not to spend two evenings on this scratching their head. Obviously a better testing methodology and using git even for those "I just need a quick helper script" cases so you can check easily across your commits would have helped too but I'll probably need at least two more of these experiences to commit (ha) to that.