【问题标题】:Applescript to Create Folder and Move Files if Files ExistsApplescript 创建文件夹并在文件存在时移动文件
【发布时间】:2018-03-15 17:41:17
【问题描述】:

我有一个目标文件夹中的文件列表:

示例/HUVEC.csv
示例/HUVEC-1.csv
示例/HUVEC-3.3.2n-1.csv(随机散列)
示例/Endo1.csv

如果“示例”文件夹中有以“HUVECxxx”开头的文件,我希望能够创建一个名为“HUVEC”的文件夹,然后将“HUVECxxx”开头的文件移动到“ HUVEC”文件夹。

这是我目前所拥有的,已从简单版本修改(见下文)

set mgFilesFolder to choose folder with prompt "Where are the files stored which you would like to move to the destination?"

tell application "Finder"
set fileList to files of mgFilesFolder

repeat with aFile in fileList
    set prefix to name of aFile
    if prefix starts with "HUVEC" and (exists folder (HUVEC_folder)) then
        move aFile to HUVEC_folder
    else
        set HUVEC_folder to make new folder in mgFilesFolder with properties {name:"HUVEC"}
    end if
end repeat

repeat with aFile in fileList
    set prefix to name of aFile
    if prefix starts with "HUVEC" then move aFile to HUVEC_folder
end repeat


end tell

更简单的版本适用于一个文件

set mgFilesFolder to choose folder with prompt "Where are the files stored which you would like to move to the destination?"

tell application "Finder"
set fileList to files of mgFilesFolder

repeat with aFile in fileList
    set prefix to name of aFile
    if prefix starts with "HUVEC" and   set HUVEC_folder to make new folder in mgFilesFolder with properties {name:"HUVEC"}
    end if
end repeat

repeat with aFile in fileList
    set prefix to name of aFile
    if prefix starts with "HUVEC" then move aFile to HUVEC_folder
end repeat


end tell

但是我很确定多个文件的问题是,如果文件夹已经存在,那么脚本就会失败。

基本上我认为最大的问题是如何检查一个文件夹是否存在,否则应该创建!
感谢您的帮助,在 Applescript 中仍然相对较新。

【问题讨论】:

    标签: macos applescript


    【解决方案1】:

    您可以利用 AppleScript 的关键字every,而不是手动循环文件,它:

    指定容器中的每个对象 (ref.)

    当您使用这个方便的小功能时,脚本如下所示:

        property prefix : "HUVEC" -- The prefix for file sorting
    
        set mgFilesFolder to choose folder with prompt "Where are the files stored?"
    
    
        tell application "Finder"
            
            -- Retrieve all files that have names
            --  beginning with the prefix
            set fileList to every file of folder mgFilesFolder ¬
                whose name starts with prefix
            
            if fileList is {} then return "No files to move." -- Nothing to do
            
            
            -- Check to see if a folder with the right
            -- name exists; if not, create it.
            if not (exists folder named prefix in folder mgFilesFolder) ¬
                then make new folder in folder mgFilesFolder ¬
                with properties {name:prefix}
            
            
            set prefix_folder to the ¬
                folder named prefix in the ¬
                folder mgFilesFolder
            
            
            -- Move the files into the folder and return
            -- a list of file references for them
            return move the fileList to the prefix_folder
            
        end tell
    

    我在整个代码中放置了 cmets 来解释脚本的每个部分的作用。但是,我一次都不需要使用repeat 循环。

    您的另一个问题是尝试将文件名和目标文件夹是否存在的检查组合到一个命令中,该命令在repeat 循环中一遍又一遍地重复。我把所有东西都分开了,所以每一行代码都知道它在做什么,并且会做得很好。如果出现问题,很容易查明问题出在哪里:

    1. 获取相关文件。
    2. 如果没有,请停止脚本 - 无需继续。
    3. 如果目标文件夹不存在,请创建它。
    4. 获取目标文件夹的引用。
    5. 将文件移动到目标文件夹中。

    完成。


    附录:Bash...?

    您是否考虑过使用 bash 脚本来完成此类任务?对于大量文件,它的速度大约要快一万倍,下面是一个实现您想要的代码示例:

    [[ -d HUVEC ]] && mv HUVEC*.csv HUVEC || { mkdir HUVEC && mv HUVEC*.csv HUVEC; }
    

    ?

    【讨论】:

    • 完美运行。我应该在第一篇文章中添加我希望那里的所有文件都可以,这样“HUVEC”和“ENDO”(见示例)都被移动到它们各自的新文件夹“HUVEC”和“ENDO” .我会继续使用你提到的代码。
    • 是的,我确实遇到了一些 Bash 示例脚本,但我不确定如何从 Finder 轻松调用 bash 脚本...在某个时间点,我也必须对此进行调查!
    • 嗯……这就是为什么我在脚本开头创建了一个名为 prefix 的变量的部分原因。您可以想象,如果您将变量的值从HUVEC’ to ENDO` 更改为,那么 ENDO- 文件将被移动到同名文件夹中。概念化如何在一个脚本中实现两者并不是一个很大的飞跃? 但是,如果您需要帮助,请告诉我。不过,请先自己尝试一下。
    • 我不得不放置另一个答案以适合我所有的代码响应......见下文。并感谢您的帮助。
    【解决方案2】:

    CJK - 我做了一些修改,但仍然有一个小问题。下面的代码引入了另一个变量prefix1,以及我的另一个起始字符串"ENDO"。

    脚本遵从并运行,甚至创建了prefix1 文件夹,但是"ENDO" 文件本身无法使用脚本末尾的两个return move... 命令移动。

    我已经确认顺序很重要,即如果我切换行的顺序

    return move the fileList1 to the prefix_folder1
    return move the fileList to the prefix_folder
    

    然后ENDO 文件被适当地移动,但HUVEC 文件无法移动。

        set prefixes to {"HUVEC", "ENDO"}
    
        set mgFilesFolder to choose folder with prompt "Where are the files stored?"
    
    
        tell application "Finder" to ¬
            repeat with prefix in prefixes
    
                set fileList to every file of folder mgFilesFolder ¬
                    whose name starts with prefix
    
                if (count fileList) is not 0 then
    
                    if not (exists folder named prefix in folder mgFilesFolder) ¬
                        then make new folder in folder mgFilesFolder ¬
                        with properties {name:prefix}
    
                    set prefix_folder to the folder named prefix ¬
                        in the folder mgFilesFolder
    
                    move the fileList to the prefix_folder
    
                end if
            end repeat
    

    【讨论】:

    • 从两行中删除单词return。
    • 谢谢 - 我以为我已经尝试过了,但显然没有。
    • 我编辑了您的代码块,为您提供更精简的解决方案版本。自己解决它做得很好。这个新版本的功能与您的完全相同,但无需再次输入所有命令的手工劳动。实际上,现在您可以拥有一千个不同的前缀,并且它们都将按前缀名称分类到文件夹中,而无需编写任何其他代码(除了将它们添加到顶部的 prefixes 列表中。我希望你可以看到编辑;它说他们正在接受同行评审(这不是使用答案部分的正确方法)。
    • 太棒了!那是我要概念化的下一件事,即在某些时候需要重复使用多个“前缀”。非常感谢!
    • @CJK - 关于如何在 Bash 中完成同样的脚本有什么想法吗?我一直在玩它,请参阅 [stackoverflow.com/questions/49437936/…,但还没有完全发挥作用。将不胜感激任何帮助!
    猜你喜欢
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    相关资源
    最近更新 更多