如何使用今天的日期作为文件路径

问题描述 投票:0回答:1

我正在尝试编写一个脚本来在路径中创建一个新文件夹:

Macintosh HD:用户:意愿:发票:今天的年份:今天的月份

我遇到了一些问题,如果有人有时间的话,希望这是一个快速且简单的修复。

set Fulldate to (current date) as «class isot» as string
set name1 to text 1 thru 4 of Fulldate
set name2 to text 6 thru 7 of Fulldate
property Invoicefolder : "Macintosh HD:Users:Will:Invoices:" & name1 & ":" & name2
tell application "Finder" to make new folder at Invoicefolder with properties {name:"New Folder"}
date path applescript
1个回答
0
投票

这是您可以采取的一种方法。

基本上,它设置了必要文件夹的列表,包括起点 - 主文件夹。

然后它循环浏览该列表并尝试创建下一个文件夹级别,直到完成所有级别。它通过将每个新文件夹名称附加到先前的路径来实现此目的。 完成后,它会弹出一个对话框,通知您成功或失败。

set Fulldate to (current date)

set invoiceFolder to "Invoices" -- name of invoices folder
set yearFolder to year of Fulldate as text -- name of year folder
if Fulldate's month is less than 10 then -- name of month folder
    set monthFolder to "0" & (month of Fulldate as integer)
else
    set monthFolder to (month of Fulldate as integer)
end if

set homeFolder to (path to home folder as text) -- containing folder
set fList to {homeFolder, invoiceFolder, yearFolder, monthFolder}
set hf to homeFolder -- will expand to include newly made folders

repeat with f from 1 to 3
    tell application "Finder"
        set nextName to item (f + 1) of fList
        try
            set hf to make new folder at hf with properties {name:nextName}
            -- log hf -- optional
        on error
            set hf to hf & nextName & ":" as text
            -- log hf -- optional
        end try
    end tell
end repeat

try -- confirm that folders now exist
    exists hf
    display dialog "It all exists"
on error
    display dialog "Something went wrong and not all folders exist"
end try

我添加了几行来记录文件夹创建结果,但已注释掉这些行。它们不是必需的,但日志记录可以显示当脚本在列表中循环时路径如何扩展。

如果情况发生变化,您需要拥有更多或更少的文件夹级别 - 例如dayFolder — 可以轻松地在列表中添加或删除。

© www.soinside.com 2019 - 2024. All rights reserved.