使用SetFile /日期格式设置创建日期

时间:2015-04-08 00:19:59

标签: shell unix applescript

我想从文件中复制创建日期并将其应用到文件所在的文件夹中。我正在使用SetFile来实现此目的。我收到错误:日期/时间无效。我的假设是日期的格式不起作用。如何正确格式化日期?有人建议解决方案吗?谢谢

    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

2 个答案:

答案 0 :(得分:1)

最终工作代码:

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

感谢大家的贡献和@Zero分享子程序

答案 1 :(得分:0)

试试这个。

       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