[NEWB] Moving files to specific folders

Hello all, i am very new to applescript. I worked with it in the past…
But for my database i need to move about 3500 files.

Files with a specific name, that need to be placed in a folder, and then a subfolder with the same name, and then in one of the subfolders.

For example i have Ohio_1997_Example001_Front_FullresThumnail.jpg that needs to be placed in a folder called ALL_PICTURES\Ohio\1997\Example001\jpg

Any idea?

I removed the obsolete versions

Yvan KOENIG (VALLAURIS, France) mardi 8 avril 2014 21:22:40

Thnx…trying to get it work, but i get:
error “ditto: can’t get real path for source” number 1

Maybe i should clarify some things.

I have a main folder, RB_NA with 2 subfolders. JPG and ZOOM. In here are the files that i edited.
The files with a name like for example: Wyoming_2010_Identification Card_AllAges_Default_FrontUV.jpg
has to be placed in another folder: NA_Docs/Wyoming/2010/Identification Card_AllAges_Default/jpg.
And i have to do this for all the States.

It’s your duty to edit the script to match the location of the source files and the location of the destination one.

(1) At this time you said that the source file is named :
Wyoming_2010_Identification Card_AllAges_Default_FrontUV.jpg
This is a filename not a filepath.

As I am not a sooth sayer I can’t guess what is the entire path.
You wrote : I have a main folder, RB_NA with 2 subfolders. JPG and ZOOM.

RB_NA is a folder name, not a folder path

(2) At this time you said that the destination folder is :

NA_Docs/Wyoming/2010/Identification Card_AllAges_Default/jpg.

Must I understand that NA_Docs is the name of harddisk dedicated to the storage of your files or is it, as I guessed, the name of a folder containing the hierarchy : Wyoming/2010/Identification Card_AllAges_Default/jpg ?

You know the answers but I don’t.

Here is a new draft :

# Define the folder in which are stored the files to move

set begOfSourceFolderPath to "path:to:the:folder:theMainFolder"
if begOfSourceFolderPath does not end with ":" then set begOfSourceFolderPath to begOfSourceFolderPath & ":"

# Define the beg of the path to destination hierarchy
set mainDestFolder to "path:of:the:main:folder:of:the:destination:NA_Docs"
if mainDestFolder does not end with ":" then set mainDestFolder to mainDestFolder & ":"
set mainDestFolder to POSIX path of mainDestFolder
tell application "System Events"
	set theFolders to path of every folder of folder begOfSourceFolderPath
	# Loop across the different source folders
	repeat with aFolder in theFolders
		# Loop across the two subfolders containing the picture files
		repeat with folderName in {"JPG:", "ZOOM"}
			set folderName to folderName as text
			if folderName does not end with ":" then set folderName to folderName & ":"
			set sourceFolder to aFolder & folderName
			# Extract the list of picture fileNames
			set theFiles to (name of every file of folder sourceFolder whose type identifier is "public.jpeg")
			# Loop moving the files one by one
			repeat with aFile in theFiles
				my storeIt(aFile, sourceFolder, mainDestFolder, folderName)
			end repeat # aFile
		end repeat # folderName
	end repeat
end tell # System Events

#=====

# Handler treating a file
on storeIt(theSourceName, theSourceFolder, theMainDestFolder, theFolderName)
	# split the name upon the underscore character
	set enListe to my decoupe(theSourceName, "_")
	# build the end of the destination hierarchy
	set endOfPath to my recolle(items 1 thru 2 of enListe, "/") & "/" & my recolle(items 3 thru -2 of enListe, "_") & "/" & text 1 thru -2 of theFolderName & "/" & theSourceName
	set quotedSourcePath to quoted form of POSIX path of (theSourceFolder & theSourceName)
	# Copy the file in the folder hierarchy, creating it if needed
	do shell script "ditto " & quotedSourcePath & space & quoted form of (theMainDestFolder & endOfPath)
	# Delete the source File
	do shell script "rm " & quotedSourcePath
end storeIt

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to "" & l
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

It’s your duty to edit the two instructions :

set begOfSourceFolderPath to “path:to:the:folder:RB_NA”

set mainDestFolder to POSIX path of (“path:of:the:main:folder:of:the:destination:NA_Docs”) # Yesterdays it was “ALL_PICTURES”

replacing the fake pathnames by the true ones.

EDITED ONE MORE TIME TO MATCH THE COMPLEMENTARY INFOS !

