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

Automator Help! Copying latest version of a file

I'm looking for a way to search for a specific file - say, GC01, select the latest version (there will be multiple versions on the disk) and then copy that to a folder, then go to the next file - GC02 and do the same, and on and on.

Posted on Apr 17, 2015 4:54 AM

Reply
5 replies

Apr 19, 2015 6:58 AM in response to rik1

Hello


You may try the following Automator workflow.


1) Run AppleScript action


- Code = as follows



set src to (choose folder with prompt "Choose source folder where to start searching files.")'s POSIX path set dst to (choose folder with prompt "Choose destination folder where to copy found files.")'s POSIX path return {src, dst}




2) Run Shell Script action


- Shell = /bin/bash

- Pass input = as arguments

- Code = as follows



#!/bin/bash SRC="$1" DST="$2" # find files with name matching pattern GC[0-9]+.* under $SRC (\0 separated) find -E "$SRC" -type f -iregex '.*/GC[0-9]+.*' -print0 | # sort found files by modification time in descending order xargs -0 stat -f '%m%t%N' | sort -rn | cut -f2- | # retrieve the first occurence of each file name (\0 separated) perl -F/ -l0ane 'if ( ($h{$F[-1]} += 1) == 1 ) { print; }' | # copy retrieved files to $DST xargs -0 -J% cp -pPR % "$DST"





The resulting workflow will look something like this:


User uploaded file




Briefly tested under OS X 10.6.8.


Good luck,

H

Apr 19, 2015 8:46 AM in response to rik1

Hi,


It's impossible to do this in Automator without script.


To search without issue, the numbers must be always contain 3 digits,

Example of an issue : Searching "GC10", it can returns ("GC10 x.pdf", "GC100 s.pdf", "GC109 y.pdf")

So, pad number which is smaller than 100 with leading zero (in every PDF's name)



Solution with a shell script in an Automator workflow :

Remove the actions in your workflow.

Add the "Run Shell Script" action, select "/bin/bash" as shell in first popup menu, and select "as argument" in the second popup menu (Pass input)

Clear all text in the action, copy/paste this text in the action :

for i in $(jot -w '%03d' 400); do
    mdfind "kMDItemFSName == 'GC$i*.pdf'" -0 | xargs -0 ls -t | tail -1
done


This script use spotlight database.

This script iterate from (001 --> 400) to search filename whose name begin with GC + the number + any character or no character + .pdf, and it sort the result by modification date to return the newer file


Add actions to copy these files to a folder of your choice.

Apr 20, 2015 9:13 AM in response to Hiroto

Here's shell script using refined pattern.


Previous pattern inappropriately matches intermediate node name; e.g., it matches:


/path/to/GC01/abc.txt



as well as


/path/to/GC01 abc.txt




The script below has corrected this behaviour by using pattern matching only leaf node name.



#!/bin/bash # # using pattern # .*/GC[0-9]+[^/]* # SRC="$1" DST="$2" # find files with name matching pattern .*/GC[0-9]+[^/]* under $SRC (\0 separated) find -E "$SRC" -type f -iregex '.*/GC[0-9]+[^/]*' -print0 | # sort found files by modification time in descending order xargs -0 stat -f '%m%t%N' | sort -rn | cut -f2- | # retrieve the first occurence of each file name (\0 separated) perl -F/ -l0ane 'if ( ($h{$F[-1]} += 1) == 1 ) { print; }' | # copy retrieved files to $DST xargs -0 -J% cp -v -pPR % "$DST"





And in case, if you want to be more specific about the number range in target names, here's another script you may try.



#!/bin/bash # # using pattern # .*/(GC01[^0-9/]|GC02[^0-9/]| ... |GC400[^0-9/])[^/]* # # generated by # START=1 # END=400 # '.*/('"$(jot -s '|' -w 'GC%02d[^0-9/]' - $START $END)"')[^/]*' # SRC="$1" DST="$2" # generate matching pattern for find(1): .*/(GC01[^0-9/]|GC02[^0-9/]| ... |GC400[^0-9/])[^/]* START=1 END=400 PAT='.*/('"$(jot -s '|' -w 'GC%02d[^0-9/]' - $START $END)"')[^/]*' # find files with name matching pattern $PAT under $SRC (\0 separated) find -E "$SRC" -type f -iregex "$PAT" -print0 | # sort found files by modification time in descending order xargs -0 stat -f '%m%t%N' | sort -rn | cut -f2- | # retrieve the first occurence of each file name (\0 separated) perl -F/ -l0ane 'if ( ($h{$F[-1]} += 1) == 1 ) { print; }' | # copy retrieved files to $DST xargs -0 -J% cp -v -pPR % "$DST"



Regards,

H

Automator Help! Copying latest version of a file

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