Move files to parent folder

Hello there,

I have several thousand images, sorted like this;

/Random-folder-name-#1/images/1.jpg
/Random-folder-name-#1/images/2.jpg
/Random-folder-name-#1/images/3.jpg
/Random-folder-name-#2/images/1.jpg
/Random-folder-name-#2/images/2.jpg
/Random-folder-name-#2/images/3.jpg
/Random-folder-name-#3/images/1.jpg
/Random-folder-name-#3/images/2.jpg
/Random-folder-name-#3/images/3.jpg
/Random-folder-name-#4/images/1.jpg
/Random-folder-name-#4/images/2.jpg
/Random-folder-name-#4/images/3.jpg

… and so on.
I would like to move all images in the sub-folder /images/ to the root, and then delete the folder /images/. It would then look something like this;

/Random-folder-name-#1/1.jpg
/Random-folder-name-#1/2.jpg
/Random-folder-name-#1/3.jpg
/Random-folder-name-#2/1.jpg
/Random-folder-name-#2/2.jpg
/Random-folder-name-#2/3.jpg
/Random-folder-name-#3/1.jpg
/Random-folder-name-#3/2.jpg
/Random-folder-name-#3/3.jpg
/Random-folder-name-#4/1.jpg
/Random-folder-name-#4/2.jpg
/Random-folder-name-#4/3.jpg

I tried to find a way to do this with Automator, but with no luck. Any help solving this, I’ll ove a big thanks… and maybe a hug too :slight_smile:

Model: PowerBook G4, PPC, 1,67GHz
Browser: Safari 420) OmniWeb/v607.17
Operating System: Mac OS X (10.4)

Hi Jocke,

welcome to MacScripter :slight_smile:

You can do it with this script

set theFolder to choose folder
tell application "Finder"
	move (get items of theFolder) to container of theFolder
	delete theFolder
end tell

It works - on one folder. How can I make a batch so it does it to all the folders? The way it is now, I have to chose the folder /images/ in order for it to work.
I tried to fire it up in Automator, and I’ve made a “Find Finder-objects”-action, where it searches for the folder “images”, and it returns all the /images/ folders.
I then added a “Perform AppleScript-procedure”, but I don’t know how to make the AppleScript use the previous results… If I use the script you posted, StefanK, the “Choose-folder”-window pops up (logically because of the code “set theFolder to choose folder”), and I’m back to square one.

Thanks for the “welcome” btw, and a very quick response :slight_smile:

Try this:

choose folder with multiple selections allowed

repeat with thisFolder in result
	tell application "Finder"
		move items of thisFolder to container of thisFolder
		delete thisFolder
	end tell
end repeat

With the following you can specify a parent folder,
then the script searches for all folders “images” in this folder (and subfolders)
and prompts the user to confirm each folder before moving the files.
If you don’t want the prompt, comment out or delete the lines starting with display dialog, if and end if

set theParentFolder to choose folder
set theFolders to paragraphs of (do shell script "find " & quoted form of POSIX path of theParentFolder & " -name images -type d")
repeat with i in theFolders
	set currentFolder to POSIX file i as alias
	tell application "Finder"
		display dialog "Move items of folder " & (currentFolder as Unicode text) & "?" buttons {"Cancel", "Skip", "Move"}
		if button returned of result is "Move" then
			move (get items of currentFolder) to container of currentFolder
			delete currentFolder
		end if
	end tell
end repeat

Can we tell I don’t do much finder scripting? LOL Anyways here was the much more complex solution I came up with though I wouldnt use it LOL

Assuming a path like this

blah blah/some folder#1/images/1.jpg
blah blah/some folder#1/images/2.jpg
blah blah/some folder#1/images/3.jpg
blah blah/some folder#2/images/1.jpg
blah blah/some folder#2/images/2.jpg
blah blah/some folder#2/images/3.jpg
blah blah/some folder#3/images/1.jpg
blah blah/some folder#3/images/2.jpg
blah blah/some folder#3/images/3.jpg

And you want to end up with

blah blah/some folder#1/1.jpg
blah blah/some folder#1/2.jpg
blah blah/some folder#1/3.jpg
blah blah/some folder#2/1.jpg
blah blah/some folder#2/2.jpg
blah blah/some folder#2/3.jpg
blah blah/some folder#3/1.jpg
blah blah/some folder#3/2.jpg
blah blah/some folder#3/3.jpg

this code should work. When asked to select the folder choose the folder that contains your some folders, ie… blah blah

set the_base_folder to choose folder
set {ATID, AppleScript's text item delimiters} to {"", ":"}
tell application "Finder"
	set all_images to every file of entire contents of folder the_base_folder as alias list
	repeat with the_image in all_images
		set image_path to the_image as string
		set path_elements to text items of image_path
		set character_count to (count of item -1 of path_elements) + (count of item -2 of path_elements) + 2
		set move_path to text 1 through -character_count of image_path
		move file image_path to folder move_path
	end repeat
	set all_folders to every folder of entire contents of folder the_base_folder as alias list
	repeat with the_folder in all_folders
		if name of the_folder is "images" then delete folder the_folder
	end repeat
end tell
set AppleScript's text item delimiters to ATID

James, a handler would be nice:

on parentFolder for someItem
	-- someItem should be an alias, a file specification, or text
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	try
		set someItem to ("" & someItem) -- coerce to text
		if text -1 thru -1 of someItem is ":" then
			-- someItem is a folder
			set someItem to (text 1 thru text item -3 of someItem) & ":"
		else
			set someItem to (text 1 thru text item -2 of someItem) & ":"
		end if
	end try
	set AppleScript's text item delimiters to ASTID
	return someItem
end parentFolder

StefanK, you could stay in the shell if you wanted:

choose folder
set sourceFolder to quoted form of (text 1 thru -2 of POSIX path of result) -- remove trailing slash for find

do shell script "/usr/bin/find " & sourceFolder & " -type f -iname '*.txt' -path '*/images/*' -execdir /bin/mv {} .. \\; " & ¬
	"; /usr/bin/find " & sourceFolder & " -type d -maxdepth 2 -name 'images' -exec /bin/rm -r {} \\;" -- Recursive rm needed to handle .DS_Store files

This is really amazing, Bruce, but I think, I don’t want. This is an AppleScript Forum ;);):wink:

