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
jdanaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
Save this as a BAT, adjust the BaseDir as needed to point to your root folder, and run it.

@echo off
setlocal EnableDelayedExpansion
set BaseDir=c:\Project Folder
for /D %%A in ("%BaseDir%\*") do (
  set Dirname=%%~A
  for /R "!Dirname!" %%B in (.) do (
    move "%%~fB\*.*" "%BaseDir%\"
    rd "%%~fB"
  )
)
pause

Open in new window

~bp
0
Bill PrewCommented:
Take out the

    rd "%%~fB"

line, that was from a different question.

~bp
0
ReneGeCommented:
This will copy all files in the source directory and it's subdirectories, directly to the destination folder.

Once happy, you can manually delete the source folder and all within it.

@ECHO OFF
SET Source=C:\Project Folder
SET Dest=C:\Destination
FOR /R "%Source%" %%A  IN (*.*) DO COPY "%%~fA" "%Dest%"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Bill PrewCommented:
@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
0
ReneGeCommented:
@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
0
Bill PrewCommented:
@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
0
ReneGeCommented:
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
0
ChopOMaticCommented:
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.
0
Paul TomasiCommented:
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?
0
Steve KnightIT ConsultancyCommented:
any reason why you repeated the suggestion given 4 hours before Paul?!
0
Paul TomasiCommented:
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.
0
Paul TomasiCommented:
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

0
jdanaAuthor Commented:
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!
0
ReneGeCommented:
Glad I could help!
0
Bill PrewCommented:
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
0
Paul TomasiCommented:
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

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

Cheers
0
Paul TomasiCommented:
You're kidding me! lol
0
ReneGeCommented:
Nope!

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

But they are long gone!

Cheers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 7

From novice to tech pro — start learning today.