Set file created/modified timestamp to the earlier of the 2 for a ton of files?

I searched for an answer to this for a while, but couldn't find one. I have a lot of wav files (thousands of them) in a sound library that somehow got either the modification or creation date updated very recently, even though I haven't touched them in any way for years.

Is there some way I could use Automator to make an application that I could drag & drop these files to (or a text file containing all of their paths & filenames) that would do the following?:

  • Check the timestamp data associated with the file (date/time: modified, and created)
  • Check to see which timestamp is the earlier of the two
  • If the created timestamp is earlier than the modified one, change the modified timestamp to the created, or vice versa.

I know how to get the date modified or created in terminal, and how to loop through the files received as input. But I'm not sure how to compare the timestamps in a script to determine which is earlier, or how specifically to format the script/app that would do this automatically for each file.

I got the following script from another thread. But this will just set the date modified to the date created without checking to see which one is earlier.

for f in *.wav; do
   olddate=$(stat -f %SB -t %Y%m%d%H%M "$f")
   touch -m -t $olddate "$f"    
done

And in case it makes a difference, I'm on a MacBook Pro running macOS Sierra 10.12.6.

Lastly, if this would be more appropriate for a different network (stackoverflow or superuser maybe), please LMK and I'll ask there instead.

Thank you in advance for the help!


The example shell script code, show below, uses GetFileInfo and SetFile from Command Line Tools for Xcode, which the latter is needed to change the creation date, as touch can only change the access and modification times.

If you do not have Xcode installed, then just install the Command Line Tools for Xcode...

In Terminal: xcode-select --install

After installing Command Line Tools for Xcode, then in Automator you can create a workflow or Service/Quick Action with the appropriate actions to pass the target files to a Run Shell Script action with settings Shell: [/bin/bash] and Pass input: [as arguments] using the following example shell script code:

for f in "$@"; do

    [ -f "$f" ] || continue

    c="$(GetFileInfo -d "$f")"
    m="$(GetFileInfo -m "$f")"

    ec="$(date -j -f "%m/%d/%Y %H:%M:%S" "$c" +%s)"
    em="$(date -j -f "%m/%d/%Y %H:%M:%S" "$m" +%s)"

    if [ "$ec" -lt "$em" ]; then
        SetFile -m "$c" "$f"
    fi

    if [ "$em" -lt "$ec" ]; then
        SetFile -d "$m" "$f"
    fi

done

How the shell script works:

  • for f in "$@"; do -- For each file do something.
  • [ -f "$f" ] || continue -- If it's not a file, then go to the next item passed.
  • c="$(GetFileInfo -d "$f")" -- Gets the creation date of the file.
  • m="$(GetFileInfo -m "$f")" -- Gets the modification date of the file.
  • ec="$(date -j -f "%m/%d/%Y %H:%M:%S" "$c" +%s)" -- Converts the creation date to the number seconds since Epoch.
  • em="$(date -j -f "%m/%d/%Y %H:%M:%S" "$m" +%s)" -- Converts the modification date to the number seconds since Epoch.
  • if [ "$ec" -lt "$em" ]; then -- If the Epoch creation date seconds is less than the Epoch modification date seconds, then:
    • SetFile -m "$c" "$f" -- Sets the modification date of the file to the creation date of the file.
  • if [ "$em" -lt "$ec" ]; then -- If the Epoch modification date seconds is less than the Epoch creation date seconds, then:
    • SetFile -d "$m" "$f" -- Sets the creation date of the file to the modification date of the file.

Notes:

Epoch (00:00:00 UTC, January 1, 1970; see time(3).



Example Automator workflow:

Just add the parent folder containing the .wav files to the Get Specified Finder Items action and set the rest of the actions as shown in the image below.

Note that this is just an example, the main part is the Run Shell Script action, so however you want to pass the target files to it is your choice.

enter image description here