【问题标题】:Applescript: Create folders/subfolders and move multiple filesApplescript:创建文件夹/子文件夹并移动多个文件
【发布时间】:2013-01-09 18:04:43
【问题描述】:

我有一个 Applescript 问题,它比我能构建的要复杂得多。这几天我一直在寻找,我找不到任何这样的脚本,也找不到足够的信息来拼凑我有限的知识。

我有多个具有结构化名称的文件。每个文件的名称结构如下:

ttu_collectionname_000001.pdf
ttu_collectionname_000002.mp3
ttu_collectionname_000003.pdf ...等(这些文件中的每一个都具有不同的文件类型。)

还有一个与每个原始文件关联的 csv 元数据文件。

ttu_collectionname_000001.csv
ttu_collectionname_000002.csv
ttu_collectionname_000003.csv ...等(这些文件中的每一个都是csv文件。)

我需要根据包含子文件夹和子子文件夹的文件名创建一个文件夹。每个顶级文件夹名称在编号序列中都是唯一的。每个顶级文件夹的每个子文件夹和子文件夹名称都相同。

文件夹结构应如下所示:

  • ttu_collectionname_000001
    • 内容
      • 存档
      • 显示
    • 元数据
      • 存档
      • 显示
  • ttu_collectionname_000002
    • 内容
      • 存档
      • 显示
    • 元数据
      • 存档
      • 显示

然后我需要将每个文件移动到特定的子文件夹。

文件 ttu_collectionname_000001.pdf 将被移动到 ttu_collectionname_000001/content/display 文件夹。

文件 ttu_collectionname_000001.csv 将移动到 ttu_collectionname_000001/metadata/display 文件夹。

【问题讨论】:

    标签: macos applescript automator


    【解决方案1】:

    试试:

    set myFolder to "Mac OS X:Users:stark:Main Folder"
    tell application "Finder" to set myFiles to folder myFolder's files as alias list
    repeat with aFile in myFiles
        tell application "System Events" to set {fileName, fileExt} to {name, name extension} of aFile
        set baseName to text 1 thru ((get offset of "." & fileExt in fileName) - 1) of fileName
        do shell script "mkdir -p " & (quoted form of (POSIX path of myFolder)) & "/" & baseName & "/{\"content\",\"metadata\"}/{\"display\",\"archive\"}"
        tell application "System Events"
            if fileExt is "pdf" then move aFile to (myFolder & ":" & baseName & ":content:display" as text)
            if fileExt is "csv" then move aFile to (myFolder & ":" & baseName & ":metadata:display" as text)
        end tell
    end repeat
    

    【讨论】:

    • 令人印象深刻。我忘记了mkdir,我从来不知道一个命令可以创建多个目录。
    • 谢谢...时不时有用。
    • 太棒了!太感谢了!我必须学习更多Applescript。这太有帮助了!
    • 不,效果很好。我一定是无意中取消了答案。很抱歉造成混乱。
    【解决方案2】:

    假设您的文件在同一个文件夹中,

    这个 AppleScript:

    set pathToFolderOfTTUFiles to (path to the desktop as text) & "TTU:"
    tell application "Finder"
        set theFiles to every item of folder pathToFolderOfTTUFiles whose name extension is not "csv" and kind is not "Folder"
        repeat with theFile in theFiles
            set lengthOfExtension to (length of (theFile's name extension as text)) + 1
            set fileNameWithoutExtension to text 1 through -(lengthOfExtension + 1) of (theFile's name as text)
            set theFolder to make new folder at folder pathToFolderOfTTUFiles with properties {name:fileNameWithoutExtension}
    
            set theContentFolder to make new folder at theFolder with properties {name:"content"}
            make new folder at theContentFolder with properties {name:"archive"}
            set theContentDisplayFolder to make new folder at theContentFolder with properties {name:"display"}
            set theMetadataFolder to make new folder at theFolder with properties {name:"metadata"}
            make new folder at theMetadataFolder with properties {name:"archive"}
            set theMetadataDisplayFolder to make new folder at theMetadataFolder with properties {name:"display"}
    
            move theFile to theContentDisplayFolder
            set pathToCSV to pathToFolderOfTTUFiles & fileNameWithoutExtension & ".csv"
            if exists pathToCSV then move pathToCSV to theMetadataDisplayFolder
        end repeat
    end tell
    

    创建这个:

    【讨论】:

    • 谢谢,约翰!我也运行了这个脚本,它除了移动 csv 文件之外什么都做。我错过了什么吗?
    • 您的 CSV 文件在哪里?在我的示例中,它们与 PDF 和 MP3 位于同一文件夹中(参见我的第一个屏幕截图)。
    • 约翰,我不确定我之前做错了什么,但我让它工作了。非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多