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?

  • What do you want to happen on subdirectories? Do you flag a sole file in a directory which also contains one or more subdirectories? Or a single subdirectory where there are no other files in its parent? – AFH Feb 6 '16 at 17:45
@echo off
Setlocal EnableDelayedExpansion


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.

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

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.