João Cana Verde
João Cana Verde

Reputation: 11

Move files to folder with the same first 5 characters in Applescript

Suppose I have the following files in folder "A":

"AAAAA 1x1", "AAAAA 1x2", "BBBBB 1x1", "BBBBB 1x2", "CCCCC 1x1", "CCCCC 1x2".

And in folder "B", I have the following folders:

"AAAAA", "BBBBB", "CCCCC".

What I'd like to do is move all the "AAAAA" files in folder "A", to folder "AAAAA" in folder "B", "BBBBB" files to folder "BBBBB", and so on.

How would I do this using Apple Script?

Upvotes: 0

Views: 405

Answers (3)

João Cana Verde
João Cana Verde

Reputation: 11

Here's the code (thanks to some fine folks at MacScripter):

set sourceFolder to alias "SSD:Users:JPCanaverde:Documents:A"
set destinationFolder to alias "SSD:Users:JPCanaverde:Documents:B"
tell application "Finder"
   repeat with aFolder in (get folders of destinationFolder)
       set folderName to name of aFolder
       set filesToMove to (files of sourceFolder whose name begins with folderName)
       move filesToMove to (contents of aFolder)
   end repeat
end tell

Upvotes: 0

adayzdone
adayzdone

Reputation: 11238

Try:

set folderA to "/Users/Joao/Desktop/A"
set folderB to "/Users/Joao/Desktop/B"

tell application "System Events"
    set subFolders to folders of (folder folderB)
    repeat with subfolderB in subFolders
        move (files of folder folderA whose name starts with (name of subfolderB)) to (path of subfolderB)
    end repeat
end tell

Upvotes: 0

Lri
Lri

Reputation: 27623

Try running a command like this in Terminal:

for f in A/*; do echo mv "$f" B/${f:2:5}; done

Remove echo to actually move the files.

Upvotes: 1

Related Questions