+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
Scripts Thread, Script to output the number of files in a folder? in Coding and Web Development; I have a folder called X and within it a number of sub-folders A, B, C, D etc. Does anyone ...
  1. #1
    kennysarmy's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    1,254
    Thank Post
    75
    Thanked 45 Times in 31 Posts
    Rep Power
    30

    Script to output the number of files in a folder?

    I have a folder called X and within it a number of sub-folders A, B, C, D etc.

    Does anyone have a script that can determine then output to a file the number of files in each sub-folder?

    Cheers

  2. #2


    Join Date
    May 2009
    Posts
    2,492
    Thank Post
    199
    Thanked 632 Times in 483 Posts
    Rep Power
    228
    In Powershell :

    Get-ChildItem <dir> -r | Measure-Object -property Length -sum | select -property Count

    (sorry that's not exactly what is being asked for, but it should be useful as the basis of it).
    Last edited by pcstru; 15th October 2013 at 10:43 AM.

  3. #3
    kennysarmy's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    1,254
    Thank Post
    75
    Thanked 45 Times in 31 Posts
    Rep Power
    30
    I think a power shell script could do it using this command:

    (Get-ChildItem "folder" -filter "*.msg").Count

    but I don't know how to pass my list of folders to the command

    I could create a text file containing them...but then what?

  4. #4
    kennysarmy's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    1,254
    Thank Post
    75
    Thanked 45 Times in 31 Posts
    Rep Power
    30
    Quote Originally Posted by pcstru View Post
    In Powershell :

    Get-ChildItem <dir> -r | Measure-Object -property Length -sum | select -property Count

    (sorry that's not exactly what is being asked for, but it should be useful as the basis of it).
    This gives ALL the occurances of files in a folder - even in sub-folders
    I just want what is in the top levels...

    But thanks

  5. #5

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,228
    Thank Post
    2,773
    Thanked 937 Times in 877 Posts
    Rep Power
    343
    VBScript :

    Code:
    Dim fso, folder, files, OutputFile
    Dim strPath, filecnt
    
    
    ' Create a FileSystemObject  
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' Create text file to output test data
    Set OutputFile = fso.CreateTextFile("c:\Script\ScriptOutput.txt", True)
    
    ' Define folder we want to list files from
    strPath = "c:\Script\"
    
    set objFSO = createobject("Scripting.FileSystemObject")
    
    GetFolders strPath
    
    sub GetFolders(byval strDirectory)
        set objFolder = objFSO.GetFolder(strDirectory)    
        for each objFolder in objFolder.SubFolders
            Set folder = fso.GetFolder(objFolder.Path)
            Set files = folder.Files
            ' Loop through each file  
            filecnt = 0
            For each item In files
                filecnt = filecnt + 1
                ' Output file properties to a text file
                
            Next
            OutputFile.WriteLine("This Folder --> '" & folder & "' has " & filecnt & " Files")
            OutputFile.WriteLine
            GetFolders objFolder.Path
        next
    
    end sub
        wscript.echo "Script has finished running"
    ' Close text file
    OutputFile.Close
    Merged these 2 vbscripts together :

    List files from a directory

    http://www.mattsbits.co.uk/item-85.html

    Recursively enumerate sub folders in a directory :

    http://www.wisesoft.co.uk/scripts/vb...m_folders.aspx

    If this does not work as you want or require then @Steve21 or possibly @Arthur may be able to assist

    Just need to adjust strPath near the top of the script to point to the relevant main directory so that line shows something like :

    strPath ="X:\main_directory\"

    Where X:\ Is the drive letter and the rest is any directory name / folder name where all the sub folders reside

    You may also want to edit the path of where the outputted text file is created / stored ( as per the below line which you only need to adjust the path between the quotes )

    Set OutputFile = fso.CreateTextFile("c:\Script\ScriptOutput.txt", True)
    Last edited by mac_shinobi; 15th October 2013 at 11:40 AM.

  6. #6


    Join Date
    May 2009
    Posts
    2,492
    Thank Post
    199
    Thanked 632 Times in 483 Posts
    Rep Power
    228
    Quote Originally Posted by kennysarmy View Post
    This gives ALL the occurances of files in a folder - even in sub-folders
    I just want what is in the top levels...
    Ok. Summat like :

    $ds = Get-ChildItem <dir> | Where-Object {$_.Mode -eq "d----" }
    Foreach ($d in $ds) {
    Get-ChildItem $d.PSPath | Measure-Object -property Length -sum | select -property Count
    }

  7. #7
    kennysarmy's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    1,254
    Thank Post
    75
    Thanked 45 Times in 31 Posts
    Rep Power
    30
    Quote Originally Posted by pcstru View Post
    Ok. Summat like :

    $ds = Get-ChildItem <dir> | Where-Object {$_.Mode -eq "d----" }
    Foreach ($d in $ds) {
    Get-ChildItem $d.PSPath | Measure-Object -property Length -sum | select -property Count
    }

    Will try this - do I just copy and paste it in to the power shell window?

  8. #8


    Join Date
    May 2009
    Posts
    2,492
    Thank Post
    199
    Thanked 632 Times in 483 Posts
    Rep Power
    228
    You need to replace <dir> with the path you want amd save as a ps1 file. I run in the powershell ISE, in which case just paste it into the untitled tab.

  9. #9
    kennysarmy's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    1,254
    Thank Post
    75
    Thanked 45 Times in 31 Posts
    Rep Power
    30
    Quote Originally Posted by mac_shinobi View Post
    VBScript :

    Code:
    Dim fso, folder, files, OutputFile
    Dim strPath, filecnt
    
    
    ' Create a FileSystemObject  
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' Create text file to output test data
    Set OutputFile = fso.CreateTextFile("c:\Script\ScriptOutput.txt", True)
    
    ' Define folder we want to list files from
    strPath = "c:\Script\"
    
    set objFSO = createobject("Scripting.FileSystemObject")
    
    GetFolders strPath
    
    sub GetFolders(byval strDirectory)
        set objFolder = objFSO.GetFolder(strDirectory)    
        for each objFolder in objFolder.SubFolders
            Set folder = fso.GetFolder(objFolder.Path)
            Set files = folder.Files
            ' Loop through each file  
            filecnt = 0
            For each item In files
                filecnt = filecnt + 1
                ' Output file properties to a text file
                
            Next
            OutputFile.WriteLine("This Folder --> '" & folder & "' has " & filecnt & " Files")
            OutputFile.WriteLine
            GetFolders objFolder.Path
        next
    
    end sub
        wscript.echo "Script has finished running"
    ' Close text file
    OutputFile.Close
    Merged these 2 vbscripts together :

    List files from a directory

    MattsBits - List All Files In A Folder Using VBScript

    Recursively enumerate sub folders in a directory :

    Enum Folders (Recursive) (VBScript)

    If this does not work as you want or require then @Steve21 or possibly @Arthur may be able to assist

    Just need to adjust strPath near the top of the script to point to the relevant main directory so that line shows something like :

    strPath ="X:\main_directory\"

    Where X:\ Is the drive letter and the rest is any directory name / folder name where all the sub folders reside

    You may also want to edit the path of where the outputted text file is created / stored ( as per the below line which you only need to adjust the path between the quotes )

    Set OutputFile = fso.CreateTextFile("c:\Script\ScriptOutput.txt", True)
    This is very close!

    It's creating the output file, but it's doing every sub directory, I only need the top level folders.
    So I have put the top level as P:\ILIBRA\MDaemon\Users\STAFF\

    and I only want the number of files in
    P:\ILIBRA\MDaemon\Users\STAFF\ABB
    P:\ILIBRA\MDaemon\Users\STAFF\AGF
    etc etc

  10. #10
    kennysarmy's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    1,254
    Thank Post
    75
    Thanked 45 Times in 31 Posts
    Rep Power
    30
    Quote Originally Posted by pcstru View Post
    You need to replace <dir> with the path you want amd save as a ps1 file. I run in the powershell ISE, in which case just paste it into the untitled tab.
    Got it - it's returning the numbers but can it output the folder name and the number to a text file?

    At the moment it's just returning numbers...



    Ta

  11. #11
    kennysarmy's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    1,254
    Thank Post
    75
    Thanked 45 Times in 31 Posts
    Rep Power
    30
    I've got a power shell solution:

    Might be useful to others:

    $dirs = "dir.txt"

    foreach ($dir in [System.IO.File]::ReadLines($dirs)) {
    "$dir, $((Get-ChildItem $dir -filter "*.msg" -ea 0).Count)"
    }

    Outputs the dir name and number of msg files in folders listed in the dir.txt file

  12. Thanks to kennysarmy from:

    mac_shinobi (15th October 2013)

  13. #12


    Join Date
    May 2009
    Posts
    2,492
    Thank Post
    199
    Thanked 632 Times in 483 Posts
    Rep Power
    228
    Quote Originally Posted by kennysarmy View Post
    At the moment it's just returning numbers...
    Foreach ($d in Get-ChildItem c:\Python27 | Where-Object {$_.Mode -eq "d----" } ) {
    $d.FullName, $(Get-ChildItem $d.PSPath | Measure-Object -property Length -sum | select -property Count)
    }

    (I'll probably get marked as could do better)

  14. #13
    kennysarmy's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    1,254
    Thank Post
    75
    Thanked 45 Times in 31 Posts
    Rep Power
    30
    Quote Originally Posted by pcstru View Post
    Foreach ($d in Get-ChildItem c:\Python27 | Where-Object {$_.Mode -eq "d----" } ) {
    $d.FullName, $(Get-ChildItem $d.PSPath | Measure-Object -property Length -sum | select -property Count)
    }

    (I'll probably get marked as could do better)
    :P

    I still value your help

  15. Thanks to kennysarmy from:

    mac_shinobi (15th October 2013)

  16. #14


    Join Date
    May 2009
    Posts
    2,492
    Thank Post
    199
    Thanked 632 Times in 483 Posts
    Rep Power
    228
    Quote Originally Posted by kennysarmy View Post
    I still value your help
    Heh, no problem. I used to hate powershell, but am gradually coming to quite like it. It still frustrates me when something like that which should be a one liner, throws up the unexpected wobbles - which translates to "so you thought you knew about that, 'eh?".

    An Edugeek powershell repository would be a very useful thing to have. @Dos_Box?
    Last edited by pcstru; 15th October 2013 at 01:05 PM.

  17. #15

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,228
    Thank Post
    2,773
    Thanked 937 Times in 877 Posts
    Rep Power
    343
    @Dos_Box

    Also not 100% sure on how to only search or enumerate only the top level folders in vbs - would be good if I had more time with powershell

    Can't believe I was so close but still so far from making it work as requested, doh !

    If you don't mind @kennysarmy - if @Steve21 has a chance - it would be good to know how to do this in vbscript ( I know , I know, it's an older scripting language etc ) but it is bugging me not knowing how to only search or enumerate the top level of sub directories
    Last edited by mac_shinobi; 15th October 2013 at 01:43 PM.

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 51
    Last Post: 4th July 2012, 08:51 AM
  2. Script to find serial numbers of all computers on network
    By bandgeekmafia78 in forum Scripts
    Replies: 3
    Last Post: 23rd May 2011, 05:11 PM
  3. Replies: 0
    Last Post: 26th April 2011, 10:36 PM
  4. Limit the number of characters in text box
    By MattCowen in forum Office Software
    Replies: 1
    Last Post: 21st September 2009, 04:57 PM
  5. Replies: 7
    Last Post: 17th May 2007, 11:42 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •