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
Windows 7Microsoft DOSWindows Server 2008

Avatar of undefined
Last Comment
ReneGe
SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
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
Avatar of ReneGe
ReneGe
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Bill Prew
Bill Prew

@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
Avatar of ReneGe
ReneGe
Flag of Canada image

@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
Avatar of Bill Prew
Bill Prew

@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
Avatar of ReneGe
ReneGe
Flag of Canada image

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
Avatar of ChopOMatic
ChopOMatic
Flag of United States of America image

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.
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

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?
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

any reason why you repeated the suggestion given 4 hours before Paul?!
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

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
jdana
Flag of United States of America image

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!
Avatar of ReneGe
ReneGe
Flag of Canada image

Glad I could help!
Avatar of Bill Prew
Bill Prew

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
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

How many times am I going to be beaten up by the acronym 'KISS' (Keep It Simple, Stupid!)?
Avatar of ReneGe
ReneGe
Flag of Canada image

That remids me, something like 15 year ago a software cie called "KISS", and guess what was there slogan?

Cheers
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

You're kidding me! lol
Avatar of ReneGe
ReneGe
Flag of Canada image

Nope!

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

But they are long gone!

Cheers
Windows Server 2008
Windows Server 2008

Windows Server 2008 and Windows Server 2008 R2, based on the Microsoft Vista codebase, is the last 32-bit server operating system released by Microsoft. It has a number of versions, including including Foundation, Standard, Enterprise, Datacenter, Web, HPC Server, Itanium and Storage; new features included server core installation and Hyper-V.

86K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo