Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lidocayne

macrumors newbie
Original poster
Jun 15, 2007
5
0
I have a directory with thousands of files and subfolders in it. I'd like to have a script/automator process that will parse the directory and its subfolders.

What I'd like to do is have the script move the files to an appropriate directory based on file creation date.

For example, if I have a file named DCF00213.cr2 created on June 2, 2008 (06-02-08) I'd like the file to be moved to a directory 2008 -> 06 - June (06-June is a subdirectory within 2008). It does not need to create the directory, just move it to the appropriate location. So if another file in the directory was created on Feb 1, 2004 it would move it to 2004 -> 02 - Feb.

Ideally I'd put a limit that this can only be performed on files with .cr2 or .jpg extensions.

Thank you so much... I have basic scripting knowledge, and after combing the forums of this and other sites, I don't seem to be able to find a script like this.

I hope this makes sense, and thank you again!
 

Partron22

macrumors 68030
Apr 13, 2011
2,655
808
Yes
a directory with thousands of files and subfolders in it.
...
have the script move the files to an appropriate directory...
In my experience trying to move more than a few hundred files through the Finder:
Code:
tell application "Finder"
	move files A through Z of the folder src to the folder dst -- pseudocode
end tell
Is terribly slow. Try to move too many files too fast and you will bring the Finder to its knees.
For big operations like you propose, you're far better off going through shell with something like this:
Code:
try
	set result to do shell script "mv " & quoted form of POSIXsrcpath & " " & quoted form of POSIXdstpath
on error -- this is some error OTHER THAN a file name collision
	set end of errorlist to nam & "\r"
end try
 

lidocayne

macrumors newbie
Original poster
Jun 15, 2007
5
0
Thank you Partron22! It appears that I need to do this via a shell script. I guess the first step in a solution is figuring out the foundation (utilizing shell instead of finder).

Unfortunately, my knowledge of shell scripting is worse than AppleScript :eek:

Let the fun begin... any suggestions?
 

sero

macrumors member
Aug 28, 2008
91
14
This will get you started.. practically finished really. But since you're interested and newish to the shell, I'll let you finish it.

Code:
IFS=$'\n'
files=$(ls -lU|egrep '(.cr2|.jpg)')
for i in $files; do
	month=$(echo "$i"|awk '{print $6}')
	getyear=$(echo "$i"|awk '{print $8}')
	if [ $(echo $getyear|awk '{print length}') -lt 5 ]; then
		year=$getyear
	else
		year=$(date "+%Y")
	fi
echo $(echo "$i"|awk '{print $9}') $month $year


do your business here


done
unset IFS

And actually the code above would't work as expected in the second half of a year - I'll leave that for you as well.

Code:
man ls
man date
man mv
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
I assume the files with names like DCF00213.cr2 are all photo files.

I also assume the numbering increases by creation date, i.e. each numbered file is a photo taken after any lower-numbered file. Every digital camera I know of works this way.

So simply show all the files in a List view in a Finder window, and Sort By Name. If the numbers are increasing by creation date, then the sorted view is effectively increasing by creation date. Check the date column to be sure.

Now, simply click and shift-click to select ranges of files. You can see the date column in the Finder window to decide the range. Then drag and drop a selected range to the desired folder.

Personally, I'd move all the shots from 2008 into a separate 2008 folder first, and then move into sub-folders from there.

The numbers don't have to be contiguous. There can be gaps. As long as the numbers increase by the date & time the photo was shot. I.e. no numbers starting over, going down, etc.


If the numbering of the photos isn't always increasing, or there are separate or overlapping ranges of numbering, then the above won't work. But you could then Sort By Date in Finder, and do a similar thing: parcel all the 2008 files into a 2008 folder, then into sub-folders from there.

If only some files have this problem, you could view, sort, and move them separately from the files that do have increasing-by-shot-date numbering.


The utility "A Better Finder Rename" might also be handy here. I'm not sure it will move files, so you should evaluate it rather than taking my word for it.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.