Jump to content

Script to output the number of files in a folder?


Recommended Posts

Posted

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

Posted (edited)

In Powershell :

 

Get-ChildItem

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

Edited by pcstru
Posted

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?

Posted
In Powershell :

 

Get-ChildItem

-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

Posted (edited)

VBScript :

 


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([b]"c:\Script\ScriptOutput.txt"[/b], True)

' Define folder we want to list files from
strPath = [b]"c:\Script\"[/b]

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/vbscript_recursive_enum_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)

Edited by mac_shinobi
Posted
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

| Where-Object {$_.Mode -eq "d----" }

Foreach ($d in $ds) {

Get-ChildItem $d.PSPath | Measure-Object -property Length -sum | select -property Count

}

Posted
Ok. Summat like :

 

$ds = Get-ChildItem

| 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?

Posted
You need to replace 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.
  • Thanks 1
Posted
VBScript :

 


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([b]"c:\Script\ScriptOutput.txt"[/b], True)

' Define folder we want to list files from
strPath = [b]"c:\Script\"[/b]

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

Posted
You need to replace 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

Posted

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 :)

  • Thanks 1
Posted
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)

Posted
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 :)

  • Thanks 1
Posted (edited)
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?

Edited by pcstru
Posted (edited)

@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

Edited by mac_shinobi
Posted

@mac_shinobi depends how snazzy you want it.

 

If you want the easiest and basic way, just add a count to the loop for showing subfolders.

 

Set FSO = CreateObject("Scripting.FileSystemObject")
ListMyStuff FSO.GetFolder("C:\test"), 2 

Sub ListMyStuff(Folder, HowManySubFolders)
   If HowManySubFolders > 0 then
       For Each Subfolder in Folder.SubFolders
           DoYourFileOutputStuff!
               HowManySubFolders  = HowManySubFolders -1
           ListMyStuff Subfolder, HowManySubFolders 
       Next
   End if
End Sub

 

So as it's doing the subfolders it'll count up, and then break out and do a recursive search of the next folders etc. (2 = "main level" + 1 sub etc)

 

If you want the snazzier way you can always do stuff like breaking filepaths down and counting \'s etc, or checking child/parent links etc.

 

Steve

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...