Tuesday, January 21, 2020

#1 2020-01-15 04:21:23 pm

Jono
Member
Registered: 2008-07-08
Posts: 110

Finder: Find files with names containing a specific word

I'm wanting to find files in a specific folder with a specific word in their name.

So for example find all the files containing 'zebra' like the names here:

Test File Zebra 01.zip
zebra.test.document.txt
audio_zebra_sounds.mp3


Once the files have been found I'd like to set the results as variables so that I could move them, copy them somewhere else, delete them etc.

I've created quite a few scripts in the past processing files I've selected in Finder, but I'm not sure where to start with finding files that contain a certain word.

Any help would be greatly appreciated smile

Offline

 

#2 2020-01-15 06:42:01 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 913

Re: Finder: Find files with names containing a specific word

Hi. The simplest Finder method would be to use a whose clause and "contains."

Applescript:

tell application "Finder" to move my ((choose folder)'s files whose name contains "Zebra") to desktop

Offline

 

#3 2020-01-15 06:47:55 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 298

Re: Finder: Find files with names containing a specific word

The following approach is the same as Marc Anthony's, except that the files are first placed in an alias list. There's no need for this with move or a similar command, but an alias list can be useful in other circumstances.

Applescript:

set theFolder to choose folder

set theString to "zebra"

tell application "Finder"
   set matchingFiles to ((every file in theFolder) whose name contains theString) as alias list
   move matchingFiles to the desktop
end tell

BTW, the OP states that he wants to "set the results as variables." Lists are normally used for this purpose, as demonstrated above.

Last edited by peavine (2020-01-15 07:25:02 pm)


2018 Mac mini - macOS Catalina

Offline

 

#4 2020-01-16 05:31:54 am

Jono
Member
Registered: 2008-07-08
Posts: 110

Re: Finder: Find files with names containing a specific word

Thanks a lot! This works how I want:

Applescript:

tell application "Finder"
   set desktopFolder to (path to desktop folder as alias)
   
   set theString to "zebra"
   
   set matchingFiles to ((every file in desktopFolder) whose name contains theString) as alias list
   
   move the matchingFiles to the trash
end tell

How would I do it if I wanted to run the script through multiple specified folders? Something like the following (but this doesn't work).

Applescript:

tell application "Finder"
   set desktopFolder to (path to desktop folder as alias)
   set documentsFolder to (path to documents folder as alias)
   set picturesFolder to (path to pictures folder as alias)
   
   set theFolders to {desktopFolder, documentsFolder, picturesFolder}
   
   set theString to "zebra"
   
   set matchingFiles to ((every file in theFolders) whose name contains theString) as alias list
   
   move the matchingFiles to the trash
end tell

I also tried a version using 'repeat' but my knowledge of AppleScript isn't good enough to make it actually work tongue

Offline

 

#5 2020-01-16 05:45:40 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11641
Website

Re: Finder: Find files with names containing a specific word

Your first script can be reduced considerably as the desktop folder is the root folder of the Finder

Applescript:


tell application "Finder"
   set theString to "zebra"
   move (every file whose name contains theString) to the trash
end tell

In the second script you need a loop to process each folder separately. By the way all path to ... folder expressions return an alias so the coercion as alias is redundant.

And it's recommended to move all code which is not related to a specific application out of the application tall block.

Applescript:


set desktopFolder to path to desktop folder
set documentsFolder to path to documents folder
set picturesFolder to path to pictures folder

set theFolders to {desktopFolder, documentsFolder, picturesFolder}

set theString to "zebra"
repeat with aFolder in theFolders
   tell application "Finder"
       move (every file of aFolder whose name contains theString) to the trash
   end tell
end repeat


regards

Stefan

Offline

 

#6 2020-01-16 05:58:03 am

Jono
Member
Registered: 2008-07-08
Posts: 110

Re: Finder: Find files with names containing a specific word

Thanks a lot Stefan, that's just what I wanted!

The first example was more about using 'a specified folder' rather than actually using 'the Desktop folder' (so I can use different folders in different scripts), but the information you've added is helping me learn more and more smile

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)