Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator workflow: find the most recent file in folder?

Hello,


Shouldn't the "Filter Finder Items" be able to return the most recent file in a folder?

I've looked thru so many forums and can't find a working solution.


I have a very simple workflow that will recursively search folders for latest versions but selecting the most recent file isn't a feature in automator, it seems. I've found a few AppleScript snippets that work (it seems) within the script editor but not the automator's "Run AppleScript" module.


Please help. I can't believe this isn't super easy to do in automator. I must be missing something.



Thanks,

Debra



Posted on Jan 6, 2022 7:32 PM

Reply
Question marked as Best reply

Posted on Jan 7, 2022 12:26 PM

Ummm ... a slight bit of overkill, but thanks for the effort.


--------------------------------------------------

I went ahead and just wrote it in Python. Which, frankly is a real pain and waste of time for such an easy feature that should be included in the Automator.



import sys, os
import argparse
from subprocess import Popen

def get_latest(dir_to_search):

# Assuming my version numbers are correct ...

    files = os.listdir(dir_to_search)    
    num_files = len(files)    
#    print(files[num_files - 1])

    os.chdir(dir_to_search)

    command = ('open', '-a', 'Quicktime Player', files[num_files - 1])
    Popen(command, shell=False)

    return 1

if __name__ == '__main__':

    parser = argparse.ArgumentParser()    
    n = len(sys.argv)
#    print("Total arguments passed:", n)
 
    for i in range(1, n):
#         print(sys.argv[i])
        get_latest(sys.argv[i])
        


6 replies
Question marked as Best reply

Jan 7, 2022 12:26 PM in response to VikingOSX

Ummm ... a slight bit of overkill, but thanks for the effort.


--------------------------------------------------

I went ahead and just wrote it in Python. Which, frankly is a real pain and waste of time for such an easy feature that should be included in the Automator.



import sys, os
import argparse
from subprocess import Popen

def get_latest(dir_to_search):

# Assuming my version numbers are correct ...

    files = os.listdir(dir_to_search)    
    num_files = len(files)    
#    print(files[num_files - 1])

    os.chdir(dir_to_search)

    command = ('open', '-a', 'Quicktime Player', files[num_files - 1])
    Popen(command, shell=False)

    return 1

if __name__ == '__main__':

    parser = argparse.ArgumentParser()    
    n = len(sys.argv)
#    print("Total arguments passed:", n)
 
    for i in range(1, n):
#         print(sys.argv[i])
        get_latest(sys.argv[i])
        


Jan 6, 2022 11:34 PM in response to rccharles

Here is the one line terminal command to do what you want.


ls -l /Users/mac/tryoutcode; ls -tT | tail -n 1


/Users/mac/tryoutcode is your folder name. To figure out the folder path. Open the terminal. Drag the folder to the terminal window. Drop the folder. You will now see the full path name.


If you want the full path returned, your going to need to sort out this post:

https://superuser.com/questions/717105/how-to-show-full-path-of-a-file-including-the-full-filename-in-mac-osx-terminal/974971


I don't know automater well. I'll see if I can made something in in the next few days.


mac $ ls -l /Users/mac/tryoutcode; ls -ltT | tail -n 1
total 0
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 a*
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 aa.txt*
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 activities.docx*
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 chart.xlsx*
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 description.doc*
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 dos.xlsx*
drwxr-xr-x  5 mac  staff   160B Jul  7  2019 level2/
drwxr-xr-x  2 mac  staff    64B Jul  7  2019 level3/
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 present.xls*
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 present.xlsx*
lrwxr-xr-x     1 mac      staff     25B Jul 29 22:11:40 2015 config@ -> /Users/mac/Dropbox/config
mac $ ls -l /Users/mac/tryoutcode; ls -tT | tail -n 1
total 0
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 a*
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 aa.txt*
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 activities.docx*
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 chart.xlsx*
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 description.doc*
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 dos.xlsx*
drwxr-xr-x  5 mac  staff   160B Jul  7  2019 level2/
drwxr-xr-x  2 mac  staff    64B Jul  7  2019 level3/
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 present.xls*
-rwxr-xr-x  1 mac  staff     0B Jul  7  2019 present.xlsx*
config@
mac $ 


Let us what more info you need.

Jan 7, 2022 10:29 AM in response to DebraPeri

Here is a Zsh script that takes a starting folder name on the command line, gets the most recent modified file in that folder, prints it, and then begins its recursion into subfolders, where it prints the most recent modified file for the given folder. The output looks like the following though, for privacy, I have edited the full path output:


lastfile.zsh ~/Desktop/Metadata


2021-12-31 08:16:40   ~/Desktop/Metadata/cheetah_1k.jpg
2021-12-26 15:50:48   ~/Desktop/Metadata/SubDir/012C0507.CR


As you didn't mention any particular file type, I am using a wildcard for all files.


#!/bin/zsh

: <<"COMMENT"
Recurse into designated folder hierarchy printing last modified regular file in
the given folder. The syntax (.Nom[1]) means no dotfiles, and sort ascending on
modified date, taking the first [1] file from each folder.
COMMENT

# convert command line arguments using tilde to absolute path
START="${1:a:s/\~\//}"

function statfile () {
    x=$(/usr/bin/stat -t  '%Y-%m-%d %X' -f "%SB   %N" "${1}")
    printf '%s\n' $x
}

# get most recently modified file
statfile ${START}/*(.Nom[1])

# Zsh uses ** symbolism for folder recursion
# Here we are getting just the paths of the subfolders
for folder in "${START}"/**/*(/);
do
    # now get the most recently modifed file in each subfolder
    for file in "${folder}"/*(.Nom[1]);
    do
        statfile "${file}"
    done
done
printf '%s\n' "Done."
exit 0


Automator workflow: find the most recent file in folder?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.