Skip to content

Instantly share code, notes, and snippets.

@ghotz
Last active September 15, 2023 21:00
Show Gist options
  • Save ghotz/c614584f44bf975153ea to your computer and use it in GitHub Desktop.
Save ghotz/c614584f44bf975153ea to your computer and use it in GitHub Desktop.
PowerShell examples to feed arguments to Exiftool without the initialization overhead of launching the process each time
#
# Exiftool example to process multiple commands without paying the initialization time
# for each call writing to an argument file
#
cls;
$Inputdir = "C:\Photos\Personal\[2007]\[2007-00-00] Test & Test"
# initializes argument file
if (Test-Path c:\temp\tmp.args) { del c:\temp\tmp.args; };
$null | Out-File c:\temp\tmp.args -Append -Encoding Ascii;
# Startup exiftool
Start-Process "C:\utils\exiftool\exiftool.exe" "-stay_open True -@ C:\temp\tmp.args";
# send commands to rename all files based on the photo creation date
# using the following template YYYYMMDD-HHmmSS-Snn.ext
"-d`n%Y%m%d-%H%M%S-S%%.2c.%%e`n-filename<CreateDate`n$Inputdir`n" | Out-File c:\temp\tmp.args -Append -Encoding Ascii;
"-execute`n" | Out-File c:\temp\tmp.args -Append -Encoding Ascii;
# send commands to set all files creation date to the photo creation date
"-filemodifydate<datetimeoriginal`n$Inputdir`n" | Out-File c:\temp\tmp.args -Append -Encoding Ascii;
"-execute`n" | Out-File c:\temp\tmp.args -Append -Encoding Ascii;
# let the window stay open for a while... (or wait for a key)
Start-Sleep -s 5;
# send command to shutdown
"-stay_open`nFalse`n" | Out-File c:\temp\tmp.args -Append -Encoding Ascii;
#
# Exiftool example to process multiple commands without paying the initialization time
# for each call using standard input redirection
#
cls;
$Inputdir = "C:\Photos\Personal\[2007]\[2007-00-00] Test & Test";
# create Exiftool process
$psi = New-Object System.Diagnostics.ProcessStartInfo;
$psi.FileName = "C:\utils\exiftool\exiftool.exe";
$psi.Arguments = "-stay_open True -@ -"; # note the second hyphen
$psi.UseShellExecute = $false;
$psi.RedirectStandardInput = $true;
$psi.RedirectStandardOutput = $true;
$psi.RedirectStandardError = $true;
$p = [System.Diagnostics.Process]::Start($psi);
Start-Sleep -s 2;
# send commands to rename all files based on the photo creation date
# using the following template YYYYMMDD-HHmmSS-Snn.ext
$p.StandardInput.WriteLine("-d`n%Y%m%d-%H%M%S-S%%.2c.%%e`n-filename<CreateDate`n$Inputdir`n");
$p.StandardInput.WriteLine("-execute`n");
# send commands to set all files creation date to the photo creation date
$p.StandardInput.WriteLine("-filemodifydate<datetimeoriginal`n$Inputdir`n");
$p.StandardInput.WriteLine("-execute`n");
# send command to shutdown
$p.StandardInput.WriteLine("-stay_open`nFalse`n");
# wait for process to exit and output STDIN and STDOUT
$p.StandardError.ReadToEnd();
$p.StandardOutput.ReadToEnd();
$p.WaitForExit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment