Automator - Move file based on time created (Not date)

I'm trying to create an Automator Folder option that delete's files created between 9:30pm and 5am but there doesn't appear to be a time based option, only day, week, month and year.


Is there a way to achieve this without using the Calendar option ?


Cheers.


Tony

Posted on Jun 26, 2020 8:28 PM

Reply
Question marked as Top-ranking reply

Posted on Jun 28, 2020 11:02 AM

Here is the first script that you would run manually from the Terminal without any arguments. It will graphically prompt for a folder selection that contains your camera images. As written, it will process only the files created between 9:30pm and 5am inclusively, ignoring the rest whose creation time does not satisfy the time range. If you instead, do not want files created exactly at 9:30pm or exactly at 5am to be removed, then change 2129 and 0501 to 2130 and 0500 respectively.


I have left the line in the code that removes (rm) commented, as I would prefer that you test this on a subject of your image in another folder to assure that it is selecting the correct files. I have tested it, but you may want to read the report that it produces, before you uncomment the (rm) line, and turn it loose on live images.


Copy and paste the content of this script into a plain text document that you save to your Desktop as arbitrary_name.zsh. In Terminal, you would mark the script executable, and then run it as follows:

cd ~/Desktop
chmod +x ./arbitrary_name.zsh
./arbitrary_name.zsh


Code:


#!/bin/zsh

: <<'COMMENT'
Present a graphical folder chooser to pick the folder enclosing the security
camera images. Remove any files in this folder selection whose creation time
is at, or after 9:30pm (2129), or at or before 5am (0501)

Run this script from the Terminal command-line, after making it executable:
chmod +x txd.zsh
./txd.zsh

Uncomment the remove (rm) statement after testing on a sample folder, and
reviewing the Desktop/rpt.txt file.
COMMENT

RPT="$HOME/Desktop/rpt.txt"

# Select the folder containing the camera images
function folder_prompt () {
    osascript <<-AS
    use scripting additions
    return POSIX path of (choose folder default location (path to desktop))
AS
}

function report_files () {
    local tm=$1
    local fp=$2
    # headers only if file does not yet exist
    [[ -s $RPT ]] || echo "Time\tFile Path" >> $RPT
    echo "$tm\t$fp" >> $RPT
    return
}

# just in case the folder name begins with an underscore
# and suppress any Finder stderr gibberish to the bit bucket
# sfolder result has trailing '/'
sfolder=$(folder_prompt | sed -e 's/\/ _/\/_/g') 2> /dev/null
# print ${sfolder}

setopt NO_CASE_GLOB
# Case-insensitive loop to get all plain files and no sub-folders
for f in ${sfolder}*.(jp*g)(.N);
do
    # extract the time as hhmm from the file creation date
    hhmm=$(stat -t '%H%M' -f '%SB' "${f:a}")
    # if file creation time is not in range, ignore file and get next one
    [[ $hhmm > 2129 || $hhmm < 0501 ]] || continue

    # report of files eligible for deletion
    report_files $hhmm ${f:a}

    # delete individual files that meet time range criterion
    # rm -f "${f:a}"
done
exit 0


9 replies
Question marked as Top-ranking reply

Jun 28, 2020 11:02 AM in response to T-o-n-y

Here is the first script that you would run manually from the Terminal without any arguments. It will graphically prompt for a folder selection that contains your camera images. As written, it will process only the files created between 9:30pm and 5am inclusively, ignoring the rest whose creation time does not satisfy the time range. If you instead, do not want files created exactly at 9:30pm or exactly at 5am to be removed, then change 2129 and 0501 to 2130 and 0500 respectively.


I have left the line in the code that removes (rm) commented, as I would prefer that you test this on a subject of your image in another folder to assure that it is selecting the correct files. I have tested it, but you may want to read the report that it produces, before you uncomment the (rm) line, and turn it loose on live images.


Copy and paste the content of this script into a plain text document that you save to your Desktop as arbitrary_name.zsh. In Terminal, you would mark the script executable, and then run it as follows:

cd ~/Desktop
chmod +x ./arbitrary_name.zsh
./arbitrary_name.zsh


Code:


#!/bin/zsh

