DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Last post Author Topic: Does any folder in tree have more than one file in it?  (Read 28,077 times)

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Does any folder in tree have more than one file in it?
« on: February 25, 2019, 01:06 PM »
Given a large directory tree full of folders, maybe thousands, each and every folder is ONLY supposed to have one single file within it.

If there is any folder with more than a single file within it, I need to take a look at what the additional file(s) is/are.

The task of following each and every branch, and opening every folder to have a peek is enormously daunting, really time consuming.

Instead, ideally, software can go throughout and do the checking.

If more than one file is in a single folder, create a link to that folder, to form a list of links.

After the process, one can click on a link to automatically open said folder in explorer.

An additional option might be to show the number of files found.  But not so necessary as simply providing the link to the folder.

Any programming help from you wizards will be greatly appreciated.

As far as existing solutions out there, TreeSize sort of appears helpful.  However, two issues with TreeSize:  1. It chokes before even showing a list of folders.  2. There is no way of telling it to ONLY show folders with MORE THAN ONE file within it; it shows ALL folders.

Thoughts and suggestions much appreciated.

Nicholas Kormanik

c.gingerich

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 748
    • Participated in NANY Challenge Six Times
    • Coding Snacks
Re: Does any folder in tree have more than one file in it?
« Reply #1 on: February 25, 2019, 01:11 PM »
I think I can whip something up. Give me a few

c.gingerich

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 748
    • Participated in NANY Challenge Six Times
    • Coding Snacks
Re: Does any folder in tree have more than one file in it?
« Reply #2 on: February 25, 2019, 01:49 PM »
Here is a quick and dirty app I call More Than One File. Extract mtof.exe and run. Enter or browse for the root folder and press Process. It'll add any sub-folder that has more than one file to the list. Double click the list item to open that folder.

Download - https://cl.ly/41efdf

Version History
1.2.27.2019 - Updated to include ALL sub-folders under the root [recursive list].
1.2.25.2019 - Initial release

92740998.png

« Last Edit: February 27, 2019, 02:47 PM by c.gingerich »

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: Does any folder in tree have more than one file in it?
« Reply #3 on: February 26, 2019, 01:55 AM »
c.gingerich, beautiful work.  Close, but not quite.  mtof.exe is a keeper as is.  But the program requested must make a distinction between "files" and "folders."

mtof.exe presently groups the two together.

Does any folder have more than a single "file" within it?

Perhaps Windows doesn't allow for the distinction.

By the way, I tried again with TreeSize, and left it running overnight.  Still was not able to show a refreshed window with results by morning.  Just sat there.  Duh.  And this the Cadillac of such processing.



c.gingerich

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 748
    • Participated in NANY Challenge Six Times
    • Coding Snacks
Re: Does any folder in tree have more than one file in it?
« Reply #4 on: February 26, 2019, 02:03 PM »
But the program requested must make a distinction between "files" and "folders."

I guess I am not understanding what you mean. mtof does not include folders when counting, it only counts "files" that are in the sub-folders under the root folder. Maybe you can show me what you mean with a screen shot?

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: Does any folder in tree have more than one file in it?
« Reply #5 on: February 27, 2019, 12:50 AM »
My bad.  Not sure how to word it.  Top-level, as opposed to out in a branch?

Please see images:

Garth.jpg

In the case of Garth, there are two files nested.  Only one in any particular folder.

Wende.jpg

In the case of Wende, there are also two files nested.  But they reside in one single folder.

mtof.exe treats both of these scenarios the same.

It's the Wende case I want to flag.



« Last Edit: February 27, 2019, 12:58 AM by nkormanik »

c.gingerich

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 748
    • Participated in NANY Challenge Six Times
    • Coding Snacks
Re: Does any folder in tree have more than one file in it?
« Reply #6 on: February 27, 2019, 12:53 AM »
Ok so you want it to go deeper than one folder if I am understanding.

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: Does any folder in tree have more than one file in it?
« Reply #7 on: February 27, 2019, 01:02 AM »
I had to modify my post above, to get the images right.  See if that helps to explain.

