You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Move folder into folder, based on partial match of name

In applescript, let's say I have a folder with a certain name. In another location, a folder exists, that has a partial match to the certain name of the first folder.


So far, I've hacked together this script, that seems to succeed in finding partial matches between strings:


set sourceFolder to {"RS_W36_21_NEWS", "TV1016_RS_W20_10_GO"} 
set destFolder to {"RS_W35_GO", “BLURB_RS_W36_21_NEWS_GO"} 


set TID to text item delimiters
set text item delimiters to "\\|"
set grepItems to sourceFolder as text
set text item delimiters to linefeed
set FolderText to destFolder as text
set matches to do shell script "echo " & quoted form of FolderText & " | grep " & quoted form of grepItems
set res to matches as text


display dialog res



This prints "BLURB_RS_W36_21_NEWS_GO", which is to be expected, since the source has a string of "RS_W36_21_NEWS".



My question is, how can I update the script to move the folder into the partially matching folder?


From the dialog, it is clear that there is a match, I just can't figure out the method for actually moving the file into the matching location.


Posted on Jul 20, 2021 5:04 AM

Reply
Question marked as Top-ranking reply

Posted on Jul 29, 2021 12:30 PM

> However, I get an error of error "Can’t get name of {}." number -1728 from name of {}, highlighting FinalString in the last if.


Your script sets destDirSubDir to "every folder of...'.

As such you will get back a LIST of folders - even if there is only one folder. Even worse, if there are no folders with a matching name, you will get an empty list.


You can't ask for name of destDirSubDir if it is either a list, or empty.


In this case I'm guessing there is no such folder, so the list is empty and hence you get the error message about getting the 'name of {}'


So you have to add additional logic to check if such a folder exists. Something like:


	set destDirSubDir to (every folder of desstDira whose name contains finalString)
	
	if destDirSubDir ≠ {} then
		if (name of item 1 of destDirSubDir contains finalString) then
			move this_file to destDirSubDir
		end if
	end if


In this way I still use your code to get 'every folder', but I then check to see that I got something back (an empty list causes the rest of the code to get skipped).

Assuming something came back in destDirSubDir I then reference item 1 of the list so that the code is now focussing on the folder and not the list itself.


Similar questions

3 replies
Question marked as Top-ranking reply

Jul 29, 2021 12:30 PM in response to Eqe0509

> However, I get an error of error "Can’t get name of {}." number -1728 from name of {}, highlighting FinalString in the last if.


Your script sets destDirSubDir to "every folder of...'.

As such you will get back a LIST of folders - even if there is only one folder. Even worse, if there are no folders with a matching name, you will get an empty list.


You can't ask for name of destDirSubDir if it is either a list, or empty.


In this case I'm guessing there is no such folder, so the list is empty and hence you get the error message about getting the 'name of {}'


So you have to add additional logic to check if such a folder exists. Something like:


	set destDirSubDir to (every folder of desstDira whose name contains finalString)
	
	if destDirSubDir ≠ {} then
		if (name of item 1 of destDirSubDir contains finalString) then
			move this_file to destDirSubDir
		end if
	end if


In this way I still use your code to get 'every folder', but I then check to see that I got something back (an empty list causes the rest of the code to get skipped).

Assuming something came back in destDirSubDir I then reference item 1 of the list so that the code is now focussing on the folder and not the list itself.


Jul 20, 2021 11:10 AM in response to Eqe0509

> My question is, how can I update the script to move the folder into the partially matching folder?


Move what folder? I don't see any folders in your script. All I see (and all AppleScript sees) is a list of text object. Nothing here says they are folders so there is no way to find the directories in question, nor where to move them to.


At the very least a folder needs a path or some other reference to a location on disk.


Peeling back the layers, though, it sounds like you have some locations in mind and you just need the substring matching. This is more easily done via a one-line 'find' shell script:


set sourceDir to "~/sourceDir"
set destDir to "~/DestDir"
set sub_string to "RS_W36_21_NEWS"

do shell script "/usr/bin/find " & sourceDir & " -depth 1 -type d  -name '*" & sub_string & "*' -exec mv {} " & destDir & " \\; "


now, to be fair, find needs some explanation...


  • The first parameter is where to search, so this variable, sourceDir should be the UNIX path for where to look for the folders in to be moved
  • -depth 1 tells it to only look at the top level of this folder (i.e. don't look for matching subfolders, too
  • -type d tells it to only find directories, not files
  • -name ... tells it what filenames to look for. This text has to be quoted (with single quotes in the above example), and the * on either side of the variable make it a wildcard match
  • -exec tells find to run the subsequent command for each found item
  • mv {} tells it to move the found item (as indicted by {} )


so amend the initial variables to match your paths and search strings and it should work for you.



Jul 28, 2021 6:27 AM in response to Camelot

Thanks for the advice Camelot, I believe I'm close to achieving my goal.

This is my current script:




tell application "Finder"
	
	set selected to selection --Select source folder
	set current_folder to item 1 of selected --first item
	set mlist to every file of current_folder --folder as list
	set destDir to POSIX path of ((path to desktop as text) & "destDir:")
	--> /Users/username/Desktop/destDir/
	set destDira to POSIX file destDir as alias
	
	
	repeat with this_file in mlist --for each
		set cur_ext to name extension of this_file
		set strLength to count of characters of this_file
		set new_name to text 9 thru (strLength + (offset of "J" in text) - (length of cur_ext) + 2) of (name of this_file as text) --this is trimming
		
		set AppleScript's text item delimiters to "_"
		set delimCount to (count of text items of (new_name as text))
		if delimCount is greater than 4 then
			set subString to text items 1 thru 4 of new_name
			subString as text
		end if --Trim down to 3 underscores
		
		set myList to subString
		set saveTID to AppleScript's text item delimiters
		set FinalString to myList as text
		set AppleScript's text item delimiters to saveTID
		display dialog FinalString --Convert from list to string
		
		
		
		--Move file if folder contains trimmed string
		set destDirSubDir to (every folder of destDira whose name contains FinalString)
		
		if (name of destDirSubDir contains FinalString) then
			move this_file to destDirSubDir
		end if
		
	end repeat
end tell



It seems the script is 4 parts.

-1. Set up some files and folders, that I'll use later.

-2. Trim the string, remove up to the 3rd underscore. The string is now a list.

-3. Put the list into a string again.

-4. Move the file into a subdirectory, if new string is contained in name of any folders.



However, I get an error of error "Can’t get name of {}." number -1728 from name of {}, highlighting FinalString in the last if.


This seems like it tries to get the name of an empty array.


Move folder into folder, based on partial match of name

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.