Move files from a List (source folder and its subfolders)

The below code was written by Mr. Yvan Koenig, from this post:
https://macscripter.net/viewtopic.php?id=45654

and it works beautifully.
Moving files from one source folder to other target folder based on a text list. Exactly what I needed.
But recently the structure of my source folder changed containing various subfolders inside.

Is there a way the code implement search the files in the subfolders, too?

The new structure of my source folder is like this:

My text list is like this:

My target folder is only one (no subfolders inside)

The source folder and the target folder are in the same external hard disks.

Some files have same name but different extension
my text list contains the names with extension like:
lapis.jpg
canarinha.xls
picapau.png
chuveiro.txt
(move only these ones)

Sorry for the long post.
highly appreciated your help.


# folders on different devices
set path2source to choose folder with prompt "Select the source folder" default location (path to desktop)
set path2dest to choose folder with prompt "Select the destination folder" default location (path to desktop)
set path2list to choose file with prompt "Select your textList" of type {"txt"}

set theList to paragraphs of (read path2list)

tell application "System Events"
   set theFiles to path of files of path2source
   set sourceList to {}
   repeat with aFile in theFiles
       if (name of disk item aFile) is in theList then
           set end of sourceList to my quotedIt(aFile)
       end if
   end repeat
end tell

do shell script "mv " & my recolle(sourceList, space) & space & quoted form of POSIX path of path2dest

#=====

on quotedIt(aFile)
   return quoted form of POSIX path of aFile
end quotedIt

#=====

on recolle(l, d)
   local oTIDs, t
   set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
   set t to l as text
   set AppleScript's text item delimiters to oTIDs
   return t
end recolle

#=====

Model: macbrook pro 2010
AppleScript: 2.6.1
Browser: Safari 9.1.3
Operating System: macOS 10.9

This works with folders if the nesting only goes one level deep.

If you may have multiple levels of nested folders, it could be modified to be recursive.


# folders on different devices
set path2source to choose folder with prompt "Select the source folder" default location (path to desktop)
set path2dest to choose folder with prompt "Select the destination folder" default location (path to desktop)
set path2list to choose file with prompt "Select your textList" of type {"txt"}

set theList to paragraphs of (read path2list)

tell application "System Events"
	set theFiles to path of files of path2source
	set theFolders to every folder of path2source
	repeat with aFolder in theFolders
		set theFiles to theFiles & (the path of files of aFolder)
	end repeat
	set sourceList to {}
	repeat with aFile in theFiles
		if (name of disk item aFile) is in theList then
			set end of sourceList to my quotedIt(aFile)
		end if
	end repeat
end tell

do shell script "mv " & my recolle(sourceList, space) & space & quoted form of POSIX path of path2dest

#=====

on quotedIt(aFile)
	return quoted form of POSIX path of aFile
end quotedIt

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Thank you very much for your kind reply.

As you said, I have multiple levels of folders. I forgot to mention it, sorry.

How could make it recursive in order to search those subfolders, too?

Appreciate your help.

Model: macbrook pro 2010
AppleScript: 2.6.1
Browser: Safari 9.1.3
Operating System: macOS 10.9

Try this:


# folders on different devices
set path2source to choose folder with prompt "Select the source folder" default location (path to desktop)
set path2dest to choose folder with prompt "Select the destination folder" default location (path to desktop)
set path2list to choose file with prompt "Select your textList" of type {"txt"}


set theList to paragraphs of (read path2list)
global theFiles
set theFiles to {}

tell application "System Events"
	my addSubfolderItems(path2source)
	set sourceList to {}
	repeat with aFile in theFiles
		if (name of disk item aFile) is in theList then
			set end of sourceList to my quotedIt(aFile)
		end if
	end repeat
end tell

do shell script "mv " & my recolle(sourceList, space) & space & quoted form of POSIX path of path2dest


#=====

on addSubfolderItems(aFolder)
	tell application "System Events"
		set theFiles to theFiles & (the path of files of aFolder)
		set theFolders to the folders of aFolder
	end tell
	repeat with someFolder in theFolders
		addSubfolderItems(someFolder)
	end repeat
end addSubfolderItems

on quotedIt(aFile)
	return quoted form of POSIX path of aFile
end quotedIt

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Yes, this is it !

Thank you very much for your kind help. :slight_smile:

I don’t write recursive handlers much, and so far when I do I’ve just been using global variables to avoid having to handle information passing through the levels of recursion.

Just for the heck of it, I thought I should figure out how to do this without a global variable. Here’s that copy, in case it’s of use to you. The end result should be identical.



# folders on different devices
set path2source to choose folder with prompt "Select the source folder" default location (path to desktop)
set path2dest to choose folder with prompt "Select the destination folder" default location (path to desktop)
set path2list to choose file with prompt "Select your textList" of type {"txt"}


set theList to paragraphs of (read path2list)
set theFiles to {}

tell application "System Events"
	set theFiles to my addSubfolderItems(path2source, theFiles)
	set sourceList to {}
	repeat with aFile in theFiles
		if (name of disk item aFile) is in theList then
			set end of sourceList to my quotedIt(aFile)
		end if
	end repeat
end tell

do shell script "mv " & my recolle(sourceList, space) & space & quoted form of POSIX path of path2dest


#=====

on addSubfolderItems(aFolder, theFiles)
	tell application "System Events"
		set theFiles to theFiles & (the path of files of aFolder)
		set theFolders to the folders of aFolder
	end tell
	repeat with someFolder in theFolders
		set theFiles to addSubfolderItems(someFolder, theFiles)
	end repeat
	return theFiles
end addSubfolderItems

on quotedIt(aFile)
	return quoted form of POSIX path of aFile
end quotedIt

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====


I tested it.

It also works very well !

Thank you very much. :slight_smile:

Was wondering if the reverse could be done? How could the list then be used to sort all those files back into their appropiate folders?