1

I have around 3 TB of recovered data from a broken hard drive. The problem is all the files and folders are completely scrambled and placed into thousands of sub folders.

Question

Is there a series of commands I can run in cmd that finds all files and move them up into the first folder and get rid of empty sub folders along the way?

If the found files can be sorted by file extension and placed into new folder for each extension that would be even better. I'm using a windows 10 computer.

1

Batch Script Solution

You can set the parent folder that contains all the files within the structure you need to copy up to the parent folder as a variable. You can then use a for /r loop and some conditional if logic and variable substitutions to perform the copy, delete, and file extension folder creation operations as needed.

You can use some conditional logic to check if a file to be copied already exist in the destination folder and if so then call a subroutine and pass variable substitutions to it to append _(dupe) to that file name so it'll not overwrite existing files with the same file name.

To quickly remove the empty directories within the parent folder location you can run a Robocopy command such as Robocopy <dest> <dest> /S /MOVE where the destination and source parameters are both the same and equal the full parent folder path.

Batch Script

@ECHO OFF

SET "Parent=C:\Momma"
SET "dupCnt=1"

FOR /R "%Parent%" %%C IN (*) DO (
    IF NOT EXIST "%Parent%\%%~XC" MD "%Parent%\%%~XC"
    IF NOT EXIST "%Parent%\%%~XC\%%~NXC" (
        XCOPY /Y /F "%%~C" "%Parent%\%%~XC\" && DEL /Q /F "%%~C"
    ) ELSE (
        CALL :Routine "%Parent%\%%~XC\%%~NXC" "%%~C" "%%~NC" "%%~XC"
        )
    )
Robocopy "%Parent%" "%Parent%" /S /MOVE
GOTO :EOF

:Routine
IF EXIST "%Parent%\%~4\%~3_(dupe%dupCnt%)%~4" (
    SET /A dupCnt=%dupCnt%+1
    CALL :Routine "%~1" "%~2" "%~3" "%~4"
) ELSE ( 
    IF EXIST "%~1" (
        ECHO F | XCOPY /Y /F "%~2" "%Parent%\%~4\%~3_(dupe%dupCnt%)%~4" && DEL /Q /F "%~2"
        SET "dupCnt=1"
        )
)
GOTO :EOF

Before and After Folder Structure Example

Before

C:\MOMMA
├───Adult-Child
│       Adult-Child.jpg
│       Adult-Child.txt
│       Adult-Child.zzz
│       Baby.txt
│
├───Baby
│       Adult-Child.txt
│       Baby.jpg
│       Baby.txt
│       Baby.zzz
│       Teen.txt
│
└───Teen
        Adult-Child.txt
        Baby.txt
        Teen.jpg
        Teen.txt
        Teen.zzz

After

C:\MOMMA
├───.jpg
│       Adult-Child.jpg
│       Baby.jpg
│       Teen.jpg
│
├───.txt
│       Adult-Child.txt
│       Adult-Child_(dupe1).txt
│       Adult-Child_(dupe2).txt
│       Baby.txt
│       Baby_(dupe1).txt
│       Baby_(dupe2).txt
│       Teen.txt
│       Teen_(dupe1).txt
│
└───.zzz
        Adult-Child.zzz
        Baby.zzz
        Teen.zzz

Further Resources

  • FOR /R
  • IF
  • MD
  • XCOPY
  • DEL
  • CALL

  • Batch Substitutions (FOR /?)

    In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    
  • SET

    Arithmetic expressions (SET /a)

    Placing expressions in "quotes" is optional for simple arithmetic but required for any expression using logical operators.

    Any SET /A calculation that returns a fractional result will be rounded down to the nearest whole integer.

    The expression to be evaluated can include the following operators:

    + Add set /a "_num=_num+5"

  • 1
    outstanding answer – JohnnyVegas Nov 5 '18 at 22:19
0

From How to Copy Files from Multiple Sub-Folders to a Single Folder?

Open a Command Prompt ('cmd.exe') window. Use the following commands:

cd /d "d:\vacation snaps\2016"

for /r %d in (*) do copy "%d" "d:\all snaps"

In the above example, the command recursively copies all files in the 'vacation snaps\2016' folder to the 'All Snaps' folder.

Alternately, use Windows Search. See How to Copy Files from Multiple Sub-Folders to a Single Folder?

  • 2
    What happens when the above script encounters identical filenames? – JohnnyVegas Nov 5 '18 at 16:32
0

Install the free utility 'Everything' from http://voidtools.com/

This quickly indexes every file on your HDD. Open an 'everything' window and look for all files by typing '*.*' or 'driveletter:/folder/*.*

Once this has all the files in the list, select all, then right click and select copy.

Paste into your folder, paying attention to the windows 10 dialogue box that may appear when it tries to paste a duplicate filename. It will give you the option to rename all, placing a number at the end of the filename.

0

FreeCommander XE is a free utility (Windows Explorer alternative) that has a "flat view" functionality to see all files within the directory and all subdirectories.

It also has quite powerful selection/search options, so you can easily select files based on extension and move to a directory of your choice. Of course, it is manual work, but you keep control over what you are doing.

Overall a great tool, which has saved my life quite a few times when working with tons of files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.