: <<'COMMENT'
Present a graphical folder chooser to pick the folder enclosing the security
camera images. Remove any files in this folder selection whose creation time
is at, or after 9:30pm (2129), or at or before 5am (0501)

Run this script from the Terminal command-line, after making it executable:
chmod +x txd.zsh
./txd.zsh

Uncomment the remove (rm) statement after testing on a sample folder, and
reviewing the Desktop/rpt.txt file.
COMMENT

RPT="$HOME/Desktop/rpt.txt"

# Select the folder containing the camera images
function folder_prompt () {
    osascript <<-AS
    use scripting additions
    return POSIX path of (choose folder default location (path to desktop))
AS
}

function report_files () {
    local tm=$1
    local fp=$2
    # headers only if file does not yet exist
    [[ -s $RPT ]] || echo "Time\tFile Path" >> $RPT
    echo "$tm\t$fp" >> $RPT
    return
}

# just in case the folder name begins with an underscore
# and suppress any Finder stderr gibberish to the bit bucket
# sfolder result has trailing '/'
sfolder=$(folder_prompt | sed -e 's/\/ _/\/_/g') 2> /dev/null
# print ${sfolder}

setopt NO_CASE_GLOB
# Case-insensitive loop to get all plain files and no sub-folders
for f in ${sfolder}*.(jp*g)(.N);
do
    # extract the time as hhmm from the file creation date
    hhmm=$(stat -t '%H%M' -f '%SB' "${f:a}")
    # if file creation time is not in range, ignore file and get next one
    [[ $hhmm > 2129 || $hhmm < 0501 ]] || continue

    # report of files eligible for deletion
    report_files $hhmm ${f:a}

    # delete individual files that meet time range criterion
    # rm -f "${f:a}"
done
exit 0


Jun 27, 2020 10:53 AM in response to T-o-n-y

Which Automator solution will this be:

  1. A Folder Action
    1. You drop files on a designated folder and the magic box then obtains the hhmm creation time from individual files and takes action on those that meet the creation time criteria.
  2. Automator application
    1. You double-click an application on your Desktop, and it prompts you for a starting folder that contains the files you want to process, and you further filter these by filetype, and potentially any previous creation time period.
    2. The files from [2.1] are passed into a Run Shell Script which extracts and compares the hhmm creation time from each file and when matched, takes action on that file.
    3. You pass the stored starting folder location into the script before the individual files are processed.


You haven't mentioned what file types are to be processed, or the copy to/move to destination either.

Jun 28, 2020 6:57 AM in response to T-o-n-y

Ok. Slept on it and I think you need two solutions:

  1. A one-time script that filters your existing collection of images based on your time window, and purges those that match.
  2. An Automator Folder action that just processes the new images saved to the folder and only acts on those in your time window.


The first script would simply prompt you for the folder location containing the images and do its thing. The second would then be far more efficient as it would be addressing the specific images as they are added by the camera, and not processing all images in the drop folder.


Need time today to finalize and test code. Two yards to mow too, so probably won't have anything before Monday.

Jun 27, 2020 7:31 PM in response to T-o-n-y

What image extensions will be in the drop folder?


I have most of the short shell script written that does what you want. The challenge is what to do with the images already in the designated folder. Right now, the script will get a list of all files in the folder, including the dropped image, when an image is written to the folder. The other alternative is to write a script that is assigned to an empty folder, and performs a loop on only the files dropped on it. That is more efficient.

Jun 27, 2020 4:03 PM in response to VikingOSX

Thanks for the reply.


I wouldn't mind if I had to manually start an Automator script to delete any file created within a particular time range, but I'd prefer a Folder option than was based on a time range.


I'm recording still shots from a security camera to my HDD that I don't want from a time range (No point saving blank images at night)

Jul 2, 2020 7:02 AM in response to T-o-n-y

The script did not remove any files because it was coded to generate a report on your Desktop named rpt.txt. That contains the hhmm timestamp and the path to the file that it would have removed — when the following line of code is uncommented:


# rm -f "${f:a}"


to


rm -f "${f:a}"


You can optionally comment the report writer line:


# report_files $hhmm ${f:a}


and each time it is run, it will append its output to the rpt.txt file. You may want to rename or remove the Desktop rpt.txt before you run it with the live delete.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automator - Move file based on time created (Not date)

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