I went for this one, since I’m tired after a long day of studies/boring stuff, and I didn’t wanna fiddle around removing stuff in the scripts - cus I know that would 99% likely fuck things up.

Anyhow, the above script seems to work - it’s currently running atm. Thanks guys - and good luck with the “I can write the same script with less lines then you”-war that seems to be going on.

This is never a war, just going in for sports :wink:

Stefan speaks the truth.

Btw, while we’re at it, I know this might be a slight off-topic, but I was wondering if you guys know about some applescript (or similar) that I can run in Automator, so that I can load URL’s from an .txt-file? As for now, I haven’t found any other way then manually adding url’s by pressing plus (+), and copy one and one url over. Do that with… ehm… 179 url’s, and you’ll get tired along the way…

EOD (end of day) so the brain is foggy… you have a text file containing urls and you want to do what?

Nah, not only your fault. I explained the “problem” in a occword way. I have a text file containing urls, that i wanna use with Automator’s Safari-actions (“Fetch URL-links from webpages”, ruffly translated to english). In order to use this action, Automator needs “URL’s”, wich can only, AFAIK, be given by entering them manually into Automator. I’ve tried several ways to “bypass” this, f.ex. to open a txt-file, read the contents (url’s), copy to clipboard, and then use the “Fetch URL-links from webpages”-action, but with no luck.

Basically I need an action, or applescript, that “converts” the url’s in a text file, to “Automator-URL’s”, so that Safari-actions can use them.

Feel like I’m living on the edge of offtopic-ness atm, so lemme know and shoot me if i break the rules.

Well perhaps a new thread might be in order, but I guess the question is “What are you ultimately trying to do?”

I do think Automator is a very cool tool, but I personally rarely use it. Depending on what your trying to do there might be easier methods with actual applescript because, like you, I’m not aware of a way to feed urls to the automator action either… though that itself could perhaps be gui scripted.

Yeah, I were an inch away from making a new thread. Thou, I’d like to clarify if it’s a job for AS, or Automator;

Basically, the url’s from the txt-files, are links to gallery-overviews of different image-sites. What I want Automator to do, is fetch the url for those galleries (wich has been tested, works fine). Only thing I was hoping for, was to save some time not having to manually enter all the url’s into Automator.

LOL, okay perhaps yes a new thread is order so if you want to make one that would be great, like I said though EOD so if you could perhaps an example would be best.

I’m still, I’m afraid, a bit fuzy on what you’re looking for so if you could spell it out a bit that would be great… Like read URL xyz.com from the file then have safari do blah blah etc… Like I said I’m still not positive what your trying to do and to what end.

Sorry :frowning:

Hihi, I gotta get up 0600 in the morning (it’s 0110 now), so I think we both need some sleep. I’ll make a new thread tomorrow, explaining a bit more in detail what it is I’m looking for.

Btw, IF your head should, in some ockword way, put itself together, and figure out what I mean, or what I’m looking for, don’t sit up waiting for me to create a new thread before you findt it right to make a post - you might forget it before I even get the slightest chance of making that thread.

Hi,

Hoping someone can pick up on this (I know it’s an old thread) - I’m trying to use the script from James Nierodzik above but I’d like to use it on multiple folders with different levels of subfolders:

Root/Parent1/Sub#1/Images
Root/Parent2/Sub#1/Sub#2/Images
Root/Parent3/Sub#1/Sub#2/ImagesSub#/3Images
Root/Parent3/Sub#1/Images
Root/Parent4/Sub#1/Images/Sub#2/ImagesSub#3/Images

…and make it:

Root/Parent1/Sub#1/Images
Root/Parent2/Sub#1/Images
Root/Parent3/Sub#1/Images
Root/Parent4/Sub#1/Images

set the_base_folder to choose folder
set {ATID, AppleScript's text item delimiters} to {"", ":"}
tell application "Finder"
	set all_images to every file of entire contents of folder the_base_folder as alias list
	repeat with the_image in all_images
		set image_path to the_image as string
		set path_elements to text items of image_path
		set character_count to (count of item -1 of path_elements) + (count of item -2 of path_elements) + 2
		set move_path to text 1 through -character_count of image_path
		move file image_path to folder move_path
	end repeat
	set all_folders to every folder of entire contents of folder the_base_folder as alias list
	repeat with the_folder in all_folders
	end repeat
end tell
set AppleScript's text item delimiters to ATID

I see I can select each Parent Folder and it runs well, but if possible, I’d like to select the Root Folder and it run through all Parent Folders.

Thanks in advance for any thoughts!