Using an actual tree analogy, only ONE BIRD is allowed to rest on any ONE BRANCH.  If TWO BIRDS are resting together on any single branch, must call 911.  (Police state, ya know.)
« Last Edit: February 27, 2019, 02:23 AM by nkormanik »

c.gingerich

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 748
    • Participated in NANY Challenge Six Times
    • Coding Snacks
Re: Does any folder in tree have more than one file in it?
« Reply #8 on: February 27, 2019, 02:48 PM »
@nkormanik - I have updated mtof. Please give it a try and let me know.

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Does any folder in tree have more than one file in it?
« Reply #9 on: March 01, 2019, 02:11 AM »
Something simple in PowerShell because I wanted to see how to get things into a ListView - was pretty easy after I dumped trying to get it all into a multi-dimensional array first Woohoo! worked out how to create one  :D

2019-03-04 20_57_02-EBO.png

EBO.ps1
Code: ( powershell)
<# This form was created using POSHGUI.com  a free online gui designer for PowerShell
.NAME
    EBO.ps1
#>

#region begin GUI{ 
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '400,579'
$Form.text                       = "EBO"
$Form.BackColor                  = "#b8e986"
$Form.TopMost                    = $false

$ListView1                       = New-Object system.Windows.Forms.ListView
$ListView1.View                  = 'Details'
$ListView1.MultiSelect           = $false
$ListView1.GridLines             = $true
$ListView1.width                 = 369
$ListView1.height                = 442
$ListView1.Anchor                = 'top,right,bottom,left'
$ListView1.location              = New-Object System.Drawing.Point(15,100)

$TextBox1                        = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline              = $false
$TextBox1.width                  = 268
$TextBox1.height                 = 20
$TextBox1.location               = New-Object System.Drawing.Point(15,27)
$TextBox1.Font                   = 'Microsoft Sans Serif,10'

$Button1                         = New-Object system.Windows.Forms.Button
$Button1.text                    = "..."
$Button1.width                   = 28
$Button1.height                  = 25
$Button1.location                = New-Object System.Drawing.Point(291,26)
$Button1.Font                    = 'Microsoft Sans Serif,10'

$Button2                         = New-Object system.Windows.Forms.Button
$Button2.text                    = "Start"
$Button2.width                   = 60
$Button2.height                  = 25
$Button2.location                = New-Object System.Drawing.Point(325,26)
$Button2.Font                    = 'Microsoft Sans Serif,10'

$Button3                         = New-Object system.Windows.Forms.Button
$Button3.text                    = "Death by File Size"
$Button3.width                   = 140
$Button3.height                  = 25
$Button3.location                = New-Object System.Drawing.Point(15,63)
$Button3.Font                    = 'Microsoft Sans Serif,10'

$Button4                         = New-Object system.Windows.Forms.Button
$Button4.text                    = "Death by Bit Rate"
$Button4.width                   = 140
$Button4.height                  = 25
$Button4.location                = New-Object System.Drawing.Point(244,63)
$Button4.Font                    = 'Microsoft Sans Serif,10'

$Label1                          = New-Object system.Windows.Forms.Label
$Label1.Text                     = 'Double-click folder to open'
$Label1.AutoSize                 = $true
$Label1.Width                    = 100
$Label1.Height                   = 10
$Label1.Anchor                   = 'bottom,left'
$Label1.Location                 = New-Object System.Drawing.Point(120,552)
$Label1.Font                     = 'Microsoft Sans Serif,10'

$ToolTip1                        = New-Object System.Windows.Forms.ToolTip
$ToolTip1.AutomaticDelay         = 500
$ToolTip1.IsBalloon              = $true
$ToolTip1.SetToolTip($Button4, "Keep highest bit rate file")
$Form.controls.AddRange(@($Button4))

$ToolTip2                        = New-Object System.Windows.Forms.ToolTip
$ToolTip2.AutomaticDelay         = 500
$ToolTip2.IsBalloon              = $true
$ToolTip2.SetToolTip($Button3, "Keep largest file")
$Form.controls.AddRange(@($Button3))

$ToolTip3                        = New-Object System.Windows.Forms.ToolTip
$ToolTip3.AutomaticDelay         = 500
$ToolTip3.IsBalloon              = $true
$ToolTip3.SetToolTip($Button1, "Select folder")
$Form.controls.AddRange(@($Button1))

