Automate renaming files based on date modified?

Discussion in 'Mac Programming' started by SavMBP15, Jul 8, 2016.

  1. SavMBP15 macrumors 6502

    Mar 26, 2010
    I have a repetitive task I would like to automate.

    I download four files and I want to rename them based on their date modified. (I use iMacro to download the files for me, but because the website I download from uses JS to initiate the DL, I cannot get iMacro to rename them).

    - It will always be four files
    - The original four file names will change each time (there is not pattern to these names)
    - The names I want to change to, will always be the same, based on the date/time modified (which is really date/time downloaded))

    So as an example I download the following four files to my download folder:
    Name Date Modified
    abc123.csv Today, 8:50:32 pm
    def456.csv Today, 8:50:51 pm Today, 8:51:03 pm
    jkl012.csv Today, 8:51:12 pm

    I want to rename these four files from the earliest date modified to the latest date modified to (these names will never change):

    Ultimately each time I download the four files and run this script it will begin to rename each file based on the earliest file to whatever I want and then go to the next one and so on.

    Any ideas?

  2. kryten2 macrumors 6502a

    Mar 17, 2012
    Your post is confusing. You talk about date/time modified but I think you mean creation date. You also say these names will never change e.g earliest.csv etc but then you say rename each file based on the earliest file to whatever I want. Anyway, here's an AppleScript example to get you going :

    set newFileNames to {"earliest", "earliest1", "later", "latest"}
    tell application "Finder"
        set fileList to every file of folder "Test" of home as alias list
        set sortedList to sort fileList by creation date
        repeat with i from 1 to number of items in sortedList
            set this_item to item i of sortedList
            set this_item_ext to name extension of this_item
            set name of this_item to item i of newFileNames & "." & this_item_ext
        end repeat
    end tell
    You can test this by creating a folder named Test in your home folder and copy the 4 files into it. You can also adapt it to be used as an Automator Service.

Share This Page