Monday, March 1, 2021

#1 2021-02-09 02:29:42 am

traegheitsmoment
Member
Registered: 2021-01-13
Posts: 16

Search for file in folders and subfolders

Hello,

I'd like to search for a file within all subfolders of a given folder.
For example search for the file here_i_am.txt.
Then I'd like to use the path to copy another file (i.e. copy_me.txt) from the desktop to the parent directory of 'here_i_am.txt'

Can anyone help me with that?
Thank you very much,

traegheitsmoment


Filed under: Search, PATH, copy

Offline

 

#2 2021-02-09 08:06:32 am

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

Re: Search for file in folders and subfolders

traegheitsmoment. Your request is easily done with basic AppleScript and I'm sure someone will provide that solution. I'm working to learn AppleScript Objective-C and wrote a solution with that. A few comments:

* The script reports an error if multiple copies of a file with the name "here_I_am.txt" are found in the search folder.

*  The search for the file "here_i_am.txt" is case insensitive, although this is easily changed.

* This script may be faster than a basic AppleScript solution and should be considered if the search folder is very large. In a test with a folder that contained 592 files and I'd guess 50 folders, this script took 0.017 second to execute.

Applescript:

use scripting additions
use framework "Foundation"

on main()
   set locationFileName to "here_i_am.txt"
   set sourceFileName to "copy_me.txt"
   
   set searchFolder to POSIX path of (choose folder)
   
   set sourceFile to current application's (NSHomeDirectory()'s stringByAppendingPathComponent:"Desktop")'s stringByAppendingPathComponent:sourceFileName
   
   set locationFile to getFile(searchFolder, locationFileName)
   set locationFilePath to (current application's NSString's stringWithString:locationFile)'s stringByDeletingLastPathComponent()
   
   copyFile(sourceFile, locationFilePath)
end main

on getFile(theFolder, theName)
   set fileManager to current application's NSFileManager's defaultManager()
   set theFolder to current application's |NSURL|'s fileURLWithPath:(theFolder)
   set folderContents to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:0 errorHandler:(missing value))'s allObjects()
   set thePred to current application's NSPredicate's predicateWithFormat_("(lastPathComponent ==[c] %@)", theName)
   set theFiles to folderContents's filteredArrayUsingPredicate:thePred
   if (count theFiles) ≠ 1 then errorDialog("Multiple or no matching files were found")
   return POSIX path of ((item 1 of theFiles) as text)
end getFile

