user3101259
user3101259

Reputation: 91

Set creation date using SetFile / date formatting

I'd like to copy creation date from a file and apply it to the folder the file is sitting in. I'm using SetFile for this purpose. I"m getting error: Invalid date/time. My assumption is that it's the format of the date that is not working. How do I format the date properly? Could someone suggest solution? Thanks

    set filesToProcess to choose folder with prompt "Select folders:" with multiple selections allowed

        repeat with thisFile in filesToProcess
            tell application "Finder"
                try
                    set creationDate to creation date of (first file in the entire contents of thisFile whose name ends with "low_r.pdf")
set formattedCreationDate to  --creationDate needs reformatting?
                    do shell script "SetFile -d " & formattedCreationDate & " " & quoted form of (POSIX path of thisFile)
                on error fileNotFound
                    set label index of thisFile to 2
                    display dialog fileNotFound
                end try
            end tell
        end repeat

Upvotes: 1

Views: 613

Answers (2)

user3101259
user3101259

Reputation: 91

Final working code:

set filesToProcess to choose folder with prompt "Select folders:" with multiple selections allowed

repeat with thisFile in filesToProcess
    tell application "Finder"
        try
            set creationDate to creation date of (first file in the entire contents of thisFile whose name ends with "low_r.pdf")
            set formattedCreationDate to quoted form of my stringForDate(creationDate)
            set formattedFolderLocation to quoted form of (POSIX path of thisFile)
            do shell script "SetFile -d " & formattedCreationDate & " " & formattedFolderLocation
        on error errorMsg
            set label index of thisFile to 2
            --display dialog errorMsg
        end try
    end tell
end repeat


on stringForDate(aDate)
    if aDate is "" then return null
    if class of aDate is not date then return null
    set {year:dYear, month:dMonth, day:dDay, hours:dHours, minutes:dMinutes, seconds:dSeconds} to aDate
    set dMonth to dMonth as integer
    if dMonth < 10 then set dMonth to "0" & dMonth
    if dDay < 10 then set dDay to "0" & dDay
    return ((dMonth & "/" & dDay & "/" & dYear & " " & dHours & ":" & dMinutes & ":" & dSeconds) as string)
end stringForDate

Thanks to everyone for contributions and @Zero for sharing the subroutine

Upvotes: 1

markhunte
markhunte

Reputation: 6932

Try this.

       set filesToProcess to choose folder with prompt "Select folders:" with multiple selections allowed

repeat with thisFile in filesToProcess
    tell application "Finder"
        try

            set theFileDate to (creation date of (first file in folder thisFile whose name ends with "low_r.pdf"))
            set formattedCreationDate to (word 2 of short date string of theFileDate & "/" & word 1 of short date string of theFileDate & "/" & word 3 of short date string of theFileDate) & space & time string of theFileDate
            --need to flip dd/mm/yyyy to be mm/dd/yyyy Ues this line -->>set formattedCreationDate to short date string of theFileDate & space & time string of theFileDate <<-- instead if your country's date format is already mm/dd/yyyy




            my doCommand(formattedCreationDate, thisFile)
        on error fileNotFound
            set label index of thisFile to 2
            display dialog fileNotFound
        end try
    end tell
end repeat
on doCommand(formattedCreationDate, thisFile)
    do shell script "SetFile -d " & (quoted form of formattedCreationDate) & " " & quoted form of (POSIX path of thisFile)
end doCommand

Upvotes: 0

Related Questions