Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: List folders and subfolders which contains file too  (Read 16108 times)

0 Members and 1 Guest are viewing this topic.

kovacsa

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Unknown
    List folders and subfolders which contains file too
    « on: January 15, 2019, 04:24:36 AM »
    I use following code to list all the folders and subfolders from a given folder: dir /s /b /o:n /a:d > folderlist.txt , but I need only those, which contains at least one file.

    Salmon Trout

    • Guest
    Re: List folders and subfolders which contains file too
    « Reply #1 on: January 15, 2019, 01:37:10 PM »
    So you mean, exclude all folders with a zero number of files?
    Is a folder with a subfolder, but no files, empty or not?



    kovacsa

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Unknown
      Re: List folders and subfolders which contains file too
      « Reply #2 on: January 16, 2019, 01:28:16 AM »
      Yes.
      If has a subfolder, but no files it is empty, but subfolder should be checked too.

      Mark.



        Adviser
      • Forum Regular
      • Thanked: 67
        • Yes
      • Certifications: List
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 10
      Re: List folders and subfolders which contains file too
      « Reply #3 on: January 16, 2019, 01:31:54 AM »
      sounds like a job for powershell.
      something like this may get you started; powershell -command "Get-ChildItem 'C:\mydocs' -Recurse -Directory | ? {[System.IO.Directory]::GetFileSystemEntries($_.FullName).Count -gt 0} | ? {$_.FullName}"
      it will not show folders with a file count of zero.

      kovacsa

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Unknown
        Re: List folders and subfolders which contains file too
        « Reply #4 on: January 18, 2019, 02:36:30 AM »
        Output seems correct! Which flag should I use please to hide folder details?

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: List folders and subfolders which contains file too
        « Reply #5 on: January 18, 2019, 08:44:57 AM »
        if you prefer to stick with your batch solution, try this:

        Code: [Select]
        @echo off
        for /f %%i in ('dir <YourFolderName> /ad /s /b') do (
           for /f %%j in ('dir %%i /w ^| find /i "file(s)"') do (
             if %%j GTR 0 echo %%i >> <YourFileName>
           )
        )

        If you prefer a Powershell solution, this may come in handy:

        Code: [Select]
        Get-ChildItem <YourFolderName> -Recurse -Directory |
          Where-Object { $_.GetFiles().Count -gt 0 } |
          Select-Object Fullname -ExpandProperty Fullname |
          Out-File -FilePath <YourFileName>
         

        Be sure to change <YourFolderName? and <YourFileName> appropriately.

        Good luck.  8)
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        kovacsa

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Unknown
          Re: List folders and subfolders which contains file too
          « Reply #6 on: January 21, 2019, 06:54:18 AM »
          Thanks for your help!