Friday, November 16, 2018

#1 2018-11-05 09:12:52 pm

belinha
Member
From:: Brasil
Registered: 2017-03-27
Posts: 9

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:

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)


Filed under: Subfolders, files, Move, text list

Offline

 

#2 2018-11-06 10:57:47 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 358

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

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

 

#3 2018-11-06 01:10:09 pm

belinha
Member
From:: Brasil
Registered: 2017-03-27
Posts: 9

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

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

 

#4 2018-11-06 08:00:50 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 358

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

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

 

#5 2018-11-07 07:09:47 am

belinha
Member
From:: Brasil
Registered: 2017-03-27
Posts: 9

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

Yes, this is it !

Thank you very much for your kind help. smile

Offline

 

#6 2018-11-07 11:05:11 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 358

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

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

 

#7 2018-11-07 01:50:31 pm

belinha
Member
From:: Brasil
Registered: 2017-03-27
Posts: 9

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

I tested it.

It also works very well !

Thank you very much. smile

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)