首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SetFile /日期格式设置创建日期

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

Stack Overflow用户
提问于 2015-04-08 08:19:59
回答 2查看 396关注 0票数 1

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

代码语言:javascript
复制
    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
EN

回答 2

Stack Overflow用户

发布于 2015-04-09 02:27:42

最终工作代码:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-04-09 02:10:09

尝尝这个。

代码语言:javascript
复制
       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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29503732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档