Link to home
Start Free TrialLog in
Avatar of jdana
jdanaFlag for United States of America

asked on

Possible to flatten a directory structure?

I was involved in a digital forensics project recently where I honed my list of "files of interest" to 125 and submitted my request to forensics company for the 125 files.  They shipped me the files in the original directory structure.  So each file is 4 to 7 directories deep.  What a pain.  Is there a trick where I can effectively tell Windows, "Here's the parent directory.  Take all files in sub folders of the parent directory and move them to the parent directory.  Take all files in sub-sub folders of the parent directory and move them to the parent directory.  Take all files in sub-sub-sub folders of the parent directory and move them to the parent directory."  For example:

Project Folder\Active\C\$Recycle.Bin\S-1-5-21-1805092725-1900085117-2720843392-1217\Corrected draft 61210.doc
Becomes
Project Folder\Corrected draft 61210.doc

Project Folder\Active\C\Users\joe_user\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Outlook\VSJKRSWU\Key issued form.doc
Becomes
Project Folder\Key issued form.doc
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill Prew
Bill Prew

Take out the

    rd "%%~fB"

line, that was from a different question.

~bp
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@ReneGe

The original question wanted to move all files under "C:\Project" up into "C:\Project", not to a different folder.  Using your approach if both source and dest are set tto the same folders, the files contained in that folder (not subfolders) will generate an error as trying to copy a file to itself.  That's why I didn't process those files...

~bp
@bp,

I guess I do not get what you mean.  Maybe it's because I'm kind of tired.

To answer to what I think you want to say; my script will copy all files contained in the "Source" folder and it's sub-directories, to the destination folder. Therefore, they are not set to the same folder and will not copy to them selves.

Have I missed something?

Your feedbacks are always appreciated.

Cheers,
Rene
@Rene

Notice these from the original request:

"Here's the parent directory.  Take all files in sub folders of the parent directory and move them to the parent directory.  Take all files in sub-sub folders of the parent directory and move them to the parent directory.  Take all files in sub-sub-sub folders of the parent directory and move them to the parent directory."

. . . . .

Project Folder\Active\C\$Recycle.Bin\S-1-5-21-1805092725-1900085117-2720843392-1217\Corrected draft 61210.doc
Becomes
Project Folder\Corrected draft 61210.doc

~bp
I get it now. I was just too focussed on how I would approach this, than how his request was made.

Thanks for taking the time to explain.

Cheers,
Rene
Open a window in Explorer and navigate to Project Folder\

Open a second window in Explorer and navigate to Project Folder\

In the first folder, do a Windows search for *.*

Select all the results

Right-click on the selected body of files, drag to first window, and tell it to move the files there.
jdana

No need to move you r files whatsoever...

Using Explorer (Windows' File Explorer), simply navigate to the 'parent folder'. In the 'Folders' pane, right-click on the parent folder and select 'Search...' from the menu.

Now enter *.* or *.doc in the 'files to search for' box remember to tick the 'include sus-folders' option.

This will now show ALL your files in a single pane on the right.... Now, just drag-and-drop or double-click or whatever...

Does that solve the issue?
any reason why you repeated the suggestion given 4 hours before Paul?!
Oops! Apologies... I only saw:

   >>"Open a window in Explorer..."

   >>"Open a second window..."

   >>"...and tell it to move the files there"

However, what i'm actually saying is that there is no need to MOVE the files if it's just a case of having all the files accessible from a single location.

If an application's 'Open File's' dialog box is normally used to access files then rather than using 'Open File' from the 'File' menu, just simply drag and drop each file from Explorer to the application.

On the otherhand, if for some reason the files can only be opened using 'File Open' from the 'File' menu then having all the files in a single location would indeed make things somewhat easier.



jdana

The problem with moving files from a sub-folder based file structure to just a single folder is that some filenames may clash with others. As you probably know, you cannot have two files with identical names in the same folder.

So, when considering moving files to a single folder, you must account for duplicate filenames. Microsoft's approach is to append a bracketed number onto the end of the filename like this:

   filename.ext
   filename (2).ext
   filename (3).ext
   etc...



From a programmer's point of view, billprew addressed the issue of copying files onto themselves - those in the "parent folder" (or root folder), but work still needs to be done regarding duplicate filenames.
Perhaps something along these lines would be better suited:

@echo off
set rootfolder=Project Folder

for /d %%a in ("%rootfolder%\*") do (
   for /r "%%a" %%b in (*) do (
      if exist "%rootfolder%\%%~nxb" (
         call :duplicatefile "%%b"
      ) else (
         move "%%b" "%rootfolder%\"
      )
   )
)
exit /b


:duplicatefile
   set count=0

   :loop
      set /a count+=1
   if exist "%rootfolder%\%~n1 (%count%)%~x1" goto :loop

   move "%~dpn1 (%count%)%~x1" "%rootfolder%"
goto :eof

Open in new window

Avatar of jdana

ASKER

ReneGe,

I suppose  billprew is correct, your solution doesn't fit my requirements to the letter.  On the other hand...

1. It works beautifully.
2. It consists of a mere three lines of code.  (I love that.)
3. It takes me about half a second to move the files to the original directory.

Thanks!
Glad I could help!
Well, I could have done it in one line of code :-) if I choose to ignore what was actually requested...

FOR /R "C:\Project Folder" %%A  IN (*.*) DO COPY "%%~fA" "C:\Destination"

Open in new window

~bp
Hahaha....

The mind boggles!

Must remember to write to Billy and ask him to rewrite MS-DOS - I can see how sub-directories can be a real hindrance!

lol

How many times am I going to be beaten up by the acronym 'KISS' (Keep It Simple, Stupid!)?
That remids me, something like 15 year ago a software cie called "KISS", and guess what was there slogan?

Cheers
You're kidding me! lol
Nope!

I remember that there solutions were greate and so easy to use, it was amazing!

But they are long gone!

Cheers