Yvan KOENIG (VALLAURIS, France) mercredi 9 avril 2014 11:35:43

Thnx!!, and sorry for being a complte noob :slight_smile:

I got the script to work, and i appreciate your work a lot, but the script renamed my files, which is not good. . So i here are some screen shots of what i need to do. Thnx in advance…

Just requires to edit a handler.

Here is the edited code :

# Handler treating a file
on storeIt(theSourceName, theSourceFolder, theMainDestFolder)
	# split the name upon the undrscore character
	set enListe to my decoupe(theSourceName, "_")
	# build the end of the destination hierarchy
	set endOfPath to my recolle(items 1 thru 2 of enListe, "/") & "/" & my recolle(items 3 thru -2 of enListe, "_") & "/jpg/" & theSourceName
	set quotedSourcePath to quoted form of POSIX path of (theSourceFolder & theSourceName)
	# Copy the file in the folder hierarchy, creating it if needed
	do shell script "ditto " & quotedSourcePath & space & quoted form of (theMainDestFolder & endOfPath)
	# Delete the source File
	do shell script "rm " & quotedSourcePath
end storeIt

I already edited the script in my preceding message.

Yvan KOENIG (VALLAURIS, France) mercredi 9 avril 2014 15:46:40

I got the script to work, but it creates also folders of the back and front. In these folders are the Fullres images situated. The need to be placed in the zoom folder.

But those need to be in the same folder, as in the picture, Commercial Drivers License_AllAges_Default/zoom

Thnx in advance, and thank you very much for helping me out. :slight_smile:

Maybe it’s because English is not my main language but my understanding was that you had ONE folder containing subfolders JPG and ZOOM.

It’s funny to see that you spelled them in UPPERCASE in your questions when they are in lowercase on the disk !

If you got the files in different folders, it’s that the adjustments which you did on your side are wrong.

The edited script is available in the old message.

Yvan KOENIG (VALLAURIS, France) mercredi 9 avril 2014 19:24:59

Yes you are right. I have one folder with JPG and ZOOM, but they need to go to jpg and zoom in the other folders…

How must I understand that ?
Must I move ".:jpg:“Ohio_1997_Example001_Front_FullresThumnail.jpg” in “.:Ohio:1997:jpg:”
and move ".:ZOOM:“Ohio_1997_Example001_Front_FullrezThumnail.jpg” in “.:Ohio:1997:zoom:”

or, as I am currently doing

move ".:jpg:“Ohio_1997_Example001_Front_FullresThumnail.jpg” in “.:Ohio:1997:jpg:”
and move ".:ZOOM:“Ohio_1997_Example001_Front_FullrezThumnail.jpg” in “.:Ohio:1997:jpg:”

It’s always the same rule : WHEN A PROBLEM IS CORRECTLY DESCRIBED IT’S QUITE SOLVED.

Given the incompletely described problem, I spent at least twice the required time on it !

Yvan KOENIG (VALLAURIS, France) jeudi 10 avril 2014 09:53:18

This is the right answer.

".:jpg:“Ohio_1997_Example001_FrontThumnail.jpg” in “.:Ohio:1997:jpg:”
".:jpg:“Ohio_1997_Example001_Front.jpg” in “.:Ohio:1997:jpg:”

and move ".:ZOOM:“Ohio_1997_Example001_Front_Fullres.jpg” in “.:Ohio:1997:zoom:”

Thank you for your time…

My patience is not infinite !

I edited an other time the script available above.

As I am a bit tired I didn’t converted the JPG and ZOOM folder names in lowercase.

Yvan KOENIG (VALLAURIS, France) jeudi 10 avril 2014 18:43:05

Thank you very much, it works!! :slight_smile: Thank you for your time and patience :smiley:

Oops

I forgot to enable the instruction supposed to delete the original files.
I commented it out for my tests so that the original files remain available if a version doesn’t behave correctly.

Yvan KOENIG (VALLAURIS, France) vendredi 11 avril 2014 12:00:36

Ok, do i need to edit the script for that?

If you don’t want to keep the originals in their « old » location it may be done by a simple change in the script.
I edited the script above accordingly.

But by choice would be to leave the delete instruction disabled.
This way you have the ability to check that the files installed in their new location are in good state.
I coded the delete because in your first message you asked for the « move » action and when Finder moves a file, if the destination is on the same volume than the origin, the file is no longer available in the original location.
If both locations are on different volumes, after executing Move we have two copies.
As I didn’t knew which is the real case and wishing to keep my originals for testing differernt versions of the script, I choose to disable the delete instruction.

