Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 03-05-2012, 10:44 AM   #1
nicolanicola
Prospect
 
Join Date: Feb 2010
Posts: 6
scripting help for creating new folders within a bunch of folders

I have a folder structure as follows all items>item name>item colour>files.

Now in this item colour folder are 2 files, a large sized image and a thumbnail version of that image. I need to create a folder in here called thumbnail and move the thumbnail image into it. Can I do this with automator or applescript?

I have maybe about 30 item name folders with various amounts of folders within each of those.

If I was to say what it was in code terms it'd be for every folder in all items,open folder then go in every folder in here and create a directory called thumbnail and move the file named closeupTN.jpg into here.
nicolanicola is offline   Reply With Quote
Old 03-05-2012, 10:50 AM   #2
SirDice
MVP
 
Join Date: Aug 2009
Posts: 1,119
A simple one-liner would do the trick.

Code:
find /base/directory -name '*TN.jpg' -exec cp {} /some/place/where/thumbs/are/stored/ \;
SirDice is offline   Reply With Quote
Old 03-05-2012, 10:55 AM   #3
nicolanicola
Prospect
 
Join Date: Feb 2010
Posts: 6
Quote:
Originally Posted by SirDice
A simple one-liner would do the trick.

Code:
find /base/directory -name '*TN.jpg' -exec cp {} /some/place/where/thumbs/are/stored/ \;

Thanks for this, problem is the pathname will change all the time, as I need a thumbanaill folder in each folder where the files are. I've attached a screenshot of my directory layout for clarity's sake.

For each of the first folders, open, then for each of the second folders, open and create a folder called thumbnail and move closeupTN.jpg into it.
Attached Thumbnails
Click image for larger version

Name:	Screen Shot 2012-03-05 at 16.54.20.png
Views:	77
Size:	57.0 KB
ID:	4467  

Last edited by nicolanicola; 03-05-2012 at 10:58 AM.
nicolanicola is offline   Reply With Quote
Old 03-05-2012, 11:09 AM   #4
SirDice
MVP
 
Join Date: Aug 2009
Posts: 1,119
Find will traverse all subfolders searching for files.
SirDice is offline   Reply With Quote
Old 03-05-2012, 11:12 AM   #5
nicolanicola
Prospect
 
Join Date: Feb 2010
Posts: 6
Is there a 'this' feature in this code? In which case it could be the above code but instead of the path make reference to current directory,or store the create directory path in a variable and then use that variable? I'm a php, actionscript and javascript programmer, so not sure if these coding practices are available for apple scripting.
nicolanicola is offline   Reply With Quote
Old 03-05-2012, 11:13 AM   #6
nicolanicola
Prospect
 
Join Date: Feb 2010
Posts: 6
Quote:
Originally Posted by SirDice
Find will traverse all subfolders searching for files.

Yes, but the second part of the code where you've got /some/place/where/thumbs/are/stored/ that isn't static, is depends on the current folder we are in
nicolanicola is offline   Reply With Quote
Old 03-06-2012, 03:56 AM   #7
SirDice
MVP
 
Join Date: Aug 2009
Posts: 1,119
Quote:
Originally Posted by nicolanicola
Is there a 'this' feature in this code? In which case it could be the above code but instead of the path make reference to current directory,or store the create directory path in a variable and then use that variable?

Read the find man page.

Code:
find $SEARCHPATH -name '*TN.jpg'
Code:
find . -name '*TN.jpg'
SirDice is offline   Reply With Quote
Old 03-06-2012, 06:23 PM   #8
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,181
Yes, there is a 'this' feature. Use -execdir instead of -exec.

If I understand you correctly, you want to search recursively through the folder hierarchy rooted at some starting point, and wherever you find a thumbnail in the same subfolder as the JPEG that it's a thumbnail of, you want to move it (not copy it) one level deeper into the folder hierarchy, into a (possibly new) subfolder named 'thumbnail'. Thus, if you start with the structure:

. start/
. . jacket/
. . . red/
. . . . big.jpg
. . . . bigTN.jpg
. . . . small.jpg
. . . . smallTN.jpg
. . vest/
. . . pink/
. . . . double.jpg
. . . . doubleTN.jpg
. . . . single.jpg
. . . . thumbnail/
. . . . . singleTN.jpg

then you want to end up with

. start/
. . jacket/
. . . red/
. . . . big.jpg
. . . . small.jpg
. . . . thumbnail/ <== new folder
. . . . . bigTN.jpg <== moved
. . . . . smallTN.jpg <== moved
. . vest/
. . . pink/
. . . . double.jpg
. . . . single.jpg
. . . . thumbnail/ <== existing folder
. . . . . doubleTN.jpg <== moved
. . . . . singleTN.jpg <== not moved

As a matter of expediency, instead of testing if the full-size picture is adjacent to a putative thumbnail, I'll use the easier test: a file is a thumbnail that needs to be moved if its name matches *TN.jpg, and it is not already nested in a folder named thumbnail.

Code:
find starting/folder -not \( -type d -iname thumbnail -prune \) -name '*TN.jpg' -execdir mkdir -p thumbnail \; -execdir mv {} thumbnail \;


.
ganbustein is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 09:22 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.