0

我有多个带有子文件夹的文件夹,其中包含需要用父文件夹+祖父文件夹名称标记的文件。

即Folder 1>Folder 2>File.jpg 需要重命名为Folder_1_Folder_2_File.jpg

我能够找到一个可以做到这一点的脚本,并且一直在尝试对其进行逆向工程,但没有任何运气。下面的脚本提出了两个挑战,1)它包括从根目录开始的整个路径,第二,它删除了文件的名称,因此只允许在一个文件出现错误之前重命名。我知道问题在于脚本正在重命名整个文件,我只是不知道如何继续。

tell application "Finder"
set a to every folder of (choose folder)
repeat with aa in a
    set Base_Name to my MakeBase(aa as string)
    set all_files to (every file in aa)
    repeat with ff in all_files
        set ff's name to (Base_Name & "." & (ff's name extension))
    end repeat

end repeat
end tell

to MakeBase(txt)
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to ":"
    set new_Name_Raw to every text item of txt
    set AppleScript's text item delimiters to "_"
    set final_Name to every text item of new_Name_Raw as text
    set AppleScript's text item delimiters to astid
    return final_Name
end MakeBase

谢谢!

4

2 回答 2

1
tell application "Finder"
    repeat with theItem in (the selection as list)
        set theItem's name to (theItem's container's container's name) & "_" & (theItem's container's name) & "_" & (theItem's name)
    end repeat
end tell

如果您想了解 AppleScript 如何与应用程序一起工作,请查看应用程序的 AppleScript 命令字典(AppleScript 编辑器 > 文件 > 打开字典...)。

编辑 1

这是一个版本,您可以在其中选择包含要重命名的项目的文件夹的“祖父文件夹”:

tell application "Finder"
    set itemsToRename to {}
    set selectedFolders to (the selection as list)
    repeat with selectedFolder in selectedFolders
        set childFolders to every item of selectedFolder
        repeat with childFolder in childFolders
            set grandchildItems to every item of childFolder
            set itemsToRename to itemsToRename & grandchildItems
        end repeat
    end repeat

    repeat with theItem in itemsToRename
        set theItem's name to (theItem's container's container's name) & "_" & (theItem's container's name) & "_" & (theItem's name)
    end repeat
end tell
于 2013-02-21T02:12:57.393 回答
0

尝试:

set myFolder to do shell script "sed 's/\\/$//' <<< " & quoted form of POSIX path of (choose folder)
set myFiles to paragraphs of (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -maxdepth 2 -mindepth 2")
repeat with aFile in myFiles
    tell application "System Events" to set file aFile's name to (do shell script "sed 's/.*\\/\\([^/]*\\)\\/\\([^/]*\\)\\/\\([^/]*$\\)/\\1_\\2_\\3/' <<< " & quoted form of aFile)
end repeat
于 2013-02-21T04:58:33.170 回答