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.

Script to move files from a folder to different folder in another directory that shares part of the same name as file

Hi,


I have 1000's of .csv files and .jpg files in multiple folders in a directory that are named RE07150144_0000000001_20150604151907Z.csv, RE47140151_0000000001_20150423155209Z.jpg, etc. The first 10 digits of the filenames are the device name. I also have hundreds of folders in a different directory that are named after devices: RE07150144 , RE07150145, etc.


I'd like a way to move the individual .csv files into their respective device named folder. Clueless when it comes to scripting and haven't had any lucking finding something that has already been put together for this purpose (or I'm just clueless with searching too).


Any help appreciated.


Regards,

Bart

MacBook Pro (Retina, 15-inch, Mid 2014), OS X Yosemite (10.10.5)

Posted on Sep 22, 2015 7:51 PM

Reply
8 replies

Sep 22, 2015 8:41 PM in response to BartC30

This is pretty straightforward. The following script should get you started:


set sourceFolder to (choose folder)


set destFolder to "path:to:destination"


tell application "Finder"



-- iterate through the files:

repeat with eachFile in (every file of folder sourceFolder)

try


-- extract the first 10 characters of the file name

set first10Chars to (characters 1 through 10 of name of eachFile) as text


-- do we have a matching folder name?

if existsfolderfirst10Chars of folderdestFolder then


-- if so, move the file there


moveeachFiletofolderfirst10Chars of folderdestFolder

end if

end try

end repeat

end tell


It will ask you for the source folder - the folder that contains all the files in question. You need to edit the second line to represent the path to the folder where the device folders are (or change this to another 'choose folder' statement to select it when you run the script.

Sep 23, 2015 6:54 AM in response to Camelot

Hi Camelot! First, thanks so much for the quick response! Truly appreciated.

I think I left out a very important piece of the puzzle. The destFolder (device folders) are also nested in different folders in a parent directory. They are all 2 levels down from the parent directory under a different "Feeder" number (eg. 101138). Sorry for omitting this as I think from looking through your script, that would have to be implicitly stated to somehow search down into the Feeder folders.

User uploaded file

Sep 23, 2015 12:03 PM in response to BartC30

Well, that does complicate things a bit 🙂


Is there any structure to where the device folder might be? any hints/shortcuts to use? or do you have to scan through all the first-level directories to find the second-level device folder?


It might not make a difference, depending on how many options there are. I'm just thinking of the time it will take to run if there are thousands of folders it has to scan through.


The simplest change would be to iterate through the folders in destFolder until you find a match. It's inefficient, but a quick hack. If it's too slow, post back and I'll look at optimization options:


set sourceFolder to (choose folder)


set destFolder to "path:to:destination"


tell application "Finder"



-- iterate through the files:

repeat with eachFile in (every file of folder sourceFolder)

try


-- extract the first 10 characters of the file name

set first10Chars to (characters 1 through 10 of name of eachFile) as text


-- do we have a matching folder name?

repeat with each_folder in (every folder of folder destFolder)

if existsfolderfirst10Chars of foldereach_folder then


-- if so, move the file there


moveeachFiletofolderfirst10Chars of folderdestFolder

exit repeat

end if

end repeat

end try

end repeat

end tell

Sep 23, 2015 12:08 PM in response to Camelot

Two minutes after posting I thought of one optimization - this turns the logic on its head...


In the first case I iterated through the files, looking for folders to put them in.

In this version I start with the folders, iterating through all the folders and gathering files that match the first 10 characters.


Which one is faster depends on the ratio of files to folders. If there are a small(it) number of folders then this gathering approach should be faster:


set sourceFolder to (choose folder)


set destFolder to "path:to:destination"


tell application "Finder"



-- get the top-level folders:


repeat with each_folder in (every folder of folder destFolder)


-- iterate through the folders:

repeat with each_device_folder in (every folder of each_folder)

try


-- extract the first 10 characters of the folder name

set first10Chars to (characters 1 through 10 of name of each_device_folder) as text


-- we have a folder name, do we have any matching files?


move (every file of foldersourceFolder whose name begins with first10Chars) tofoldereach_device_folder

end try

end repeat

end repeat

end tell

Sep 23, 2015 2:10 PM in response to Camelot

I tried your second script since we only have about 500 Feeder Folders versus 8000 files to move to them. Unfortunately, it errored out.


tell application "Script Editor"

choose folder


--> alias "Macintosh HD:Users:bartc:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms:Result s:Merge_Outages:Dan_Files:FEEDER_CSV:"

choose folder


--> alias "Macintosh HD:Users:bartc:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms:Result s:Merge_Outages:FEEDER_copy:"

end tell

tell application "Finder"

count every folder of folder (alias "Macintosh HD:Users:bartco:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms:Resul ts:Merge_Outages:FEEDER_copy:")


--> 583

count every folder of item 1 of every folder of folder (alias "Macintosh HD:Users:bartc:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms:Result s:Merge_Outages:FEEDER_copy:")


--> error number -1728 fromitem 1 of folder "FEEDER_copy" of folder "Merge_Outages" of folder "Results" of folder "Scripts_to_pull_waveforms" of folder "Scripts" of folder "FPL" of folder "Utilities" of folder "Documents" of folder "bartc" of folder "Users" of startup disk

Result:

error "Finder got an error: Can’t get item 1 of folder \"FEEDER_copy\" of folder \"Merge_Outages\" of folder \"Results\" of folder \"Scripts_to_pull_waveforms\" of folder \"Scripts\" of folder \"FPL\" of folder \"Utilities\" of folder \"Documents\" of folder \"bartc\" of folder \"Users\" of startup disk." number -1728 fromitem 1 of folder "FEEDER_copy" of folder "Merge_Outages" of folder "Results" of folder "Scripts_to_pull_waveforms" of folder "Scripts" of folder "FPL" of folder "Utilities" of folder "Documents" of folder "bartc" of folder "Users" of startup disk

Sep 23, 2015 3:45 PM in response to BartC30

Grrr, the Finder can be so picky sometimes 🙂


Try this version:


set sourceFolder to (choose folder)


set destFolder to path to"path:to:destination"


tell application "Finder"



-- get the top-level folders:

repeat with each_folder in (every folder of folder destFolder as alias list)


-- iterate through the folders:

repeat with each_device_folder in (get every folder of each_folder as alias list)

try


-- extract the first 10 characters of the folder name

set first10Chars to (characters 1 through 10 of (get name of each_device_folder)) as text


-- we have a folder name, do we have any matching files?

move (get every file of folder sourceFolder whose name begins with first10Chars) to folder each_device_folder

end try

end repeat

end repeat

end tell

Sep 23, 2015 7:33 PM in response to Camelot

Camelot, you are a prince! (See what I did there?)🙂


It went way further this time, looking like it processed a few hundred files but eventually, it tired of what it was doing and timed out..

.......many lines of the bolded content below and then the non-bolded


HD:Users:bartcournane:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms: Results:Merge_Outages:FEEDER_copy:706666:", alias "Macintosh HD:Users:bartcournane:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms :Results:Merge_Outages:FEEDER_copy:706861:", alias "Macintosh HD:Users:bartcournane:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms :Results:Merge_Outages:FEEDER_copy:706862:", alias "Macintosh HD:Users:bartcournane:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms :Results:Merge_Outages:FEEDER_copy:706863:", alias "Macintosh

HD:Users:bartcournane:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms: Results:Merge_Outages:FEEDER_copy:707532:", alias "Macintosh HD:Users:bartcournane:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms :Results:Merge_Outages:FEEDER_copy:707533:"}

get every folder of alias "Macintosh HD:Users:bartcournane:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms :Results:Merge_Outages:FEEDER_copy:101131:"

--> {alias "Macintosh HD:Users:bartcournane:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms :Results:Merge_Outages:FEEDER_copy:101131:RE11150100:", alias "Macintosh HD:Users:bartcournane:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms :Results:Merge_Outages:FEEDER_copy:101131:RE11150112:"}

get name of alias "Macintosh HD:Users:bartcournane:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms :Results:Merge_Outages:FEEDER_copy:101131:RE11150100:"

--> "RE11150100"

get every file of folder (alias "Macintosh HD:Users:bartcournane:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms :Results:Merge_Outages:Dan_Files:FEEDER_CSV:") whose name starts with "RE11150100"

get name of alias "Macintosh HD:Users:bartcournane:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms :Results:Merge_Outages:FEEDER_copy:101131:RE11150112:"

get every folder of alias "Macintosh HD:Users:bartcournane:Documents:Utilities:FPL:Scripts:Scripts_to_pull_waveforms :Results:Merge_Outages:FEEDER_copy:101137:"

Result:

error "Finder got an error: AppleEvent timed out." number -1712



I appreciate your work and determination (and general coolness) but I had to pass this endeavor on to one of our developers since it was a work related item and it was a bit time sensitive. Thanks a million!

Sep 23, 2015 9:04 PM in response to BartC30

Hello


You might try the following AppleScript, which is a simple wrapper of shell script.



set src to (choose folder with prompt "Choose source (csv, jpg) folder")'s POSIX path set dst to (choose folder with prompt "Choose destination (Feeders) folder")'s POSIX path set args to src's quoted form & space & dst's quoted form do shell script "/bin/bash -s <<'EOF' - " & args & " SRC=$1 DST=$2 find -E \"$SRC\" -type f -iregex '.*/(..........)_[^/]*$' -print0 | while read -d $'\\0' f do # get destination name ($m) from file name ($n) of $f n=${f##*/} [[ $n =~ (..........)_.*$ ]] || continue m=${BASH_REMATCH[1]} # get destination directory ($d) d=$(find \"$DST\" -type d -iname \"$m\" | head -n1) [[ -n $d ]] || continue # check current parent directory of $f [[ ${f%/*} == $d ]] && continue # skip if already moved # move $f to $d mv \"$f\" \"$d\" done EOF"



Briefly tested under OS X 10.6.8 but no warranties of any kind. Please make sure you have backup of directories before running this sort of script.



Regards,

H

Script to move files from a folder to different folder in another directory that shares part of the same name as file

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