2

I have a number of project folders that all got their date modified set to the current date & time somehow, despite not having touched anything in the folders. I'm looking for a way to use either a batch applet or some other utility that will allow me to drop a folder/folders on it and have their date modified set to the date modified of the most recently modified file in the folder. Can anyone please tell me how I can do this?

In case it matters, I'm on OS X Mavericks 10.9.5. Thanks!

Max Well
  • 257
  • 2
  • 9

1 Answers1

2

If you start a Terminal, and use stat you can get the modification times of all the files and their corresponding names, separated by a colon as follows:

stat -f "%m:%N" *

Sample Output

1476985161:1.png
1476985168:2.png
1476985178:3.png
1476985188:4.png
...
1476728459:Alpha.png
1476728459:AlphaEdges.png

You can now sort that and take the first line, and remove the timestamp so you have the name of the newest file:

stat -f "%m:%N" *png | sort -rn | head -1 | cut -f2 -d:

Sample Output

result.png

Now, you can put that in a variable, and use touch to set the modification times of all the other files to match its modification time:

newest=$(stat -f "%m:%N" *png | sort -rn | head -1 | cut -f2 -d:)
touch -r "$newest" *

So, if you wanted to be able to do that for any given directory name, you could make a little script in your HOME directory called setMod like this:

#!/bin/bash
# Check that exactly one parameter has been specified - the directory
if [ $# -eq 1 ]; then
   # Go to that directory or give up and die
   cd "$1" || exit 1
   # Get name of newest file
   newest=$(stat -f "%m:%N" * | sort -rn | head -1 | cut -f2 -d:)
   # Set modification times of all other files to match
   touch -r "$newest" *
fi

Then make that executable, just necessary one time, with:

chmod +x $HOME/setMod

Now, you can set the modification times of all files in /tmp/freddyFrog like this:

$HOME/setMod /tmp/freddyFrog

Or, if you prefer, you can call that from Applescript with a:

do shell script "$HOME/setMod " & nameOfDirectory

The nameOfDirectory will need to look Unix-y (like /Users/mark/tmp) rather than Apple-y (like Macintosh HD:Users:mark:tmp).

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks! This is really close to what I need to do. But instead of changing all of the files in the folder to the date modified of the newest file in the folder, I need to change the date modified of the folder itself to match the date modified of the newest file in that folder. Is there a way this method could work to do that? I need the other files in the folder to remain untouched and stick with their current date modified, and just change the date modified on the folder to match the date & time of the most recently modified file. – Max Well Oct 21 '16 at 10:12
  • Should be possible - change the line `touch -r "$newest" *` to `touch -r "$newest" .` So basically replace the asterisk with a full-stop. – Mark Setchell Oct 21 '16 at 10:33
  • Something isn't right. I get a compile error either trying to compile or save the script "Expected expression, etc. but found unknown token." When I tried to chmod it, I got a "no such file or directory" error, until I added the .scpt extension in the chmod command. But now it still doesn't seem to work. Also, I don't know how to change spaces in a path to the Unix format. So my path is /Users/MM/Desktop/temp\ US\ tracks but the command with the script name and that path gives a command not found error. – Max Well Oct 21 '16 at 13:32
  • You shouldn't be compiling the script `setMod` - it is a `bash` script and doesn't need compiling. If you are compiling something else, please click `edit` under your original question and show what you are trying to compile, how you are trying to compile it and what happens... – Mark Setchell Oct 21 '16 at 13:38
  • Okay. I just tried compiling it because I got the error when trying to save it of "Expected expression, etc. but found unknown token." and the $ in the first line after the if statement is highlighted in the applescript. Also, when typing the chmod line in terminal to make it an executable, do I actually type it as shown $HOME or replace HOME with my username for my home directory? – Max Well Oct 21 '16 at 13:50
  • The script should be saved as `setMod`, not as `setMod.scpt`. If you are using TextEdit to write the script, it must NOT be saved in RTF format, it should be ASCII or PlainText. – Mark Setchell Oct 21 '16 at 14:00
  • `SHIFT-CMD-T` makes plain text in TextEdit. – Mark Setchell Oct 21 '16 at 14:05
  • If you want to try it on a directory with spaces in its name, enclose that directory name in double quotes... `$HOME/setMod "$HOME/Desktop/Directory with spaces"` – Mark Setchell Oct 21 '16 at 14:18
  • Okay now I am seeing the following errors when entering the command in terminal as shown below: $MM/setMod $MM/Desktop/temptracks -bash: /setMod: No such file or directory $HOME/setMod $HOME/Desktop/temptracks -bash: /Users/MM/setMod: cannot execute binary file Also, I'm not sure what to do about the error I see when trying to save the script "Expected expression, etc. but found unknown token." and the $ in the first line in the script after the if statement is highlighted. Thanks again for all the help with this! This is so close to working I can taste it! – Max Well Oct 22 '16 at 05:01
  • EUREKA! I got it to work! I had to delete the old script I made in applescript, then created a new text document without extension in simple text format, pasted the script text into it, saved it, ran the chmod +x command on it, and then was able to make it work for the target directory entering the command as: $HOME/setMod $HOME/Desktop/temptracks I just wanted to leave the resolution here in case anyone else runs into this issue. And thanks again for the huge help! This will save me countless hours of renaming/rearranging project folders. – Max Well Oct 22 '16 at 05:12
  • Okay. Now I'm trying to do something simpler, but scratching my head as to how to modify the script. Basically, I want to make a droplet that will update the date modified of any file or folder dropped onto it to the current date & time. Any idea what the applescript app and executable would look like to do this? Or would it be simple enough to just do it using applescript/automator without a separate executable file? – Max Well Feb 24 '17 at 13:27
  • This is almost what I want to recover from enabling iCloud, but I would like to recursively go through the tree modifying the deepest folders (depth first search) then coming back so that the higher level folders have the correct dates. Any pointers for how to do this? – robince Feb 22 '22 at 15:01
  • @robince Not sure I understand exactly what you are looking for, but you may want to run `man find` and look at the option `-d` for *"depth first"* search. The comments are probably not the best place to discuss as I'll be more or less the only respondent and there are many much smarter folk than me on Stack Exchange. You might consider asking a question on the Ask Different Stack Exchange site and maybe add a link to the question here for reference - you'll get better exposure and hopefully a better quality of reply. – Mark Setchell Feb 22 '22 at 15:10
  • Hi @MarkSetchell. My shells script not up to the task so I did it in Python. I tried a few things (file mtime unless no files then child folder mtime, ctime of any file or folder, or this which is mtime of any file or folder which has files in its tree). This matches my needs best, maybe it would be useful to someone else: https://gist.github.com/robince/02f7613511b27279f9d661971ff880b7 "cd ~/Documents; set_folder_date_modified.py ./" – robince Feb 23 '22 at 07:56
  • @robince Glad you got it sorted and thank you for sharing your code back with the community. – Mark Setchell Feb 23 '22 at 08:51