I am sorry to say I am not a programmer at all. But from the past I know that I can get help in this forum.
In 2005 I posted in this forum that I need to move files based on the first 10 chars of their filename to folders named to these 10 chars (http://macscripter.net/viewtopic.php?id=13794)
So for the last years I have used this Script with success:
property photos_source_directory : “iMac_HD:Users:christian:!My Photos:Foto_Upload:Neu_und_unsortiert”
property photos_destination_directory : “iMac_HD:Users:christian:!My Photos:Foto_Upload:Sortiert_nach_Datum”
tell application “Finder” to set theFiles to files of alias photos_source_directory
repeat with thisfile in theFiles
tell application “Finder”
set thisfilename to thisfile as string
set x to length of the photos_source_directory
set y to length of the thisfilename
-- LÃ¤nge des Dateinamens Ã¼berprÃ¼fen
if (x + 23) = y then
set day_directory to characters (x + 1) thru (x + 10) of the thisfilename as string
if folder named day_directory in folder photos_destination_directory exists then
-- do nothing
set pub_date_folder to make folder in folder photos_destination_directory
set name of pub_date_folder to day_directory
move thisfile to folder day_directory of folder photos_destination_directory replacing yes
Now with Snow Leopard it does not work at all.
Can you please help or point me to a direction where I can continue …
this should work also in Snow Leopard
property photos_source_directory : "iMac_HD:Users:christian:!My Photos:Foto_Upload:Neu_und_unsortiert:"
property photos_destination_directory : "iMac_HD:Users:christian:!My Photos:Foto_Upload:Sortiert_nach_Datum:"
tell application "Finder" to set theFiles to name of files of folder photos_source_directory
repeat with thisfile in theFiles
set sourceFile to quoted form of (POSIX path of photos_source_directory & thisfile)
do shell script "/usr/bin/ditto " & sourceFile & space & quoted form of (POSIX path of photos_destination_directory & text 1 thru 10 of thisfile & "/" & thisfile)
do shell script "/bin/rm " & sourceFile
Thank you so much. It works like a charme! The new script works in Snow Leopard but what is really astonishing to me - it is much faster then before.
My deep an personal thanks to you! You make this place such a really great forum.
This is no witchcraft. The UNIX shell is closer to the lower level routines than AppleScript.
That means, there is less “translation overhead”
How would the code look like without ditto and rm where the files would be moved insted of copied?
Because if the ditto statement ends with an error (and the file is not copied), I fear it would be deleted by rm.
(Maybe I am wrong)
So I adapted your script to:
do shell script “/bin/mkdir” & space & quoted form of (POSIX path of photos_destination_directory & text 1 thru 10 of thisfile)
do shell script "/bin/mv " & sourceFile & space & quoted form of (POSIX path of photos_destination_directory & text 1 thru 10 of thisfile & “/” & thisfile)
The problem with this: When the destination directory exists the script stops at the mkdir. How can I make a “if not exist folder” statement here in the first line??
add the -p switch
do shell script "/bin/mkdir -p " & quoted form of (POSIX path of photos_destination_directory & text 1 thru 10 of thisfile)
Thank you again very much for this extra quick reply. You have saved my day!