Moving files out of subfolders based on extension

I am very new to AppleScript, and so I have tried searching the forums, but I’m not sure what keywords to use. Maybe someone here can help me. :slight_smile:

I have project files that I must download that come in folders. Those folders go into a folder named with the customer’s e-mail address. Some of the files from the subfolders need to be moved up a level into the one with the customer’s name, and everything else needs trashing. Normally, I would just do this by hand, but when I have to do 50+ in a day… I think it could be scripted.

Below are some images that I hope will help explain this. (Sorry, I couldn’t get the [img] tags to work; maybe they’re disabled.)

How it starts: http://i530.photobucket.com/albums/dd342/farethoughts/AppleScript%20Testing/Before.png

How it should end: http://i530.photobucket.com/albums/dd342/farethoughts/AppleScript%20Testing/After.png

The numbers in the file names change, but the letters are always the same.

So, I found this script online, and it starts part of the process, but it moves everything, not just the files I need:


set main_folder to (choose folder with prompt "Choose the main folder")
tell application "Finder"
	set sub_folders to folders of main_folder
	repeat with each_folder in sub_folders
		move every file of each_folder to main_folder with replacing
	end repeat
end tell

So, is this too specific for AppleScript to do? I just haven’t been able to find a command that would allow me to select just the files I need. Even if I could just grab the eps and jpegs, that would be a great start.

Thanks for any ideas.

Model: MacBook Pro Duo Core
AppleScript: 2.1.2
Browser: Firefox 3.6.12 GTB7.1
Operating System: Mac OS X (10.6)

Hi,

select the main_folder in a finder window and then run the script …
if you call the script by a shortcut, for example you can use the programm spark to handle this, it’ll be pretty nice …


--set main_folder to (choose folder with prompt "Choose the main folder") --uncomment this line if you want to use the choose folder command

tell application "Finder"
	set main_folder to (selection as alias)--delete this line, if you want to use the first line (choose folder ...)
	
	set sub_folders to folders of main_folder
	repeat with each_folder in sub_folders
		move (every file of each_folder whose file type is "JPEG" or file type is "EPSP") to main_folder with replacing
	end repeat
	try
		delete every item of sub_folders
	end try
end tell

Thanks for the reply and the script. Unfortunately, that deletes everything but the main folder. I suspect that no files are being recognized by their file type of JPEG or EPSP.

hi,

if your files always have a suffix you can replace the move… with this line:

		move (every file of each_folder whose name extension is in {"jpg", "eps"}) to main_folder with replacing

Have a nice day :slight_smile:

Hans

Hans,

Thanks for the help. That is very close. The only thing left to do is delete all files with:

1.) Name starts with L

AND

2.) Is a jpeg.

Is that possible? I tried this, but it doesn’t do anything more than your wonderful code does:



set main_folder to (choose folder with prompt "Choose the main folder") --uncomment this line if you want to use the choose folder command

tell application "Finder"
	--set main_folder to (selection as alias) --delete this line, if you want to use the first line (choose folder ...)
	
	set sub_folders to folders of main_folder
	repeat with each_folder in sub_folders
		
		move (every file of each_folder whose name extension is in {"jpg", "eps"}) to main_folder with replacing
	end repeat
	
	try
		delete every item of sub_folders
	end try
	
	try
		delete (every item whose {file name begins with "L", name extension is "jpg"})
	end try
	
end tell

If that’s too much for AS to do, I’m just thrilled with the help you’ve provided me.

Thanks again.

Sorry,
didn’t have a look at your screenshots …:confused:

Hope this works:


set main_folder to (choose folder with prompt "Choose the main folder")

tell application "Finder"
	
	set sub_folders to folders of main_folder
	repeat with each_folder in sub_folders
		
		move (every file of each_folder whose name extension is in {"eps"}) to main_folder with replacing
		move (every file of each_folder whose name extension is in {"jpg"} and name does not start with "L") to main_folder with replacing
	end repeat
	
	try
		delete every item of sub_folders
	end try
	
end tell

Woohoo! That’s it. Thank you so much! Truly, just seeing the way you wrote the script has helped me understand some of the language of AppleScript.

So, if I can ask you for one final thing, do you have a source you could recommend to help me learn AppleScript?

Thanks again. You are wonderful.