I'm trying to make a script that will do this:
There will be a .txt file with a list of names, one per paragraph, like this:
name1
name2
name3
name4
name5
There also will be a folder with lots of files in it. I want the script to look at every file in that folder, and in any of the names listed in .txt file are contained in the file name, then move that file to a specific folder. If the file name does not contain one of the names from the list, then move that file to a different folder.
Example:
- There are "name1", "name2" and "name3" in the .txt file.
- There are "000name2000.jpg", "000name7000.jpg" and "000name3000.jpg" in the folder.
- "000name2000.jpg" contains "name2" in it so it should be moved to folder1.
- "000name3000.jpg" should be moved to folder1 as well.
- And "000name7000.jpg" should be moved to folder2.
I want it to be so because this list with names will be very long and I want script to be as small as possible.
This is how my script looks like now:
property source_folder : alias "path:to:source_folder"
property tattoos_folder : alias "path:to:first_folder"
property models_folder : alias "path:to:second_folder"
property text_file : alias "path:to:text_file.txt"
process_folder(source_folder)
on process_folder(this_folder)
set these_items to list folder this_folder without invisibles
set container_name to name of (info for this_folder)
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
if folder of (info for this_item) is true then
process_folder(this_item)
else
process_item(this_item, container_name, i)
end if
end repeat
end process_folder
on process_item(this_item, c, i)
if i < 10 then
set i to "000" & i
else if (i < 100) and (i > 9) then
set i to "00" & i
else if (i < 1000) and (i > 99) then
set i to "0" & i
end if
set r to (random number from 0 to 9999)
if r < 10 then
set r to "000" & r
else if (r < 100) and (r > 9) then
set r to "00" & r
else if (r < 1000) and (r > 99) then
set r to "0" & r
end if
tell application "System Events"
-- get file extension so not overwritten
set e to name extension of this_item
set new_name to "" & r & "" & c & "" & i & "." & e
set name of this_item to new_name
move this_item to first_folder -- THIS IS WHERE THIS NEW PART OF CODE SHOULD BE
end if -- IF NAME IS (meets name from .txt file) THEN MOVE TO first_folder,
end tell -- IF ANOTHER THEN MOVE TO second_folder
end process_item
display notification "All images were processed." with title "New" sound name "Glass.aiff"
tell me to quit
答案 0 :(得分:0)
这假设您可以在脚本中定义文本文件和文件夹路径。如果需要,您可以添加选择对话框。 此脚本将源文件夹中的所有文件放入一个列表中,然后遍历每个文件,检查名称列表中的名称是否匹配,如果匹配,则将其移动到folder1,否则,如果在检查全部文件后仍然保留名称,然后将其移动到folder2。
-- define folder/file locations
property nameFile : ((path to desktop) & "nameList.txt") as string
property sourceFolder : ((path to desktop) & "test:") as string
property folder1 : ((path to desktop) & "folder1:") as string
property folder2 : ((path to desktop) & "folder2:") as string
try
set move1List to {}
set move2List to {}
set nameList to paragraphs of (read file nameFile)
set fileList to list folder sourceFolder without invisibles
repeat with n from 1 to count (fileList)
set thisFile to item n of fileList
-- set fileName to name of (info for alias (sourceFolder & thisFile))
set fileRef to alias (sourceFolder & thisFile)
repeat with thisName in nameList
if (thisName as string) is in thisFile then
set end of move1List to fileRef
exit repeat
end if
end repeat
if exists fileRef then set end of move2List to fileRef
end repeat
display dialog "About to move: " & return & (count move1List) & " files to folder1" & return & (count move2List) & " files to folder2"
-- move all the files in the lists
tell application "Finder" -- can try "System Events"
move move1List to folder folder1
move move2List to folder folder2
end tell
-- could avoid Finder by iterating to a shell script using
-- do shell script "mv " & quoted form of thisFile & space & quoted form of folder1
on error err
display dialog err
end try