You are not logged in.
Pages:: 1
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:
Atestado Source Folder
coberto.xls
lapis.jpg
sapato.doc
Novo subfolder
canarinha.doc
canarinha.xls
picapau.xls
chuveiro.wma
Antigos subfolder
canarinha.jpg
lembranca.txt
picapau.doc
laranja.doc
madarina.mp4
Arquivado subfolder
picapau.png
banana.xls
89.txt
futuro subfolder
porta-486.jpg
melao.mov
janela245.xls
tenedor.mp4
lapis.wmv
lembranca.doc
panela.png
chuveiro.txt
torneira.doc
My text list is like this:
lapis.jpg
canarinha.xls
madarina.mp4
picapau.png
banana.xls
89.txt
porta-486.jpg
melao.mov
janela245.xls
chuveiro.txt
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.
Applescript:
# 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
Last edited by belinha (2018-11-05 09:44:58 pm)
Offline
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.
Applescript:
# 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
#=====
Hackintosh built February, 2012 | Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb
Offline
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
Offline
Try this:
Applescript:
# 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
#=====
Hackintosh built February, 2012 | Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb
Offline
Yes, this is it !
Thank you very much for your kind help.
Offline
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.
Applescript:
# 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
#=====
Hackintosh built February, 2012 | Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb
Offline
I tested it.
It also works very well !
Thank you very much.
Offline
Pages:: 1