批量重命名文件夹中的AppleScript,只会每其他文件夹的名称变更 [英] Batch Renaming Folders in AppleScript, Will Only Change Names of Every Other Folder
问题描述
我试图写重命名文件夹,子文件夹中的脚本。换句话说,该分类将走向:
I am trying to write a script that renames folders in subfolders. In other words, the taxonomy will go from:
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
到
Directory/Company X/Project 1_1
Directory/Company X/Project 2_2
Directory/Company X/Project 3_3
etc..
到目前为止,我有:
So far I have:
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
不过,该文件夹现在看起来像:
However, the folder now looks like:
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
为什么这样做呢?我要如何改变这个code,因此将重新命名所有的文件吗?如果你愿意,我可以转储活动/回复日志。
Why is it doing this? How do I change this code so it will rename all the files? If you want, I can dump the Events/Replies log.
推荐答案
Jart,主要问题是与第二环,则需要通过X文件夹变量的每一个文件夹中,而不是循环。看来,通过文件夹循环的工作,但是当你给他们改名它造成导致只有读每2文件夹的索引问题。
Jart, the main issue was with the second loop, you need to be looping through every folder of the x folder variable instead. It appears that looping through the folder worked, but when you renamed them it caused an index issue resulting in only reading every 2nd folder.
下面是一个小返工。
- 改名从Y和X你的变量不那么神秘。
- 改变长度测试,以支持名称> 9.
- 我是不能确定在新名称的末尾的项目,如果它应该是一个递增的编号来代替。 (你可以很容易地恢复)
- 留在供你参考一些日志调用,这些都可以帮助调试东西
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
心连心
这篇关于批量重命名文件夹中的AppleScript,只会每其他文件夹的名称变更的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!