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

$src = "G:\temp"
$target = "G:\notalone"

if (Test-Path $src)
{
    $folders = Get-ChildItem $src -Recurse | ?{ $_.PSIsContainer }
    foreach($folder in $folders)
    {
        $fc = Get-ChildItem $folder.FullName | ?{!$_.PSIsContainer} | Measure-Object | Select-Object -Expand Count
        if ($fc -eq 1)
        {
            $file = Get-ChildItem $folder.FullName | ?{!$_.PSIsContainer} | Select-Object
            Write-Host "Moving " $file.FullName " to " $target
            Move-Item $file.FullName $target
        }
    }
}

This should work in Powershell, replace src and target. If you have the same filenames, it will not overwrite. You can add force to Move-Item to make that happen.

This could probably be condensed, I'm novice with powershell.


@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.