• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 744
  • Last Modified:

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
0
jdana
Asked:
jdana
  • 6
  • 6
  • 5
  • +3
2 Solutions
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
paultomasiCommented:
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
 
paultomasiCommented:
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
 
paultomasiCommented:
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
 
paultomasiCommented:
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
 
paultomasiCommented:
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
 
paultomasiCommented:
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 6
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now