简体   繁体   English

Applescript:创建文件夹/子文件夹并移动多个文件

[英]Applescript: Create folders/subfolders and move multiple files

I have an Applescript question that is much more complex than I can construct. 我有一个比我能构建的复杂得多的Applescript问题。 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_000001.pdf
ttu_collectionname_000002.mp3 ttu_collectionname_000002.mp3
ttu_collectionname_000003.pdf ... etc. (Each of these files are of varying file types.) ttu_collectionname_000003.pdf ...等(这些文件中的每一个都有不同的文件类型。)

There is also a csv metadata file associated with each of the original files. 还有一个与每个原始文件关联的csv元数据文件。

ttu_collectionname_000001.csv ttu_collectionname_000001.csv
ttu_collectionname_000002.csv ttu_collectionname_000002.csv
ttu_collectionname_000003.csv ... etc. (Each of these files are csv files.) ttu_collectionname_000003.csv ...等(这些文件中的每一个都是csv文件。)

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 ttu_collectionname_000001
    • content 内容
      • archive 档案
      • display 显示
    • metadata 元数据
      • archive 档案
      • display 显示
  • ttu_collectionname_000002 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. 文件ttu_collectionname_000001.pdf将移动到ttu_collectionname_000001 / content / display文件夹。

The file ttu_collectionname_000001.csv would be moved to the ttu_collectionname_000001/metadata/display folder. 文件ttu_collectionname_000001.csv将被移动到ttu_collectionname_000001 / metadata / display文件夹。

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

Assuming your files are in the same folder, 假设您的文件位于同一文件夹中, 之前

this AppleScript: 这个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: 创造了这个: 后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件从几个文件夹移动到子文件夹 - Move files from several folders to subfolders 根据文件名 YYYY-MM 的开头将文件从一个文件夹移动到文件夹中的多个子文件夹 - Move files from one folder to multiple subfolders in folders based on start of filename YYYY-MM Applescript创建文件夹并在文件存在时移动文件 - Applescript to Create Folder and Move Files if Files Exists 如何使用Applescript创建文件夹(包含子文件夹) - How to create a folder (with subfolders) with Applescript Applescript:如何从文件名创建新文件夹,创建子文件夹,将文件移动到子文件夹 - Applescript: How to create new folder from file name, create subfolders, move file into subfolder 使用AppleScript移动文件 - move files with AppleScript AppleScript以递归方式处理文件夹中的文件 - AppleScript Processing Files in Folders recursively 如何使用 Automator 和 Applescript 根据日历事件中的特定文本将文件/文件夹批量移动到指定文件夹,MacOS - How to use Automator and Applescript to batch move files/folders to a specified folder based on specific text in Calendar Events, MacOS 使用Apple Script将多个文件夹中的多个文件移动到一个文件夹 - Using Apple Script to move multiple files from multiple folders to one 使用applescript删除多个文件 - Delete multiple files with applescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM