I have a folder that gets a lot of text files and jpegs dropped in it and wanted to write a script to move them out of the folders and to the correct spots. I started using this…
if exists file “PathToFiles/*.txt” then…
The only constants are the two extension .jpg and .txt. I was hoping there was a wildcard like “*” or anything else that could be used.
set myFiles to files of PathToFiles
repeat with currFile in myFiles
if name extension of currFile = "txt" then...
end if
if name extension of currFile ="jpg" then...
end if
end repeat
You can also use the Finder’s bulk references to group the files for action, which saves having to check each extension individually:
tell application "Finder"
set textFiles to files of folder "pathToFiles" whose name extension is "txt"
repeat with thisFile in textFiles
-- Do something with thisFile.
-- If there are no ".txt" files, this repeat simply won't execute.
end repeat
-- Similarly with the jpg's.
end tell
If you simply want to move the text files, say, to another folder — and you don’t need to do any safety checks first — you can use a bulk reference for that too:
tell application "Finder"
move (files of folder "pathToFile" whose name extension is "txt") to folder "somewhereElse" with replacing
end tell