我有一个目标文件夹中的文件列表:
示例/ HUVEC.csv
实施例/ HUVEC-1.csv
示例/ HUVEC-3.3.2n-1.csv(随机散列)
实施例/ Endo1.csv
如果在“示例”文件夹中有以“HUVECxxx”开头的文件,然后将以“HUVECxxx”开头的文件移动到“pv”,我可以创建一个名为“HUVEC”的文件夹。 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上还是比较新的。
答案 0 :(得分:0)
您可以利用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
我在整个代码中添加了注释,以解释脚本的每个部分的作用。但是,我没有一次需要使用repeat
循环。
您的另一个问题是尝试将对文件名和目标文件夹存在的检查合并到一个命令中,该命令在repeat
循环中反复重复。我把所有东西都分开了,所以每行代码都知道它做了什么,并且做得很好。如果某些东西不起作用,很容易确定问题出现的地方:
完成。
您是否考虑过将bash脚本用于此类任务?它对于大量文件来说快了大约一万倍,这里有一个完成你想要的代码示例:
[[ -d HUVEC ]] && mv HUVEC*.csv HUVEC || { mkdir HUVEC && mv HUVEC*.csv HUVEC; }
答案 1 :(得分:0)
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