In short, I’m an old ape trying to play safe.

Yvan KOENIG (VALLAURIS, France) vendredi 11 avril 2014 21:24:51

Thnx for the editing, but now i get this error.

error "System Events got an error: folder " Macintosh HD: Users: admin: Desktop: TEST: RB_NA: JPG: JPG: \ “can not be retrieved.” number -1728 from folder "Macintosh HD: Users: admin: Desktop TEST: RB_NA: JPG: JPG "

It seems the script is searching for another JPG folder…

PS i made a test folder on my desktop first to test the new script…

I’m unable to guess what you did.
Here the script behave flawlessly.

I just added two lines so that it uses the correct locations.


# Define the folder in which are stored the files to move

set begOfSourceFolderPath to "path:to:the:folder:theMainFolder" # Macintosh HD:Users:¢¢¢¢¢¢¢¢¢¢:Desktop:theMainFolder:
set begOfSourceFolderPath to (path to desktop as text) & "theMainFolder" # ADDED
if begOfSourceFolderPath does not end with ":" then set begOfSourceFolderPath to begOfSourceFolderPath & ":"

# Define the beg of the path to destination hierarchy
set mainDestFolder to "path:of:the:main:folder:of:the:destination:NA_Docs" # Macintosh HD:Users:¢¢¢¢¢¢¢¢¢¢:Desktop:NA_Docs:
set mainDestFolder to (path to desktop as text) & "NA_Docs" # ADDED

When I read your error message :
error "System Events got an error: folder " Macintosh HD: Users: admin: Desktop: TEST: RB_NA: JPG: JPG: \ “can not be retrieved.” number -1728 from folder "Macintosh HD: Users: admin: Desktop TEST: RB_NA: JPG: JPG "

I think, that you added some strings in my code.
My own script is never building a path with JPG used twice.

Clearly, you made odd changes.

I repeat that you just have to edit two instructions, the one defining begOfSourceFolderPath and the one defining mainDestFolder.

If you are unable to do that, here is a short script to help you

set maybe to choose folder
if maybe is false then error number -128
(maybe as text)
set the clipboard to result

Run it once navigating to the source folder.
You will have the exact pathname in the clipboard so you will be able to paste it in the instruction defining begOfSourceFolderPath.

Run it a second time to navigate to the destination folder.
The clipboard will contain the wanted path.
Paste it in the instruction defining mainDestFolder.

The pathnames of the files in the source are supposed to be:

/Users/¢¢¢¢¢¢¢¢¢¢/Desktop/theMainFolder/RB_NA/ZOOM/Ohio_1997_Example001_Front_FullresTagada.jpg’
which is moved by :

do shell script “ditto ‘/Users/¢¢¢¢¢¢¢¢¢¢/Desktop/theMainFolder/RB_NA/ZOOM/Ohio_1997_Example001_Front_FullresTagada.jpg’ ‘/Users/¢¢¢¢¢¢¢¢¢¢/Desktop/NA_Docs/Ohio/1997/Example001_Front/ZOOM/Ohio_1997_Example001_Front_FullresTagada.jpg’”
then deleted by :
do shell script “rm ‘/Users/¢¢¢¢¢¢¢¢¢¢/Desktop/theMainFolder/RB_NA/ZOOM/Ohio_1997_Example001_Front_FullresTagada.jpg’”

/Users/¢¢¢¢¢¢¢¢¢¢/Desktop/theMainFolder/RB_NA/JPG/Brésil_1997_Example001_Front_FullresThumnail - copie.jpg’
which is moved by :
do shell script “ditto ‘/Users/¢¢¢¢¢¢¢¢¢¢/Desktop/theMainFolder/RB_NA/JPG/Brésil_1997_Example001_Front_FullresThumnail - copie.jpg’ ‘/Users/¢¢¢¢¢¢¢¢¢¢/Desktop/NA_Docs/Brésil/1997/Example001_Front/JPG/Brésil_1997_Example001_Front_FullresThumnail - copie.jpg’”
then deleted by :
do shell script “rm ‘/Users/¢¢¢¢¢¢¢¢¢¢/Desktop/theMainFolder/RB_NA/JPG/Brésil_1997_Example001_Front_FullresThumnail - copie.jpg’”

Yvan KOENIG (VALLAURIS, France) lundi 14 avril 2014 11:24:38