Help wanted in adapting CMD exiftool syntax to Powershell script environment

Started by Bob_K, January 24, 2023, 10:09:28 PM

Previous topic - Next topic

Bob_K

Reference my post on Metadata board "Samsung S22 Ultra: Samsung Gallery edits causing error reading OtherImageStart" for background, but this bit can standalone.

I working on a Powershell script to address the "fix" for the above problem.

The basic crux of what I want to do is implement an "IF $filemodifydate lt X do some exiftool write functions" where I want the X date/time value to be a Powershell variable I'll read from the system clock within the script.

Short story on why is: In order to run a PASS 2 "fix it" step on files that error out of getting modified during my PASS 1 step, which will be evidenced by them having a "younger" filemodifydate vs the files that didn't error out.

The "fix it" step as it works in CMD is:
exiftool -overwrite_original -all= -tagsfromfile @ -all:all -unsafe -icc_profile -OwnerName="(me)" -Artist="(me)" -Model="SM-S908U1 S22 Ultra - T-Mobile (2022)" -n -FileSource=3 -if "$filemodifydate lt "YYYY:MM:DD HH:MM:SS-8:00"' DIR\*.jpg
Where the DateTime value has to be hand applied. (And the specific tags being written are a rerun of the PASS 1 step which failed on the files being fixed via "tagsfromfile" in PASS 2.)

For PS script development/debug purposes at this point, that can be simplified down to a CMD version of:
exiftool -filename -if "$filemodifydate lt 'YYYY:MM:DD HH:MM:SS-8:00'" DIR\*.jpg

For the PS script, the "YYYY:MM:DD HH:MM:SS-08:00" is to be replaced by a variable representing the actual time the script started:
   $start_time = Get-Date -format "yyyy:MM:dd HH:mm:ssK"
And the DIR will be a user input variable:
   $tgt_dir=Read-Host "Enter the path to the target photo files directory"

Thus the proto-foundation of the eventual script can be boiled down to:
$tgt_dir=Read-Host "Enter the path to the target photo files directory"
$start_time=Get-Date -format "yyyy:MM:dd HH:mm:ssK" #get current system time
     Write-Host "INFO: Start_time is $start_time" #For debug visibity
# 1st pass tag writing would be done here. Typically over 90% will not error out
# So the "good" files would have a filemodify date gt $start_time
# In this prototype, no tags are being written, so all files will be lt $start_time
     Write-Host "INFO: Expecting all files to pass the IF Condition" #For debug visibility
  exiftool -filename -if "$filemodifydate lt '$start_time'" $tgt_dir\*.jpg
  pause


PROBLEM IS THAT THIS ALWAYS FAILS THE IF CONDITION WHEN IT SHOULD ALWAYS PASS.

POWERSHELL ISE DETECTS NO ERRORS.

I'M PRETTY SURE THAT THE PROBLEM LIES IN THE NEED TO ADJUST THE ' and " CHARACTERS IN:
exiftool -filename -if "$filemodifydate lt '$start_time'" $tgt_dir\*.jpg

I'VE TRIED LOTS OF VARIATIONS BUT NOTHING HAS WORKED FOR ME SO FAR.

Sample Output:
PS C:\Users\rober> C:\Users\rober\Documents\Batch_Scripts\PS_scripts\OIS_Prototype_Simple_1.ps1
Enter the path to the target photo files directory: "P:\Import Phone Pics HERE\Temp3"
INFO: Start_time is 2023:01:24 18:23:25-08:00
INFO: Expecting all files to pass the IF Condition
    3 files failed condition
    0 image files read
Press Enter to continue...:














StarGeek

All you need to do is swap double and single quotes for PowerShell.

It helps if you copy the command into PS and look at the highlighting.  For example, in this cap, you can see that everything starting with a dollar sign is highlighted differently when enclosed in double quotes.  The correct command for PS will have the same color highlight for each parameter
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

StarGeek

Looked into this a bit more and it shows why I hate Powershell so much.  I know Windows admins swear by it, but there is so much stupid.  See Redirection/Pipe binary corruption with Windows PowerShell

In theory, you want single quotes around anything that's an exiftool variable, and double quotes around shell variable.  So you would do something like
-if '$filemodifydate lt "'$start_time'"'
But this doesn't work, so I have no clue.

You can work around this by using the -userParam option
exiftool -UserParam etStartTime==$start_time -if '$filemodifydate lt "$etStartTime"' ...

Example.  Here I have already preset $start_time and "FMD" is my shortcut for FileModifyDate
PS C:\> echo $start_time
2023:01:26 12:00:00
PS C:\> exiftool -G1 -a -s -fmd y:\!temp\Test3.jpg y:\!temp\Test4.jpg
======== y:/!temp/Test3.jpg
[System]        FileModifyDate                  : 2023:01:26 11:00:00-08:00
======== y:/!temp/Test4.jpg
[System]        FileModifyDate                  : 2023:01:26 13:00:00-08:00
    2 image files read
PS C:\> exiftool -G1 -a -s -Userparam Start_Time=$start_time -if '$filemodifydate lt "$start_time"' -Filename y:\!temp\Test3.jpg y:\!temp\Test4.jpg
======== y:/!temp/Test3.jpg
[System]        FileName                        : Test3.jpg
    1 files failed condition
    1 image files read

Anything further with Powershell, you'll have to research on your own.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Bob_K

Thanks, the -userParam workaround works to make the -if condition work properly.

I had been banging my head on the  ' vs " syntax and reached the premise that Powershell was probably seeing $filemodifydate as a powershell variable with value of "Null" and handing that to exiftool vs exiftool reading it from the target files. Whether that premise was right or wrong, the -userParam is the cure.

BTW: I've also been working to get PS to handoff PS variables as tag values for exiftool to write. Determined that double-quoting those works.
For example in PS:
$New_artist="I. M. Kilroy"
exiftool -Artist="$New_artist" FILE
exiftool -Artist FILE   >>> outputs I. M. Kilroy