Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to find and delete all folders within C:\parent which contain only one file. The folder structure is flat (no subfolders):

C:\parent\folder2\file1 <-- Delete folder2
C:\parent\folder3\file1 <-- Delete folder3

Anyone can recommend a program / script to do this? As I'm not very familiar with terminal commands, it would be great to get some kind of walkthrough..


share|improve this question
up vote 4 down vote accepted

Simple enough with PowerShell:

cd C:\Parent
Get-ChildItem | Where-Object { $_.IsPSContainer -and @(Get-ChildItem $_).Count -eq 1 } | Remove-Item -Recurse


  • The second line consists of multiple commands, each of which has its output sent to the next one using the | (pipe) character.

  • Get-ChildItem returns a list of all files and folders in the current folder.

  • Where-Object allows us to filter that list, to get only the folders that match the criteria. $_ refers to the current object for each iteration.

    • $_.IsPSContainer returns true only for folders, so this allows us to exclude any files in the parent directory.

    • @(Get-ChildItem $_).Count -eq 1 is true only for folders that have exactly 1 file or subfolder inside them. The @ sign is necessary for the Count property to work correctly when there's only 1 item (see here for the explanation).

  • Finally, Remove-Item deletes each folder that made it through the filter. The -Recurse parameter is needed to automatically delete non-empty folders; without it, PowerShell would prompt you each time.

share|improve this answer

Here's another possibility using a small Processing program:

String parentFolder = "M:\\INSERT_PARENT_DIR_HERE";

void setup(){
  File fParentFolder = new File(parentFolder);
  println("Scanning " + fParentFolder.getAbsolutePath());
  println("Folder exists: " + fParentFolder.exists());
  File[] folderList = fParentFolder.listFiles();
  println("Number of files/folders: " + folderList.length);
  for(int i=0; i<folderList.length; i++){
    if(folderList[i].isDirectory() && folderList[i].list().length < 2){
      println("Deleting directory: " + folderList[i].getAbsolutePath() + "\t\t" + deleteDir(folderList[i]));  

// Deletes all files and subdirectories under dir.
// Returns true if all deletions were successful.
// If a deletion fails, the method stops attempting to delete and returns false.
public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i=0; i<children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;

    // The directory is now empty so delete it
    return dir.delete();
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .