move content of two folders into subfolders

I have 2 folders:

  1. one called XML Files with many XML files SSS-888-12344.xml
  2. the second called Covers which has the covers for those XML files with the same name, so for SSS-888-12344.xml the cover if SSS-888-12344.jpg.

what I want to do is move the both the XML and cover in a separate folder. So, for SSS-888-12344.xml and SSS-888-12344.jpg, the folder will be called SSS-888-12344 and has (SSS-888-12344.xml and SSS-888-12344.jpg).

Any ideas or suggestions would help.

Thanks.

I don’t have the time to work up an example at the moment, but this isn’t overly difficult.

1: Set your paths (source xml, source jpg, destination for new folders)
2: Loop through either source xml or jpg folders
3: Strip the extension, to get your base name
4: Create folder from base name at destination
5: Move base name .xml and base name .jpg to destination
6: More items? Return to 2

So there is a rough flowchart. If no one beats me to it I will work up an example if my day decides to slow down.

Hi,

assuming both source folders are on desktop:

tell application "Finder"
	set XMLfolder to folder "XML Files"
	set COVERfolder to folder "Covers"
	set XMLlist to (get items of XMLfolder)
	set destFolder to make new folder at desktop with properties {name:"XML_Cover_Destination"}
end tell
repeat with i in XMLlist
	set {name:Nm, name extension:Ex} to info for (i as alias)
	if Ex is missing value then set Ex to ""
	if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
	try
		set f to (destFolder & Nm) as alias
	on error
		tell application "Finder" to set f to make new folder at destFolder with properties {name:Nm}
	end try
	tell application "Finder"
		move i to f
		move (1st item of COVERfolder whose name begins with Nm) to f
	end tell
end repeat

Stephan,

Thanks for you help on this.
I tried running your script by making surethe folder names match the folder names in the script but keep getting an error:
Can’t get folder “XML Files”.

I am not a good scripter myself, any suggestions?

Thanks.

I think I found my mistake. Thisis emberassing, but if I moved folders to dektop “like you suggested in the beginning of your post” it will work fine.
It hangs when it runs into issue like, an XML file that don’t have a cover.

again, many thanks to all of you.

If the folders are not on desktop, you must specify the whole path like this:

set XMLfolder to folder ((path to home folder as Unicode text) & "path:to:XML Files:")

Note: path to home folder points to the home folder of the current user

Edit: you can avoid hanging if the corresponding cover file doesn’t exist with


...
tell application "Finder"
	move i to f
	try
		move (1st item of COVERfolder whose name begins with Nm) to f
	end try
end tell
...

Stefan,

I have tried the edit you suggested, and works just fine. Thanks again.

Here’s the final script for any one interested:

tell application “Finder”
set XMLfolder to folder “XML Files”
set COVERfolder to folder “Covers”
set XMLlist to (get items of XMLfolder)
set destFolder to make new folder at desktop with properties {name:“XML_Cover_Destination”}
end tell
repeat with i in XMLlist
set {name:Nm, name extension:Ex} to info for (i as alias)
if Ex is missing value then set Ex to “”
if Ex is not “” then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
try
set f to (destFolder & Nm) as alias
on error
tell application “Finder” to set f to make new folder at destFolder with properties {name:Nm}
end try
tell application “Finder”
move i to f
try
move (1st item of COVERfolder whose name begins with Nm) to f
end try
end tell
end repeat