Windows 7 – Find all files that are alone in a folder

file searchfindwindows 7

I need to find a way to select all files that are alone in their folders, so I can cut and paste them someplace else. All respective folders are inside one main root folder. Is there a command prompt, or total commander trick for this?

Best Answer

@echo off
Setlocal EnableDelayedExpansion

SET ROOT_FOLDER=C:\TEST 1
SET TARGET_FOLDER=C:\TEST 2

FOR /D %%G IN ("%ROOT_FOLDER%"\*) do (
CD %%G
    FOR /f %%A in ('dir ^| find "File(s)"') do (
        set cnt=%%A
        Echo %%G  : !cnt!
        IF !cnt! == 1 (
            move /-y "*.*" "%TARGET_FOLDER%"
        )
    )
)

This Batch will look inside C:\TEST 1 sub folders an count files. once it finds a lonely file it'll move it to C:\TEST 2. it will also ask for overwrite in case file name already exist.

Replace C:\TEST 1 and C:\TEST 2 with your own values.

you can add pause at end of the batch to read the files count echo'ed by it.

Related Question