macos – 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!