在文件名后附加文件夹名称 [不明白] [英] Append filenames with folder name [dont understand]

查看:34
本文介绍了在文件名后附加文件夹名称 [不明白]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力理解如何使用文件夹名称重命名(附加)文件夹内的文件名.例如about.txt、picture.jpg等变成folderName-about.txt、folderName-picture等我是 Applescript 的完全菜鸟,这是我的第一个脚本!

Im struggling to understand how to rename(append) filenames inside a folder with the folders name. Eg, about.txt, picture.jpg, etc to become folderName-about.txt, folderName-picture, etc. Im a complete noob at applescript, this is my first script ever!

on run {input, parameters}

tell application "Finder"
set theFolder to folder "OS X:Users:David:Desktop:SCRIPT:script-copy"
set targetFolder to folder "OS X:Users:David:Desktop:SCRIPT:script-copy-finished"

display dialog "Which structure do you wish to duplicate?" buttons {"Structure-MAIN", "Structure-OTHER"}
set chosenStructure to button returned of result

if contents of chosenStructure is equal to "Structure-MAIN" then
    set chosenStructure to "OS X:Users:David:Desktop:SCRIPT:script-copy:-MAIN"
else
    set chosenStructure to "OS X:Users:David:Desktop:SCRIPT:script-copy:-OTHER"
end if

display dialog "Specify a new folder name:" default answer "John The Dog"
set newName to (text returned of result)
set createNewStructure to make new folder at targetFolder with properties {name:newName}
duplicate every file of entire contents of folder chosenStructure to createNewStructure






set the_folder to name of folder chosenStructure
repeat with this_file in (get files of entire contents of folder chosenStructure)

    set the_start to offset of "_" in ((name of this_file) as string)
    set the_stop to count (name of this_file as string)
    set name of this_file to (the_folder & (items the_start thru the_stop of (name of this_file as string)))
end repeat

end tell


return input
end run

感谢您的帮助!

推荐答案

我希望文件夹内的所有文件名都具有文件夹名称放在文件名的开头.例如,about.txt 变成folderName-about.txt 等

I want all filenames inside the folder to have the folders name prepended at the beginning of the filename. Eg, about.txt to become folderName-about.txt, etc.

试试这个:

tell application "Finder"
    ...
    set the_folder to name of createNewStructure
    repeat with this_file in (get files of entire contents of createNewStructure)
        set name of this_file to the_folder & "-" & (name of this_file)
    end repeat
end tell

整个脚本:

set theFolder to ((path to desktop) as text) & "SCRIPT:script-copy"
set targetFolder to ((path to desktop) as text) & "SCRIPT:script-copy-finished"

display dialog "Which structure do you wish to duplicate?" buttons {"-MAIN", "-OTHER"}
set chosenStructure to ((path to desktop) as text) & "SCRIPT:script-copy:" & button returned of result

display dialog "Specify a new folder name:" default answer "John The Dog"
set newName to (text returned of result)

tell application "Finder"
    set createNewStructure to make new folder at targetFolder with properties {name:newName}
    duplicate every file of entire contents of folder chosenStructure to createNewStructure

    set the_folder to name of createNewStructure
    repeat with this_file in (get files of entire contents of createNewStructure)
        set name of this_file to the_folder & "-" & (name of this_file)
    end repeat
end tell

这篇关于在文件名后附加文件夹名称 [不明白]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