$Form.controls.AddRange(@($ListView1,$TextBox1,$Button1,$Button2,$Button3,$Button4,$Label1))

#region gui events {
$ListView1.Add_DoubleClick({
  Start-Process ($ListView1.SelectedItems).Text
})

$Button3.Add_Click({
  Murder-Them Size
})

$Button4.Add_Click({
  Murder-Them BitRate
})

$Button2.Add_Click({
  $ListView1.Clear() | Out-Null
  $ListView1.Columns.Add('Folder') | Out-Null
  $ListView1.Columns.Add('Files') | Out-Null

  Get-Folders $TextBox1.Text
})

$Button1.Add_Click({
  $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
  $objForm.Description = "Select folder"
  $objForm.SelectedPath = [System.Environment+SpecialFolder]'MyComputer'
  $objForm.ShowNewFolderButton = $false
  $result = $objForm.ShowDialog()
  if ($result -eq "OK") {
    $TextBox1.Text = $objForm.SelectedPath
  } else {
    $TextBox1.Text = ""
  }
})

#endregion events }
#endregion GUI }

#Write your logic code here
Add-Type -AssemblyName Microsoft.VisualBasic

Function Murder-Them {
  param (
    [string]$sort
  )
  Write-Host '---'
  for ($i = 0; $i -lt $ListView1.Items.Count; $i++ ) {
    $files = (Get-MetaData -path $ListView1.Items[$i].Text .mp3)
    $temp = @()
    for ($j = 0; $j -lt $files.Count; $j++) {
      $temp += ,@( $files[$j].'Path', (((Get-Item $files[$j].'Path').Length).ToString()).PadLeft(20, '0') , ($files[$j].'Bit rate' -replace '.{4}$').PadLeft(4, '0') )
    }

    if ($sort -eq 'Size') {
      $temp = $temp | Sort-Object {$_[1]}
    } else {
      $temp = $temp | Sort-Object {$_[2]}
    }

    for ($k = 0; $k -lt $temp.Count - 1; $k++) {
      [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile("$($temp[$k][0])",'OnlyErrorDialogs','SendToRecycleBin')
#      Remove-Item -Path "$($temp[$k][0])"
    }
  }
}

Function Get-MetaData {
  PARAM (
    [PARAMETER(Mandatory=$true)]
    [string]$path = "",
    [PARAMETER( Mandatory=$true, HelpMessage="extension eg. .mp3, .txt or .mov")]
    [string]$type = "", # 'mp3'
    [switch]$recurse
  )
  if ($recurse) {
    $LPath = Get-ChildItem -Path $path -Directory -Recurse
  } else {
    $LPath = $path
  }

  $DirectoryCount = 1
  $RetrievedMetadata = $true
  $OutputList = New-Object 'System.Collections.generic.List[psobject]'

  Foreach ($pa in $LPath) {
    $shell = New-Object -ComObject shell.application

    if ($recurse) {
      $objshell = $shell.NameSpace($pa.FullName)
    } else {
      $objshell = $shell.NameSpace($pa)
    }
    #Build data list
    $count = 0

    #Filter on filetype
    Measure-Command -Expression { $filter = $objshell.items() | where {$_.path -match $type} } | Out-File -FilePath K:\test.txt
    foreach ($file in $filter) {
      if ($RetrievedMetadata) {
        # Build metanumbers
        Write-Verbose "Building MetaIndex for filetype $type"
        $Metanumbers = New-Object -TypeName 'System.Collections.Generic.List[int]'
        for ($a = 0; $a -le 400; $a++) {
          if ($objshell.GetDetailsOf($file, $a)) {
            $Metanumbers.Add([int]$a)
          }    
        }
        $RetrievedMetadata = $false
        Write-Verbose "$($Metanumbers.Count) entries in MetaIndex for filetype $type"    
      }

      $count++
      $CurrentDirectory = Get-ChildItem -Path $file.path
      try {
        Write-Progress -Activity " Getting metadata from $DirectoryCount/$($LPath.count) $($CurrentDirectory[0].DirectoryName)" -Status "Working on $count/$($filter.count) - $($file.Name)" -PercentComplete (($count / $filter.count) * 100) -ErrorAction stop
      } catch {}

      #Build Hashtable for each file
      $Hash = @{}
      foreach ($nr in $Metanumbers) {
        $PropertyName = $($objshell.GetDetailsOf($objshell.Items, $nr))
        $PropertyValue = $($objshell.GetDetailsOf($File, $nr))
        $Hash[$PropertyName] = $PropertyValue
      }
            
      $Hash.Remove("")
      $FileMetaData = New-Object -TypeName PSobject -Property $hash
      $OutputList.Add($FileMetaData)
      $hash = $null
    }
    $DirectoryCount++        
  }
  Write-Verbose "MetaData for $($OutputList.count) files found"
  return $OutputList
}

Function Get-Folders {
  param (
    [string]$path
  )
  $a = Get-ChildItem $path -Recurse -Directory
  for ($i = 0; $i -lt $a.Count; $i++) {
    $b = (Get-ChildItem $a[$i].FullName -File | Measure-Object).Count
    if ($b -gt 1) {
      $ListView_Item = New-Object System.Windows.Forms.ListViewItem($a[$i].FullName)
      $ListView_Item.SubItems.Add($b)
      $ListView1.Items.AddRange(($ListView_Item))
    }
  }
  $ListView1.AutoResizeColumn(1, 1)
  $ListView1.AutoResizeColumn(0, 2)
}

[void]$Form.ShowDialog()

1.png2.png

3.png4.png
The one marked VBR bit rate is actually 224kHz that's why it's remained.

Still no error checking :P

No progress indicator either  :-[

PS. Forgot to mention it deletes to the Recycle Bin.
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: March 06, 2019, 06:07 AM by 4wd »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: Does any folder in tree have more than one file in it?
« Reply #10 on: March 01, 2019, 05:20 AM »
Wow... I write all my forms freehand- I never realized that you could do the same in PowerShell.  I might be writing more scripting than ever now.

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: Does any folder in tree have more than one file in it?
« Reply #11 on: March 01, 2019, 05:48 AM »
@c.gingerich  Sill the same problem.  See snap of Garth Brooks above.

As an experiment I created a large folder hierarchy/tree structure with NO TWO FILES in any single folder.  mtof.exe flags folders even in this case, if more than one file exists in the mini-sub-hierarchy.

The objective is:  ONLY CALL 911 IF YOU SPOT TWO (OR MORE) BIRDS SITTING **TOGETHER** ON THE SAME SINGLE BRANCH.

Maybe it can't be done.

mtof.exe, though, is still a pretty neat little gem that I do appreciate.

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Does any folder in tree have more than one file in it?
« Reply #12 on: March 01, 2019, 06:16 AM »
Wow... I write all my forms freehand- I never realized that you could do the same in PowerShell.

Wouldn't really call it "writing freehand" since I just move objects around the POSHGUI editor and then copy out the code - just do a little tweaking and add form options not available in the POSHGUI editor, (eg. the ListView view mode), afterwards.

Could probably have added a column sort routine but once it seemed to work I got lazy  :)
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: Does any folder in tree have more than one file in it?
« Reply #13 on: March 01, 2019, 12:52 PM »
I meant that the code that poshgui generates looks like just the freehand code.  you can move things around the xaml canvas also, and it generates the same sort of code.  I just find that I have more fine control doing it myself.

c.gingerich

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 748
    • Participated in NANY Challenge Six Times
    • Coding Snacks
Re: Does any folder in tree have more than one file in it?
« Reply #14 on: March 01, 2019, 01:58 PM »
I think I'll have concede this one. I may go back and see what I can do mid next week, but I don't have any more time to spend on this week. Sorry  :(

highend01

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 188
Re: Does any folder in tree have more than one file in it?
« Reply #15 on: March 01, 2019, 05:09 PM »
Try this...

No threading for the scan, Cancel button is therefore useless atm...

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: Does any folder in tree have more than one file in it?
« Reply #16 on: March 02, 2019, 01:07 AM »
@c.gingerich.  Hope to see you back, Chris.  Either on this one, or the next.  Thanks for your skilled efforts!

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,175
    • Coding Snacks
    • Participated in NANY Challenge Three Times
    • Plugin Coding Medal
Re: Does any folder in tree have more than one file in it?
« Reply #17 on: March 02, 2019, 11:04 AM »
Not a script but the file search tool Everything supports the syntax to find all folders that contain a specific number of files
Code: (text)
childfilecount:<count>
See https://www.voidtools.com/support/everything/searching/
You can combine that with restricting the search to some base folder and negate the syntax (to show only folders that contain not 1 file) and only list the matching folders in the results view.
Code: (text)
"C:\base folder\" !childfilecount:1 folder:

chashnniel

  • Member
  • Joined in 2018
  • **
  • Posts: 127
Re: Does any folder in tree have more than one file in it?
« Reply #18 on: March 02, 2019, 01:08 PM »
This is what TC(with its plugin dirsizecalc) do.

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Does any folder in tree have more than one file in it?
« Reply #19 on: March 02, 2019, 11:23 PM »
Not a script but the file search tool Everything supports the syntax to find all folders that contain a specific number of files
Code: (text)
childfilecount:<count>
See https://www.voidtools.com/support/everything/searching/
You can combine that with restricting the search to some base folder and negate the syntax (to show only folders that contain not 1 file) and only list the matching folders in the results view.
Code: (text)
"C:\base folder\" !childfilecount:1 folder:

Now that is an excellent idea  :Thmbsup:

Wonder if you can 'and' in a not 0 file count also?
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: March 03, 2019, 12:19 AM by 4wd »

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: Does any folder in tree have more than one file in it?
« Reply #20 on: March 03, 2019, 05:50 AM »
@Nod5

How about that!  Everything does indeed seem to do the trick.

"M:\Audio 2\" childfilecount:>1 folder:

Sooo many additional options as well.  Never knew.

And, because of drawing upon the index, fast as lightning.

Tentatively may be a winner.



nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: Does any folder in tree have more than one file in it?
« Reply #21 on: March 03, 2019, 05:56 AM »
@4wd

Well, your PowerShell script nailed it.  Wonderful work.

Sorting, as you mentioned above, would not be so important as having the ability to export the list.  (However, nirsoft's SysExporter can indirectly grab the list, I noticed.)

Thanks, 4wd, for your continuing assistance.


nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: Does any folder in tree have more than one file in it?
« Reply #22 on: March 03, 2019, 06:08 AM »
@highend01

You betcha, highend01!  You da man, and, yes, "More than one file.exe" is gold, for the purpose which it was created -- deleting all but the largest file in each folder, over a directory structure.

Now..., if you want to ENHANCE "More than one file.exe".....???

Before cutting out all that deadwood, really best to at least take a sampling of the files being zapped.  Open a few of those folders and see what's what.

That's the reason for the present request.

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: Does any folder in tree have more than one file in it?
« Reply #23 on: March 03, 2019, 06:19 AM »
A quick note on what the application is with the present request:

Music Collection Maintenance.

I use a program called "Stream Writer" to capture streaming music.  And Media Monkey to maintain the organizing and database of the music.

Because of duplicates in pieces, I'd estimate the best stuff is only 20% of all that's been collected.  No sense in keeping the lower bitrate files, for instance.  And, often there's a "(2)" suffixed to same song, same bitrate.  (See "Wende" snap in original post at top.)

Seems we're not yet there, as far as optimal music collections and maintenance is concerned.

« Last Edit: March 03, 2019, 06:26 AM by nkormanik »

highend01

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 188
Re: Does any folder in tree have more than one file in it?
« Reply #24 on: March 03, 2019, 01:13 PM »
This version can "prune" (delete only the files with the smallest size and keep the one with the largest size) for each found folder...

Apart from that, the window is now resizable, and System Volume Information and $RECYCLE.BIN folders are now left out by default...

Use with care, you aren't asked if you're sure when you click the prune button^^
When the pruning is done, a log with all deleted files is copied to clipboard automatically...
« Last Edit: March 03, 2019, 02:15 PM by highend01 »

Original address: https://www.donationcoder.com/forum/index.php?topic=47222.0