Organizing files in subdirectories based on filename (DateTime)

Hi,
I’m totally new to Mac and OS X which I enjoy much. :slight_smile:

Although I did a lot of programming in the past under Windows with Visual Basic, I do not have a clue where to start.

On my new Mac under my user I have a directory “Import” where I have lots of files.

The naming convention for these files is YYYY-MM-DD_HH-MM-SS.jpg (-> year,month,day,hour,minute,second)

Eg.

2005-08-16_16-25-55
2005-08-16_18-12-21
2005-08-17_11-42-11
2005-08-18_02-08-15

Now I need a script to move these files to different subdirectories YYYY-MM-DD under “Photos” based on the filename.
These subdirectories have to be created if the do not exist.

2005-08-16_16-25-55 → \Photos\2005-08-16
2005-08-16_18-12-21 → \Photos\2005-08-16

2005-08-17_11-42-11 → \Photos\2005-08-17

2005-08-18_02-08-15 → \Photos\2005-08-18

Can you please help or point me to a direction where I can continue …
Now I am sitting around and surfing the web for hours on this issue.

Thanks,
Christian

It 'aint pretty, but it seems to do what you want
cheers!!
Howard


property photos_archive : alias "hard drive name:Users:username:Desktop:photos:"

on open thefiles
	repeat with thisfile in thefiles
		tell application "Finder"
			set pathname to thisfile as alias
			set the_file_name to thisfile as string
			set AppleScript's text item delimiters to "-"
			set folder_name to words -8 thru -6 of the_file_name as string
			if folder named folder_name in folder photos_archive exists then
				move thisfile to folder folder_name of folder photos_archive replacing yes
			else
				set pub_date_folder to make folder in folder photos_archive
				set name of pub_date_folder to folder_name
				move thisfile to folder folder_name of folder photos_archive replacing yes
			end if
			set AppleScript's text item delimiters to ""
		end tell
	end repeat
end open

:slight_smile: THANK YOU SO MUCH! :slight_smile:

I will try it immediately when I come home this evening and let you know how it worked!
Christian

Once again thank you very much. :slight_smile:
That really did trick!!!

I only had to remove the word “alias” from the first line of code (->syntax error).
But the rest worked well.

How would the program look like if at the start it would take all files from a certain folder?

Regards
Christian

I’m assuming that what you want to do is have an attached folder action to automatically process jobs you stick in a folder? If so it’s pretty easy - you just have to change the first and last lines

You’ll need to save the it as a “script” into you Library/Scripts/Folder Action Scripts folder and attach it to another folder as a folder action

property photos_archive :  "Macintosh HD:Users:yourusername:Desktop:photos:"

on adding folder items to this_folder after receiving thefiles
	repeat with thisfile in thefiles
		tell application "Finder"
			set pathname to thisfile as alias
			set the_file_name to thisfile as string
			set AppleScript's text item delimiters to "-"
			set folder_name to words -8 thru -6 of the_file_name as string
			
			if folder named folder_name in folder photos_archive exists then
				move thisfile to folder folder_name of folder photos_archive replacing yes
			else
				set pub_date_folder to make folder in folder photos_archive
				set name of pub_date_folder to folder_name
				move thisfile to folder folder_name of folder photos_archive replacing yes
			end if
			
			set AppleScript's text item delimiters to ""
		end tell
	end repeat
end adding folder items to

cheers
Howard

THANKS, but not really what I like to do.

I use the standard OS X tool Image Capture for importing my digital images from the camera to the Mac.
When all photos have been imported Image Capture automatically starts a small freeware program called ExifRenamer that reads the Exif Data out of the photo files and then renames each file.

ExifRenamer also has the possibility to start a program when it is finished. Last evening I compiled your script and told ExifRenamer to start it. The conseqeuence was that it only processed a single file.

Therefore I thought it would be a could idea to alter the script at the beginning in that way that it takes all files from folder X (in my case the subfolder called “Import” which I created in my users home folder) for processing.

Regards
Christian

The script is a droplet… were you looking for a script to run by clicking, and it targets your folder?


property photos_archive :  "hard drive name:Users:username:Import:"

tell application "Finder" to set theFiles to files of photos_archive
     repeat with thisfile in thefiles
         tell application "Finder"
             set pathname to thisfile as alias
             set the_file_name to thisfile as string
             set AppleScript's text item delimiters to "-"
             set folder_name to words -8 thru -6 of the_file_name as string
             if folder named folder_name in folder photos_archive exists then
                 move thisfile to folder folder_name of folder photos_archive replacing yes
             else
                 set pub_date_folder to make folder in folder photos_archive
                 set name of pub_date_folder to folder_name
                 move thisfile to folder folder_name of folder photos_archive replacing yes
             end if
             set AppleScript's text item delimiters to ""
         end tell
     end repeat

An adaptation of Mindtpi’s script. Save as application with options never show startup screen, don’t stay open.

SC

I had to change the beginning to

property photos_import : “Internal Harddisc 80GB:Users:fotos:Import:”
property photos_archive : “Internal Harddisc 80GB:Users:fotos:Fotos:”
tell application “Finder” to set theFiles to files of alias photos_import

and now it works.

So as you can see I appended the source folder and appended “alias” to the “tell application …” line.

Once again THANK’S A LOT !

I had not found a solution on my own in such a short time.
Today I ordered an Applescript book at Amazon*'s. (Applescript - The Missing Manual):smiley:

Regards
Christian

I found this book very good (also on Amazon)

AppleScript: A Comprehensive Guide to Scripting and Automation on Mac OS X (Paperback)
by Hanaan Rosenthal

Question for you- I have a very similar project, only my files begin with folder names. So inside my main folder “files” there it contains several files and sub folders for example: 100_august.pdf, 100_manual.pdf, 501_report.pdf, etc… I want these files moved to the corresponding folders 100, 501, etc…

Is it possible to have this script tweeked to work for my case? HELP?!