DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: [Complete] Consolidate files in the parent folder  (Read 21,082 times)

Carol Haynes

  • Waffles for England (patent pending)
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 8,073
    • Cody Award
    • Furry Fandom Badge - Furry Fandom
[Complete] Consolidate files in the parent folder
« on: March 13, 2009, 12:04 AM »
I have an odd problem that I need to solve and I am not sure how to solve it.

There may be a simple tool in Windows or a function in DOpus that can achieve this but I haven't found it.

I have a folder full of many subfolders (actually a number of these folder structures) and each subfolder contains files.

All the file names are unique and I would like to move all the files up a level into the parent folder and then delete all the empty subfolders. Like this:

Before:

Folder
.
SubFolder 1   ... SubFolder 2 .... etc
.
File1                 File 2              etc.


After:

Folder
.
File 1    Files 2  etc


I have tried using a command line but you can't use wild cards for folder names.

Anyone got any ideas?
« Last Edit: March 13, 2009, 10:05 AM by Carol Haynes »

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,840
    • Coding Snacks
Re: [Request] Consolidate files in the parent folder
« Reply #1 on: March 13, 2009, 12:57 AM »
you could try this...

Code
fileselectfolder,MyFold,::{20d04fe0-3aea-1069-a2d8-08002b30309d}

loop, %myfold%\*.*,0,1
{
	If a_loopfiledir <> %myfold%
		filemove, %a_loopfilefullpath%,%myfold%
}

loop, %myfold%\*.*,2,1
	fileremovedir, %a_loopfilefullpath%,1
		
exitapp		

prompts for a folder, then moves all files from the subfolders to the root (files in subfolders are identified as having a different directory path than those in the root), then deletes all the subfolders

there are probably more elegant solutions, but this was quick - a brief tests indicates it works as advertised (though maybe best to test it for yourself...)

EDIT: simplified the code
Alcohol is photoshop, bottled
Askhole - someone who ask for advice, then ignores it
"Look wise, say nothing, and grunt. Speech was given to conceal thought" - Sir William Osler
« Last Edit: March 15, 2009, 10:45 PM by Target »

Carol Haynes

  • Waffles for England (patent pending)
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 8,073
    • Cody Award
    • Furry Fandom Badge - Furry Fandom
Re: [Request] Consolidate files in the parent folder
« Reply #2 on: March 13, 2009, 01:40 AM »
Thanks I will give it a try in the morning

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: [Request] Consolidate files in the parent folder
« Reply #3 on: March 13, 2009, 01:42 AM »
i'm, sure you've thought of this carol but one worry is what happens if different files with same name are located in different subdirectories.

Carol Haynes

  • Waffles for England (patent pending)
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 8,073
    • Cody Award
    • Furry Fandom Badge - Furry Fandom
Re: [Request] Consolidate files in the parent folder
« Reply #4 on: March 13, 2009, 01:58 AM »
That wasn't the problem in the case - all the file names were unique.

Couldn't wait until morning - works great. Exactly what I needed in this case ;)

A few pennies winging there way and on Target  :-* :-* :-*

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,840
    • Coding Snacks
Re: [Request] Consolidate files in the parent folder
« Reply #5 on: March 13, 2009, 02:13 AM »
you're most welcome Carol!!

i did consider what Mouser said, but you'd already said the filenames were all unique (I could change it to cater for that if you want...)

and thanks for the credits (not necessary, but appreciated)
Alcohol is photoshop, bottled
Askhole - someone who ask for advice, then ignores it
"Look wise, say nothing, and grunt. Speech was given to conceal thought" - Sir William Osler

exold

  • Do nothing. Time is too precious to waste.
  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 10
    • Military Service Badge - US Army
Re: [Complete] Consolidate files in the parent folder
« Reply #6 on: March 31, 2009, 10:18 AM »
If you're trying to do this somewhere you only have access to CMD.EXE, I'm pretty sure you can use FOR..IN..DO to get the job done. It's a little-known tool that provides a lot of powerful functionality. The UI is crap, though.... :P
---
David A. P.
Ars Arboris -- Art of the Tree.

bootz15

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 17
Re: [Complete] Consolidate files in the parent folder
« Reply #7 on: April 05, 2009, 06:54 PM »
This is awesome! I would really like to see the code that assumes the sub-files are not uniquely named, too.

Ampa

  • I am cute ;)
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 592
Re: [Complete] Consolidate files in the parent folder
« Reply #8 on: April 06, 2009, 01:56 PM »
In Xplorer2 you can flatten a folder structure by selecting the folder and pressing Shift+Enter. This shows all files within the folder and subfolders in a single pane, at which point you can move / rename or do anything else with them.

Perhaps DOpus has something similar?

vixay

  • ViXaY
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 140
    • Coding Snacks
Re: [Complete] Consolidate files in the parent folder
« Reply #9 on: July 15, 2009, 10:42 AM »
I have done some more modifications to suit my purpose, which was to rename the files to include the folder path it had.
i.e.
Quote
root
a
   1
b
   1
   2
would become
Quote
root
a-1
b-1
b-2

here's the code for it
FlattenWithFolderNames.ahk
Code
fileselectfolder,MyFold,::{20d04fe0-3aea-1069-a2d8-08002b30309d}
SetWorkingDir, %MyFold%
loop, *.*,0,1
{
	parentpath := RegExReplace(A_LoopFileDir,"\\","-")
	;StringReplace, parentpath, A_LoopFileDir, \,-,All
	newname = %parentpath%-%A_LoopFileName%
	;msgbox %newname%
	If a_loopfiledir <>
		filemove, %a_loopfilefullpath%,%newname%
}

loop, %myfold%\*.*,2,1
	fileremovedir, %a_loopfilefullpath%,1
		
exitapp
"Drunk on the Nectar of Life!" -me

Carol Haynes

  • Waffles for England (patent pending)
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 8,073
    • Cody Award
    • Furry Fandom Badge - Furry Fandom
Re: [Complete] Consolidate files in the parent folder
« Reply #10 on: July 15, 2009, 11:31 AM »
Neat idea which should avoid naming conflicts

Original address: https://www.donationcoder.com/forum/index.php?topic=17428.0