首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AppleScript中的批重命名文件夹,只会更改其他文件夹的名称。

AppleScript中的批重命名文件夹,只会更改其他文件夹的名称。
EN

Stack Overflow用户
提问于 2013-08-27 22:46:05
回答 1查看 2.1K关注 0票数 0

我正在尝试编写一个脚本来重命名子文件夹中的文件夹。换句话说,分类将从以下几个方面着手:

代码语言:javascript
复制
Directory/Company X/1
Directory/Company X/2
Directory/Company X/3
Directory/Company X/4
Directory/Company X/5
Directory/Company X/6
Directory/Company X/7
Directory/Company X/8
Directory/Company X/9
Directory/Company X/10
Directory/Company X/info

代码语言:javascript
复制
Directory/Company X/Project 1_1
Directory/Company X/Project 2_2
Directory/Company X/Project 3_3
etc..

到目前为止,我已经:

代码语言:javascript
复制
tell application "Finder"

-- Defining Pro Folders 
set pro_folder to folder POSIX file "/Users/Jart/Desktop/test"
set all_pro_folders to every folder of pro_folder

-- Iterate through folders and check if folder name is greater than one character
repeat with x in all_pro_folders
    repeat with y in x
        -- Convert the folder name to a string and check if that string is longer than one character
        set incomingName to folder ((y as alias) as text)
        set incomingNameString to name of incomingName
        if length of incomingNameString = 1 then
            -- Set old name (number) to a new variable
            set oldname to folder ((y as alias) as text)
            set oldnameString to name of oldname
            -- Do it again for the second part
            set oldname2 to folder ((y as alias) as text)
            set oldnameString2 to name of oldname2
            -- Rename here
            set name of y to "Project" & " " & oldnameString & "_" & oldnameString
        end if
    end repeat
end repeat

end tell

但是,该文件夹现在看起来如下:

代码语言:javascript
复制
Directory/Company X/3
Directory/Company X/5
Directory/Company X/7
Directory/Company X/9
Directory/Company X/10
Directory/Company X/info
Directory/Company X/Project 1_1
Directory/Company X/Project 2_2
Directory/Company X/Project 4_4
Directory/Company X/Project 6_6
Directory/Company X/Project 8_8

它为什么要这么做?如何更改此代码以使其重命名所有文件?如果您愿意,我可以转储事件/回复日志。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-28 00:31:09

Jart,主要问题是第二个循环,您需要循环遍历x文件夹变量的每个文件夹。看起来,循环遍历文件夹是有效的,但是当您重命名它们时,它会导致索引问题,导致只读取第二个文件夹。

这里有一点返工。

  • 将y和x中的变量重新命名为不那么神秘。
  • 更改长度测试以支持名称> 9。
  • 我不确定新名称末尾的项目是否应该是一个递增的数字。(您可以很容易地恢复)
  • 在一些供您参考的日志调用中,这些可以帮助调试一些内容。

代码语言:javascript
复制
tell application "Finder"
    -- Defining Pro Folders 
    set pro_folder to folder POSIX file "/Users/Jart/Desktop/test"
    set all_pro_folders to every folder of pro_folder

    -- Iterate through folders and check if folder name is greater than one character
    repeat with parent_folder in all_pro_folders
        set counter to 0 -- reset a counter variable we use for the end of the name 
        set child_folders to every folder of parent_folder
        repeat with current_folder in child_folders
            set fold_name to the name of current_folder
            log (fold_name)
            if (the length of fold_name < 3) then -- this will allow names > 9  
                set counter to counter + 1 -- increment the counter every time we find valid folder
                set new_name to "Project" & " " & fold_name & "_" & counter
                log (new_name)
                set the name of current_folder to new_name
            end if
        end repeat
    end repeat
end tell

HTH

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18476702

复制
相关文章

相似问题

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