Applescript: Create folders/subfolders and move multiple files

一曲冷凌霜 提交于 2019-12-30 13:34:10

问题


I have an Applescript question that is much more complex than I can construct. I have been searching for the past couple of days, and I cannot find any script like this, nor can I find enough information to piece one together with my limited knowledge.

I have multiple files with structured names. Each file has the following name structure:

ttu_collectionname_000001.pdf
ttu_collectionname_000002.mp3
ttu_collectionname_000003.pdf ... etc. (Each of these files are of varying file types.)

There is also a csv metadata file associated with each of the original files.

ttu_collectionname_000001.csv
ttu_collectionname_000002.csv
ttu_collectionname_000003.csv ... etc. (Each of these files are csv files.)

I need to create a folder based on the name of the file with sub and sub-subfolders. Each top-level folder name will be unique in the number sequence. Each sub and sub-subfolder name will be the same for each top-level folder.

The folder structure should look like this:

  • ttu_collectionname_000001
    • content
      • archive
      • display
    • metadata
      • archive
      • display
  • ttu_collectionname_000002
    • content
      • archive
      • display
    • metadata
      • archive
      • display

I then need to move the each file to a particular sub-subfolder.

The file ttu_collectionname_000001.pdf would be moved to the ttu_collectionname_000001/content/display folder.

The file ttu_collectionname_000001.csv would be moved to the ttu_collectionname_000001/metadata/display folder.


回答1:


Try:

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



回答2:


Assuming your files are in the same folder,

this 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

creates this:



来源:https://stackoverflow.com/questions/14527505/applescript-create-folders-subfolders-and-move-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!