on copyFile(theFile, theFolder)
   set sourceFile to current application's NSString's stringWithString:theFile
   set sourceName to sourceFile's lastPathComponent()
   set targetFolder to current application's NSString's stringWithString:theFolder
   set targetFile to targetFolder's stringByAppendingPathComponent:sourceName
   set fileManager to current application's NSFileManager's defaultManager()
   if (fileManager's fileExistsAtPath:targetFile) then errorDialog("A file with that name already exists")
   set {theResult, theError} to fileManager's copyItemAtPath:sourceFile toPath:targetFile |error|:(reference)
   if (theResult as boolean) is false then errorDialog("An error ocurred during the file copy")
end copyFile

on errorDialog(dialogText)
   display dialog dialogText buttons {"OK"} default button 1 cancel button 1 with title "" with icon stop
end errorDialog

main()

Last edited by peavine (2021-02-10 09:27:51 am)


2018 Mac mini - macOS Catalina

Offline

 

#3 2021-02-09 08:57:18 am

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

Re: Search for file in folders and subfolders

This is a simple vanilla AppleScript solution with help of the efficient find command of the shell

Applescript:

set baseFolder to POSIX path of (choose folder)
set fileName to "here_i_am.txt"

set foundFiles to paragraphs of (do shell script "/usr/bin/find " & quoted form of baseFolder & " -name " & quoted form of fileName)
if (count foundFiles) is 0 then return
tell application "System Events" to set parentFolder to path of container of (disk item (item 1 of foundFiles))
tell application "Finder" to duplicate file "copy_me.txt" to folder parentFolder

Note: To copy an item you have to write duplicate in AppleScript

Last edited by StefanK (2021-02-09 08:58:33 am)


regards

Stefan

Offline

 

#4 2021-02-11 06:04:00 am

traegheitsmoment
Member
Registered: 2021-01-13
Posts: 16

Re: Search for file in folders and subfolders

Thank you very much, Stefan and peavine for your help!

I'll try to do it as suggested and give you some feedback then...

Take care
Carl

Offline

 

#5 2021-02-11 07:43:59 am

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

Re: Search for file in folders and subfolders

traegheitsmoment wrote:

Thank you very much, Stefan and peavine for your help!

I'll try to do it as suggested and give you some feedback then...

Take care
Carl



Carl. Your most welcome.

I was curious as to the relative speeds of the Find utility and ASObjC in finding the here_i_am.txt file, and I ran some timing tests. There were small differences depending on the folder searched but, as a practical matter, it was pretty much a wash.

I wrote my script in post 2 with an eye toward learning ASObjC and that was not as helpful as it could have been. My suggestion for actual use is as shown below. It overwrites an existing destination file, although this is easily changed.

Applescript:

use scripting additions
use framework "Foundation"

set locationFileName to "here_i_am.txt"
set sourceFileName to "copy_me.txt"

try
   set sourceFile to ((path to desktop as text) & sourceFileName) as alias
on error
   errorDialog("The source file could not be found")
end try

set targetFolder to getFile((choose folder), locationFileName)

tell application "Finder" to duplicate sourceFile to targetFolder with replacing

on getFile(theFolder, theName)
   set fileManager to current application's NSFileManager's defaultManager()
   set folderContents to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:((current application's NSDirectoryEnumerationSkipsPackageDescendants) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)) errorHandler:(missing value))'s allObjects()
   set thePred to current application's NSPredicate's predicateWithFormat_("(lastPathComponent ==[c] %@)", theName)
   set theFiles to folderContents's filteredArrayUsingPredicate:thePred
   set fileCount to (count theFiles)
   if fileCount = 0 then
       errorDialog("No matching files were found")
   else if fileCount > 1 then
       errorDialog((fileCount as text) & " matching files were found")
   end if
   return ((theFiles's URLByDeletingLastPathComponent) as alias)
end getFile

on errorDialog(dialogText)
   display dialog dialogText buttons {"OK"} default button 1 cancel button 1 with title "" with icon stop
end errorDialog

Last edited by peavine (2021-02-11 06:42:11 pm)


2018 Mac mini - macOS Catalina

Offline

 

#6 2021-02-11 04:52:54 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6575

Re: Search for file in folders and subfolders

Peavine, it's probably worth including a couple of enumeration options: NSDirectoryEnumerationSkipsHiddenFiles and NSDirectoryEnumerationSkipsPackageDescendants. The latter can make a big difference is there are packages in the target folder.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#7 2021-02-11 05:12:48 pm

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

Re: Search for file in folders and subfolders

Thanks Shane. I modified my script in post 5 to include those options.


2018 Mac mini - macOS Catalina

Offline

 

#8 2021-02-12 02:12:22 pm

traegheitsmoment
Member
Registered: 2021-01-13
Posts: 16

Re: Search for file in folders and subfolders

@peavine

Your script is really awesome! It just works fantastically.
And the search is quite fast as well.

I hardly dare to ask but maybe you can help me even more...

The name of the copy_me.txt file is actually coded like:
abcd-company-copy_me.txt

And the name of the so called 'flag' file here_i_am.txt file is actually coded like:
company-here_i_am.txt
...and is located somewhere in the sub-sub-folder structure of a folder named like:
abcd-something
which is located on the smb-mounted file server.

Is it possible to extract the first 4 letters of the abcd-company-copy_me.txt file name to narrow the search on the project folder called 'abcd-something'?

and...

Is it possible to define the search term for the flag file 'company-here_i_am.txt' by extracting the word after the first 4 letters of the abcd-company-copy_me.txt file. The company begins with the 6th letter and is separated with minus signs.?

If you can maybe give me at least some hints for the first question I might be able to find out the solution for the second question...

Thank you so much
kind regards

Carl

Offline

 

#9 2021-02-12 03:42:03 pm

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

Re: Search for file in folders and subfolders

Carl,

I modified my script in post 5 to work as you want. Please note:

* I don't have a server and instead used a volume on an external SSD.
* I don't know how you will obtain the copy-me file, so I used a dialog.
* You may want to add some additional error correction but that is easily done.
* I tested the script and it worked as expected.

Applescript:

use scripting additions
use framework "Foundation"

on main()
   set searchPath to "/Volumes/Store/Test/" -- this should be path to folder on server
   set sourceFile to (choose file)
   
   set ATID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {":"}
   set sourceFileName to text item -1 of (sourceFile as text)
   set AppleScript's text item delimiters to {"-"}
   set theCompanyPrefix to text item 1 of sourceFileName
   set theCompanyName to text item 2 of sourceFileName
   set AppleScript's text item delimiters to ATID
   
   set searchFolder to POSIX file (searchPath & theCompanyPrefix & "-something")
   set locationFileName to theCompanyName & "-here_i_am.txt"
   set targetFolder to getFile(searchFolder, locationFileName)
   
   tell application "Finder" to duplicate sourceFile to targetFolder with replacing
end main

on getFile(theFolder, theName)
   set fileManager to current application's NSFileManager's defaultManager()
   set folderContents to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:((current application's NSDirectoryEnumerationSkipsPackageDescendants) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)) errorHandler:(missing value))'s allObjects()
   set thePred to current application's NSPredicate's predicateWithFormat_("(lastPathComponent ==[c] %@)", theName)
   set theFiles to folderContents's filteredArrayUsingPredicate:thePred
   set fileCount to (count theFiles)
   if fileCount = 0 then
       errorDialog("No matching files were found")
   else if fileCount > 1 then
       errorDialog((fileCount as text) & " matching files were found")
   end if
   return ((theFiles's URLByDeletingLastPathComponent) as alias)
end getFile

on errorDialog(dialogText)
   display dialog dialogText buttons {"OK"} default button 1 cancel button 1 with title "" with icon stop
end errorDialog

main()

Last edited by peavine (2021-02-13 10:29:20 am)


2018 Mac mini - macOS Catalina

Offline

 

#10 2021-02-13 11:58:05 am

traegheitsmoment
Member
Registered: 2021-01-13
Posts: 16

Re: Search for file in folders and subfolders

More than cool! Thank you so much.
I haven't testet it yet but I'm looking forward to it.
It's really astonishing what's possible with AppleScript for an expert.
I'll try figure out how to implement your hints for error correction.
And eventually I will figure out how to transform it into a folder action so that every file, that is saved in a special folder will be handled by the script.

I will let you know if I get it done,
thanks again

Carl

Offline

 

#11 2021-02-17 01:44:41 pm

traegheitsmoment
Member
Registered: 2021-01-13
Posts: 16

Re: Search for file in folders and subfolders

Hi peavine,

the script works great and I wanted to implement another feature, but I failed unfortunately.

The thing is...

the search folder's name is actually theCompanyPrefix-something (i.e.abcd-something or abcd-anything or abcd-thisandthat

So 'something' can be any word or expression. And the 'something' is unknown so that the folder can only be identified by theCompanyPrefix. This should be ok, because in the defined search directory the CompanyPrefix is unique and the 'something' is just like some comment for the user.

In the Finder I'd start a search for type folder, name beginning with abcd-

But I failed to figure out how to do that within the script.
Maybe it's simply possible by implementing a wildcard instead of "-something" in the line:

set searchFolder to POSIX file (searchPath & theCompanyPrefix & "-something")

But everything I tried didn't work.
Maybe it's an even more complicated thing...

Could you please help me once more?

Kind regards
Carl

Offline

 

#12 2021-02-17 03:35:37 pm

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

Re: Search for file in folders and subfolders

Carl. Three options come to mind that might accomplish what you want:

1) Add to the script a handler that would search searchPath ("/Volumes/Store/Test/" in my script) for a folder that begins with theCompanyPrefix (abcd in your example). The returned folder would then be used with the getFile handler to limit the search to that particular folder.

2) Have the getFile handler search searchPath for the here_i_am file and not to limit the search to a folder with a particular company prefix. This would not work if an identically-named here_i_am file is located in more than one folder.

3) Have the getFile handler search searchPath for the here_i_am file and then return all paths in which the here_i_am file is found. The script could then loop through the returned paths and select the one that contains the company prefix.

Option 3 seems the best and is not particularly complicated. I'll work on this.

Last edited by peavine (2021-02-17 06:41:38 pm)


2018 Mac mini - macOS Catalina

Offline

 

#13 2021-02-17 05:49:35 pm

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

Re: Search for file in folders and subfolders

Carl. The following script implements option 3 above. I did some testing and it seemed to work as expected.

Applescript:

use scripting additions
use framework "Foundation"

on main()
   set searchPath to "/Volumes/Store/Test/" -- this should be path to folder on server
   set sourceFile to (choose file)
   
   set ATID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {":"}
   set sourceFileName to text item -1 of (sourceFile as text)
   set AppleScript's text item delimiters to {"-"}
   set theCompanyPrefix to text item 1 of sourceFileName
   set theCompanyName to text item 2 of sourceFileName
   set AppleScript's text item delimiters to ATID
   
   set locationFileName to theCompanyName & "-here_i_am.txt"
   set locationFiles to getFiles(searchPath, locationFileName)
   if locationFiles = {} then errorDialog("No location files were found")
   
   set text item delimiters to {"/"}
   set targetFolder to ""
   repeat with anItem in locationFiles
       set anItem to text 1 thru text item -2 of (POSIX path of anItem)
       if anItem contains theCompanyPrefix then
           set targetFolder to POSIX file anItem
           exit repeat
       end if
   end repeat
   if targetFolder = "" then errorDialog("A matching location file was not found")
   set text item delimiters to {""}
   
   tell application "Finder" to duplicate sourceFile to folder targetFolder with replacing
end main

on getFiles(theFolder, theName)
   set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
   set fileManager to current application's NSFileManager's defaultManager()
   set folderContents to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:((current application's NSDirectoryEnumerationSkipsPackageDescendants) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)) errorHandler:(missing value))'s allObjects()
   set thePred to current application's NSPredicate's predicateWithFormat_("(lastPathComponent ==[c] %@)", theName)
   return (folderContents's filteredArrayUsingPredicate:thePred) as list
end getFiles

on errorDialog(dialogText)
   display dialog dialogText buttons {"OK"} default button 1 cancel button 1 with title "" with icon stop
end errorDialog

main()

Last edited by peavine (2021-02-17 07:11:38 pm)


2018 Mac mini - macOS Catalina

Offline

 

#14 2021-02-17 06:45:57 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6575

Re: Search for file in folders and subfolders

Peavine, if I read you correctly, you can probably just modify the predicate to something like this:

Applescript:

set thePred to current application's NSPredicate's predicateWithFormat_("(lastPathComponent ==[c] %@) AND (URLByDeletingLastPathComponent.lastPathComponent BEGINSWITH[c] %@)", theName, theCompanyPrefix)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#15 2021-02-17 07:42:09 pm

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

Re: Search for file in folders and subfolders

Thanks Shane. That simplifies the script and makes it more reliable to boot.

Just a note to Carl. The following script assumes that the here_i_am file is directly located in the folder that begins with the company prefix. If the here_i_am file is located in a subfolder of the folder that begins with the company prefix, you can delete the line that begins with "set thePred" and remove the comment characters from the next line, which also begins with "set thePred".

Applescript:

use scripting additions
use framework "Foundation"

on main()
   set searchPath to "/Volumes/Store/Test/" -- this should be path to folder on server
   set sourceFile to (choose file)
   
   set ATID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {":"}
   set sourceFileName to text item -1 of (sourceFile as text)
   set AppleScript's text item delimiters to {"-"}
   set theCompanyPrefix to text item 1 of sourceFileName
   set theCompanyName to text item 2 of sourceFileName
   set AppleScript's text item delimiters to ATID
   
   set locationFileName to theCompanyName & "-here_i_am.txt"
   set targetFolder to getFiles(searchPath, locationFileName, theCompanyPrefix)
   
   tell application "Finder" to duplicate sourceFile to targetFolder with replacing
end main

on getFiles(theFolder, theName, theCompanyPrefix)
   set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
   set fileManager to current application's NSFileManager's defaultManager()
   set folderContents to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:((current application's NSDirectoryEnumerationSkipsPackageDescendants) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)) errorHandler:(missing value))'s allObjects()
   set thePred to current application's NSPredicate's predicateWithFormat_("(lastPathComponent ==[c] %@) AND (URLByDeletingLastPathComponent.lastPathComponent BEGINSWITH[c] %@)", theName, theCompanyPrefix)
   -- set thePred to current application's NSPredicate's predicateWithFormat_("(lastPathComponent ==[c] %@) AND (path CONTAINS[c] %@)", theName, theCompanyPrefix)
   set theFiles to (folderContents's filteredArrayUsingPredicate:thePred)
   if (count theFiles) = 0 then errorDialog("No location files were found")
   return ((theFiles's URLByDeletingLastPathComponent) as alias)
end getFiles

on errorDialog(dialogText)
   display dialog dialogText buttons {"OK"} default button 1 cancel button 1 with title "" with icon stop
end errorDialog

main()

Last edited by peavine (2021-02-17 08:38:05 pm)


2018 Mac mini - macOS Catalina

Offline

 

#16 2021-02-18 07:47:00 am

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

Re: Search for file in folders and subfolders

I had a question and hoped Shane or other forum member could help.

The predicate Shane suggests as a possible solution is:

Applescript:

(URLByDeletingLastPathComponent.lastPathComponent BEGINSWITH[c] %@)

This works if the lastPathComponent begins with %@, which we've defined in this thread as theCompanyPrefix. This doesn't work if theCompanyPrefix is at the beginnning of a folder that is not the lastPathComponent. My tentative solution is:

Applescript:

(path CONTAINS[c] %@)

This is somewhat less reliable because theCompanyPrefix might be found elsewhere in a folder name, and this is not the desired result. I looked at the documentation and thought perhaps pathComponents might be used to accomplish this but pathComponents returns an array and I don't know how to check each item of the array to see if it begins with %@ (i.e. theCompanyPrefix).  I  tried the following:

Applescript:

(pathComponents BEGINSWITH[c] %@)

Pretty much as expected, my script with the above predicate returned:

error "Can't do a substring operation with something that isn't a string



The OP has indicated that theCompanyPrefixes are unique, so I think my existing solution is workable, but I'd like to understand this just for learning purposes.

Thanks.

Last edited by peavine (2021-02-18 08:00:28 am)


2018 Mac mini - macOS Catalina

Offline

 

#17 2021-02-18 09:19:14 am

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

Re: Search for file in folders and subfolders

The result of pathComponents is an array (a list), you cannot apply the BEGINSWITH predicate to an array.

The only way is Regular Expression

Applescript:

(path MATCHES[c] %@)

and for %@ write

Applescript:

("/" & theCompanyPrefix)

The predicate evaluates to true if there is (at least) one occurrence of a slash followed by the prefix

Last edited by StefanK (2021-02-18 09:19:51 am)


regards

Stefan

Offline

 

#18 2021-02-18 10:04:19 am

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

Re: Search for file in folders and subfolders

Stefan. Thanks for responding to my post.

I tried your suggestion, which made great sense, but it didn't seem to work. Instead, the next line of the script, which begins with "set theFiles", returned no files when there were matching files.

I replaced MATCHES with CONTAINS and the script seemed to work as desired and appeared to enforce the requirement that theCompanyPrefix be at the front of a folder name.

Applescript:

set thePred to current application's NSPredicate's predicateWithFormat_("(lastPathComponent ==[c] %@) AND (path CONTAINS[c] %@)", theName, ("/" & theCompanyPrefix))

Is there any advantage to using MATCHES or do I not understand your suggestion?


2018 Mac mini - macOS Catalina

Offline

 

#19 2021-02-18 11:09:03 am

traegheitsmoment
Member
Registered: 2021-01-13
Posts: 16

Re: Search for file in folders and subfolders

@Peavine, Shane & StefanK.

Thanks a lot to all of you.

My impression is, that you really do high-end scripting here and as total beginner there is nothing I could contribute. Right know I try to understand the expressions you use to figure out a little bit how this works.

@Peavine
You mentioned three options a few posts earlier and thankfully you offer and solution for option 3.

I just like to mention why option 1 maybe a little bit faster in my case, but maybe I am wrong:

The here_i_am file is located somewhere in the sub-sub-folder depths of the searchFolder (i.e.theCompanyPrefix-something).

But the searchFolder is only one of many folders of its parent folder. And each of the siblings folders has a wide subfolder structure. Therefore I guess it would be efficient to identify the searchFolder in the first place to narrow and maybe speed up the search for the here_i_am file.

The here_i_am file is unique in the whole folder structure, so a global search may also work.

I just wanted to let you know this background just for the case that it's of interest for you.
I myself will try to study the posts and try and study the solutions to learn even more.

Thanks again
Carl

Last edited by traegheitsmoment (2021-02-18 11:09:18 am)

Offline

 

#20 2021-02-18 12:09:13 pm

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

Re: Search for file in folders and subfolders

peavine wrote:



Applescript:

set thePred to current application's NSPredicate's predicateWithFormat_("(lastPathComponent ==[c] %@) AND (path CONTAINS[c] %@)", theName, ("/" & theCompanyPrefix))

Is there any advantage to using MATCHES or do I not understand your suggestion?



Most likely you have to escape the slash

Applescript:

set thePred to current application's NSPredicate's predicateWithFormat_("(lastPathComponent ==[c] %@) AND (path MATCHES[c] %@)", theName, ("\\/" & theCompanyPrefix))

Regular Expression is much more powerful than standard contains for example the pattern

\\/[ABC][123]\\s?

finds every (sub)string which starts with a slash followed by A, B or C followed by 1, 2 or 3 followed by a optional whitespace character


regards

Stefan

Offline

 

#21 2021-02-18 02:20:38 pm

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

Re: Search for file in folders and subfolders

Carl. Having given the matter some more thought, the script proposal I identified as option 1 would not work well. So, I have included below my best suggestion, which seems to meet all your requirements. While I was at it, I included the ability to select several copy-me files in the dialog. I tested this script without issue but let me know if you encounter any issues.

Applescript:

use scripting additions
use framework "Foundation"

on main()
   set searchPath to "/Volumes/Store/Test/" -- this should be path to folder on server
   set sourceFiles to (choose file with multiple selections allowed)
   
   set ATID to AppleScript's text item delimiters
   repeat with aFile in sourceFiles
       set AppleScript's text item delimiters to {":"}
       set sourceFileName to text item -1 of (aFile as text)
       set AppleScript's text item delimiters to {"-"}
       set theCompanyPrefix to text item 1 of sourceFileName
       set theCompanyName to text item 2 of sourceFileName
       set locationFileName to theCompanyName & "-here_i_am.txt"
       set targetFolder to getFiles(searchPath, locationFileName, theCompanyPrefix)
       tell application "Finder" to duplicate aFile to targetFolder with replacing
   end repeat
   set AppleScript's text item delimiters to ATID
end main

on getFiles(theFolder, theName, theCompanyPrefix)
   set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
   set fileManager to current application's NSFileManager's defaultManager()
   set folderContents to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:((current application's NSDirectoryEnumerationSkipsPackageDescendants) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)) errorHandler:(missing value))'s allObjects()
   set thePred to current application's NSPredicate's predicateWithFormat_("(lastPathComponent ==[c] %@) AND (path MATCHES[c] %@)", theName, (".*?/" & theCompanyPrefix & ".*"))
   set theFiles to (folderContents's filteredArrayUsingPredicate:thePred)
   if (count theFiles) = 0 then display dialog "A location file was not found for company " & theCompanyPrefix buttons {"OK"} cancel button 1 default button 1 with icon stop
   return ((theFiles's URLByDeletingLastPathComponent) as alias)
end getFiles

main()

Last edited by peavine (2021-02-19 09:13:02 am)


2018 Mac mini - macOS Catalina

Offline

 

#22 2021-02-18 02:25:46 pm

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

Re: Search for file in folders and subfolders

Thanks Stefan. I still can't get that to work--I suspect I'm doing something wrong. I'm working to learn predicates and I'll include the MATCHES operator in my studies.


2018 Mac mini - macOS Catalina

Offline

 

#23 2021-02-18 03:32:05 pm

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

Re: Search for file in folders and subfolders

Sorry, my bad, unlike NSRegularExpression the MATCHES pattern must cover the entire string, so we have to add wildcard characters at the beginning and the end

Applescript:

set thePred to current application's NSPredicate's predicateWithFormat_("(lastPathComponent ==[c] %@) AND (path MATCHES[c] %@)", theName, (".*?/" & theCompanyPrefix & ".*"))


regards

Stefan

Offline

 

#24 2021-02-18 04:49:13 pm

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

Re: Search for file in folders and subfolders

Stefan. I tested that and it worked great. Thanks.


2018 Mac mini - macOS Catalina

Offline

 

#25 2021-02-19 11:23:30 am

traegheitsmoment
Member
Registered: 2021-01-13
Posts: 16

Re: Search for file in folders and subfolders

Hi peavine,

thank you very much again.
In my setup environment the script works just fantastically and it's very fast. One file takes just about a second.

In the real environment I have some issues with speed and I guess it has something to do with network (NAS SMB sharing over LAN) or my adaption to the real world environment.
I had to interrupt one try after about 2 hours.

Then I cheated a little bit and narrowed the search to theSearchfolder (i.e.theCompanyPrefix-something) by changing the line:

set searchPath to "/Volumes/Store/Test/theCompanyPrefix-something/"

I'm not sure if that even made sense because I couldn't figure out if your last version does a general search for the here_i_am file or if the search is already narrowed by the script to the folder theCompanyPrefix-something. Anyway with this cheat, it took about 5 minutes to do the search and copy the file successfully but the cheat won't help me with the task in real life.

So I'm not sure, what to do next. It was possible, to even narrow the search one level down to the next subfolder which is named like  'theCompanyPrefix-communication'.

Therefore I tried to unparse line with the NSPredicate but I didn't succeed yet. I hope to find some developer documentation on the internet, that might help me with that. So I don't give up an I'm still on it.

I'm really very thankful for your fantastic help so far and I humbly don't want to bother you too much and steal your time.

Kind regards
Carl

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)