Page 1 of 1

'Flatten folders' by moving all sub-folders . . .

Posted: 13 May 2010 14:34
by Biggynuff
Hi all,

Started using XYplorer a few weeks ago and I've been searching and searching for how to do this with no luck. Any tips or suggestions would be most welcome 8)

What I have is thousands of photo's in various folders and subfolders, so in a tree view it looks something like this (but MUCH larger!):

New York/city/pic1.jpg
New York/city/pic2.jpg
New York/city/pic3.jpg
New York/city/pic4.jpg

New York/city/library/pic1.jpg
New York/city/library/pic2.jpg
New York/city/library/pic3.jpg
New York/city/library/pic4.jpg

New York/city/buildings/pic1.jpg
New York/city/buildings/pic2.jpg
New York/city/buildings/pic3.jpg
New York/city/buildings/pic4.jpg

What I'd like to be able to do is to search the folder 'New York' for all image files, then move those files into a folder in the root directory which is named the same as the folder the images were originally in. If any of that makes sense? :oops:

So the deepest sub folder with contents (of any kind) is moved to a selected root directory

When opened the root folder would look like is this:

city/pic1.jpg
city/pic2.jpg
city/pic3.jpg
city/pic4.jpg

library/pic1.jpg
library/pic2.jpg
library/pic3.jpg
library/pic4.jpg

buildings/pic1.jpg
buildings/pic2.jpg
buildings/pic3.jpg
buildings/pic4.jpg

So all subfolders with contents have been moved to the selected root directory

I've searched and searched for a method to do this on a selected directory containing many subfolders, but can't find anything that looks possible. I've found the 'show all items in branch' command, which is excellent, but it still means I have to manually go through hundreds of subfolders, copying files and creating new folders for them

Thanks guys and really sorry if this seems like a stupid task, but it would be immensely beneficial to me

Biggy :o

Re: 'Flatten folders' by moving all sub-folders . . .

Posted: 13 May 2010 16:30
by jacky
If I understood correctly, I think this should work.
- go to your folder 'New York' and do your search (e.g. Show All Items In Branch)
- select the files you wanna move
- run this script. it'll ask you for the destination folder, so if you select d:\tmp files will be moved to d:\tmp\city, d:\tmp\library, d:\tmp\buildings, etc

Code: Select all

 $root = inputfolder(<curpath>);
 $list = report("{Path}>{Name}<crlf>", 1);
 $i = 1;
 $files = '';
 while (1)
 {
  $line = gettoken($list, $i, <crlf>);
  if ('' == $line) { break; }
  $path = gettoken($line, 1, '>');
  $file = gettoken($line, 2, '>');
  $folder = regexreplace($path, '^.+\\([^\\]+)$', '$1');
  moveto "$root\$folder", "$path\$file";
  $i++;
 }
I tested it rapidly and it seems to work, but usual stuff: there's no guarantee, and you better do a first little test to make sure it all works as you expect first...

Re: 'Flatten folders' by moving all sub-folders . . .

Posted: 13 May 2010 17:28
by Biggynuff
Jacky,

Thank you so much, I'll load up your script as soon as I'm back near my PC and let you know how I get on

Just doing some research on this problem, I realised how helpful everyone on here are with each other. I can't thank you enough for taking the time to help me with this :lol:

THANK YOU!

Biggy

Re: 'Flatten folders' by moving all sub-folders . . .

Posted: 15 May 2010 13:56
by RalphM
Maybe my understanding of "flattening" folders is different or I misunderstood sth, but wasn't the main goal of this to end up with just one folder with all the files in it?
The way I see it now, it does rather a "Rich Copy" of the search results?!

Re: 'Flatten folders' by moving all sub-folders . . .

Posted: 15 May 2010 14:08
by jacky
RalphM wrote:The way I see it now, it does rather a "Rich Copy" of the search results?!
Not exactly. A Rich Copy would need a root, and then preserver the folder structure below that point. Here he wants folders to be created, but only one, the one files were in.

To be clear, if this is the source:
New York/city/pic4.jpg
New York/city/library/pic1.jpg
New York/city/buildings/pic1.jpg

A Rich Copy would need a source, if set to "New York" then in destination we'd get:
city/pic4.jpg
city/library/pic1.jpg
city/buildings/pic1.jpg

And if set to city, we'd get;
pic4.jpg
library/pic1.jpg
buildings/pic1.jpg

But what he was looking for, I think, is this:
city/pic4.jpg
library/pic1.jpg
buildings/pic1.jpg

So while we're re-creating folders, we're not actually preserving the folder structure as it was on the source, which is what a Rich Operation would do.

Re: 'Flatten folders' by moving all sub-folders . . .

Posted: 15 May 2010 14:29
by RalphM
Sure, right.
As I said, maybe I just misunderstood what Biggy was after, so (s)he has to check, whether it does solve the problem...

Re: 'Flatten folders' by moving all sub-folders . . .

Posted: 15 May 2010 23:33
by Biggynuff
Hi Guys,

I think Jacky has my request for help absolutely spot on, and I'm very grateful for the time taken to try to help me

I've tried to run the script above, but (probably due to my very limited experience) I've not managed to get it to run. The script opens up the 'script box' and seems to go into step mode even though I've not chosen to run in step mode

I've attached an image of the box that pops up:
xyscript.JPG
Anyway, I've actually found a way to do what I needed using the 'show all items in branch' option. I can list all items (including folders) then select sub folders via their path and move them to another directory all at once. It's a solution for the time being, but the thought of a script to do this in one click of a button is very interesting 8)

I've managed to get some scripts I've found on the forum working really well, but with my limited knowledge of scripting I'm having a few problems with some. I'll get there eventually and I'm actually really looking forward to getting into this myself. It seems to me that the possiblities are endless

Thanks for your help

Biggy

Re: 'Flatten folders' by moving all sub-folders . . .

Posted: 16 May 2010 00:09
by Stefan
If you use the menu command "Scripting > Try Script..." its normal to see this debug dialog.
Even for an simple script like
msg hi

To skip this dialog try an right click on the [Continue] button and spot the options.
Or better paste the script into an plain text file with .xys extension and run this.
If you need more help just ask, or read more at the wiki > http://88.191.26.34/XYwiki/index.php/Sc ... Script_.3F

Re: 'Flatten folders' by moving all sub-folders . . .

Posted: 16 May 2010 00:13
by Biggynuff
Thank you Stefan, I'll give your suggestions a try. There's certainly a lot to learn with XY, but I can already see that using this incredible software becomes addictive . . . I'm now looking for ways to do things that I didn't even know I wanted to do :D

Re: 'Flatten folders' by moving all sub-folders . . .

Posted: 16 May 2010 02:42
by j_c_hallgren
Biggynuff wrote:There's certainly a lot to learn with XY, but I can already see that using this incredible software becomes addictive . . . I'm now looking for ways to do things that I didn't even know I wanted to do :D
Hi and belated welcome to the XY forums!

Yes, XY and these forums ARE addictive! For some of us, a day w/o checking in here is like a day w/o sunshine as the ole slogan said.