Monday, November 20, 2017

#1 2016-04-21 05:03:15 pm

mac.jedi
Member
Registered: 2008-04-23
Posts: 43

What's the fastest way to check if a list of files are in a directory?

Hi all!

I have a folder on a server that's connected to Dropbox.  It's filled with thousands of zip files used to send data to vendors.  Occasionally we update a hundred or so files, or create a batch of new ones.  For the past few years I've been using a Finder script that repeats through each file, checking then copying, it's slow.  I'm updating it to ASOC and was wondering if there is a faster, or better way to go about this. I've thought of getting the directory contents, then filtering it with the items in trying to copy to see if they exist, or doing a repeat loop with filemanager.  Any advice?

Thanks so much!!!

Offline

 

#2 2016-04-21 06:17:02 pm

ccstone
Member
Registered: 2009-02-07
Posts: 409

Re: What's the fastest way to check if a list of files are in a directory?

mac.jedi wrote:

I've thought of getting the directory contents, then filtering it with the items…


Hey There,

What I've done is to use Shane Stanley's BridgePlus library to get a recursive list of Posix Paths of the front Finder window.

I then have used the Satimage.osax's regular expressions to add a space before the file name in each path to facilitate searching by name.

Then I've done a search and found several items.

Then I've removed the space I previous added, so I have viable Posix Paths again.

The regex stuff can be done with ASObjC, but I'm not going to bother.

Conversely the file-listing stuff can be done with the Satimage.osax, which is what I'd do on my own system.

My downloads folder has over 6K items in it, and this script takes less than half a second to run.

Subsequent look-ups after the path-list is built will be very fast.

I suspect that ASObjC will do even faster look-ups if the file-list is maintained as a ASObjC-object, but I don't know how to do that yet.

In any case this gives you an idea of what can be done.

--
Best Regards,
Chris
_________________________________________________________
{ MacBookPro6,1 · 2.66 GHz Intel Core i7 · 8GB RAM · OSX 10.11.4 }
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

REQUIRES: Satimage.osax, BridgePlus

Applescript:


-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/04/21 17:35
# dMod: 2016/04/21 17:51
# Appl: Finder
# Task: Lookup File Names in a Recursive Path List of the Designated Finder Window
# Osax: Satimage.osax { [url]http://tinyurl.com/dc3soh[/url] }
# Libs: BridgePlus { [url]http://tinyurl.com/zuljd35[/url] }
# Tags: @Applescript, @Script, @Finder, @File, @Name, @Lookup
-------------------------------------------------------------------------------------------
use AppleScript version "2.3.1"
use framework "Foundation"
use scripting additions
use script "BridgePlus" version "1.3.1"
set Forder to load framework
-------------------------------------------------------------------------------------------

# Currently targeting the front Finder window.
tell application "Finder" to set theTarget to target of Finder window 1 as alias

# Get all items in folder using Shane Stanley's BridgePlus library.
set allPaths to Forder's itemsIn:theTarget recursive:true skipHidden:true skipInsidePackages:true asPaths:true
set allPaths to (allPaths's componentsJoinedByString:linefeed) as text
# Add a space after the last path separator “/” to facilitate name searching.
set allPaths to cng("^.+/", "\\0 ", allPaths) of me

# Now we're ready to search for files:

set foundItems to fnd("^.+/ chronosync.*", allPaths, true, true) of me
set foundItems to cng("^(.+/) ", "\\1", foundItems) of me

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on cng(_find, _replace, _data)
   change _find into _replace in _data with regexp without case sensitive
end cng
-------------------------------------------------------------------------------------------
on fnd(_find, _data, _all, strRslt)
   try
       find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
   on error
       return false
   end try
end fnd
-------------------------------------------------------------------------------------------

{
  "/Users/me/Downloads/00) Software/ChronoSync 4.6.3 { 2015.09.07 } { x86_64 } { 10.8-10.10 }.dmg", 
  "/Users/me/Downloads/00) Software/ChronoSync 4.6.4 { 2015.10.01 } { x86_64 } { 10.8-10.10 }.dmg", 
  "/Users/me/Downloads/00) Software/ChronoSync 4.6.5 { 2015.12.15 } { x86_64 } { 10.8-10.11.2 }.dmg", 
  "/Users/me/Downloads/2016.03.17 · 11.31.23 { Gathered }/2016.03.19 · 09.29.33 { Consolidated Items Folder }/ChronoSync 4.6.6 { 2016.03.07 } { x86_64 } { 10.8-10.11.3 }.dmg", 
  "/Users/me/Downloads/2016.03.25 · 15.54.05 { Gathered }/ChronoSync 4.6.7 { 2016.03.22 } { x86_64 } { 10.8-10.11.4 }.dmg"
}

Offline

 

#3 2016-04-25 02:13:07 pm

mac.jedi
Member
Registered: 2008-04-23
Posts: 43

Re: What's the fastest way to check if a list of files are in a directory?

Thanks so much for this!!  Nice implementation! 

I converted my code AppleScriptObjC this past weekend and its super fast. The obj-c methods are certainly faster than using vanilla AS and slicker than using shell commands. 

I'm going to use the updated script in a service that'll enable me to have a Finder like interface to skip, replace, stop, have an  "apply to all" checkbox, along with a progress bar.

I'll post the final script when I get it done.

Thanks Again!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)