• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 818
  • Last Modified:

VBscript needed to count files in a folder

Hi,
I need a Vbscript that would look into a directory that contains folders in this format:
dd-mm-yyyy and count the number of files in the folder that is dated today. i.e.
if today is 11-22-2010 then it should count the files in that folder.It would be nice if I could use this code in excel.
Thanks
S

0
Sean
Asked:
Sean
  • 11
  • 5
  • 4
  • +2
2 Solutions
 
MotifIndiaCommented:
dim oFS, oFolder
set oFS = WScript.CreateObject("Scripting.FileSystemObject")
set oFolder = oFS.GetFolder("YourPathName")

ShowFolderDetails oFolder

sub ShowFolderDetails(oF)
dim F
    wscript.echo oF.Name & ":Size=" & oF.Size
    wscript.echo oF.Name & ":#Files=" & oF.Files.Count
    wscript.echo oF.Name & ":#Folders=" & oF.Subfolders.count
    wscript.echo oF.Name & ":Size=" & oF.Size
    for each F in oF.Subfolders
        ShowFolderDetails(F)
    next
end sub
_______________________________________________________

Open a copy of NotePad and copy and paste my above code into it.

Change "YourPathName" to "Z:\". Save this as GetZFolders.vbs. .vbs is one of the two common file extensions for VBScript files. The other is .wsf.

To run the script using cscript type:

cscript GetZFolders.vbs

at a command line prompt. You should see the output go shooting up the screen.

To get the output from this into a text file type:

cscript GetZFolders.vbs > ZFolderSizes.txt

This will redirect the screen output from the wscript.echo commands to the text file ZFolderSizes.txt. If you want to modify the format of the information just change the wscript.echo commands to output what you want.

Try it out first and then post some example output and tell me what you want changed.

Thanks,

Daxesh Patel
0
 
Justin MathewsCommented:
Try this. It should work as an Excel macro too. Just copy it into a Sub. Replace your folder path.

Set fso = CreateObject("Scripting.FileSystemObject")
dir = "C:\Your\Folder\Path"

dt = replace(Date, "/", "-")

Set sf = fso.GetFolder(dir).SubFolders

For Each f in sf
  If f.name = dt Then
	Set fc = f.Files
	MsgBox fc.Count
'	For Each f1 in fc
'		MsgBox f1.name
'	Next
   End If
Next

Open in new window

0
 
gbanikCommented:
Here is an Excel file... start it with macro enabled. It does the needful using FileSystemObject
GetFileNames.xlsm
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
raiERBCommented:
Without looking into the subfolders here ist the script:
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim strFolder : strFolder = "C:\Temp"
Dim objFolder : Set objFolder = objFSO.GetFolder(strFolder)
Dim colFiles  : Set colFiles = objFolder.Files

WScript.Echo "Files in the folder: " & colFiles.Count

Open in new window

0
 
raiERBCommented:
Here is a recursive one that includes the subfolders...
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim strFolder : strFolder = "C:\Temp"
Dim objFolder : Set objFolder = objFSO.GetFolder(strFolder)
Dim colFiles  : Set colFiles = objFolder.Files

WScript.Echo "Files in the directory: " & colFiles.Count
WScript.Echo "Including subfolders: " & CountFiles(strFolder)

Function CountFiles(strRootFolder)
  Dim intPFileCounter
  Dim objPFSO : Set objPFSO = CreateObject("Scripting.FileSystemObject")
  Dim objPFolder : Set objPFolder = objPFSO.GetFolder(strRootFolder)
  Dim colPFiles  : Set colPFiles = objPFolder.Files
  Dim colPSubFolders : Set colPSubFolders = objPFolder.SubFolders
  Dim objPSubFolder
  
  intPFileCounter = intPFileCounter + colPFiles.Count
  
  For Each objPSubFolder In colPSubFolders
    intPFileCounter = intPFileCounter + CountFiles(objPSubFolder)
  Next
  
  CountFiles = intPFileCounter
  
End Function

Open in new window

0
 
SeanAuthor Commented:
Hi,
I am sorry folks. I think I must not have been clear on what I needed.
here is what the directory that contains the folder would look like today:
c:\rec
11-20-2010
11-21-2010
11-22-2010
11-23-2010

if the script/macro runs today and today is 11/23/2010 it should count the number of files in 11-23-2010

tomorrow the directory would look like this:
11-20-2010
11-21-2010
11-22-2010
11-23-2010
11-24-2010

when I run the script tomorrow it will count the number of files in the folder 11-24-2010.
every day a new folder is created in directory c:\rec and every night the script/macro runs and gives me the number of files in the folder for that date.

Thanks
S

0
 
Justin MathewsCommented:
That is exactly what my solution above does.
0
 
SeanAuthor Commented:
Motifindia:
Thank you for your response. here is the results for your script:
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

rec:Size=4315814
rec:#Files=0
rec:#Folders=2
rec:Size=4315814
11-22-2010:Size=3934520
11-22-2010:#Files=10
11-22-2010:#Folders=0
11-22-2010:Size=3934520
11-23-2010:Size=381294
11-23-2010:#Files=4
11-23-2010:#Folders=0
11-23-2010:Size=381294

All I would need is the number of files in folder 11-23-2010 which is correct at 4.
and like I mentioned when the script is executed tomorrow it should only give me the results for the folder 11-24-2010 since tomorrow will be 11/24/2010.

S
0
 
SeanAuthor Commented:
jmatix,
your script is correct. However I would need to run this in excel so I would need the out put to be similar to this:

Total files    4

Thanks
S
0
 
SeanAuthor Commented:
raiERB,

You script is also correct in giving me total number of files in the directory c:\rec.
Files in the directory: 0
Including subfolders: 14
but all I need is total number of files in a folder that is dated today 11-23-2010 if today's date is 11/23/2010
tomorrow it should give me the total files in folder c:\rec\11-24-2010.
Thanks
S
0
 
SeanAuthor Commented:
railERB,
This script returned 0 as the number of files which is incorrect.

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim strFolder : strFolder = "C:\rec"
Dim objFolder : Set objFolder = objFSO.GetFolder(strFolder)
Dim colFiles  : Set colFiles = objFolder.Files

WScript.Echo "Files in the folder: " & colFiles.Count


Thanks
S
0
 
SeanAuthor Commented:
qbanik,

Love your sheet but it is not returning any info.
S
0
 
Justin MathewsCommented:
Set fso = CreateObject("Scripting.FileSystemObject")
dir = "C:\Your\Folder\Path"

dt = replace(Date, "/", "-")

Set sf = fso.GetFolder(dir).SubFolders

For Each f in sf
  If f.name = dt Then
      Set fc = f.Files
                 ActiveSheet.Cells(1, 1) = "Total files"
                 ActiveSheet.Cells(1, 2) = fc.Count
'      For Each f1 in fc
'            MsgBox f1.name
'      Next
   End If
Next
0
 
gbanikCommented:
Yes ... i got your requirement incorrect.
I was actually expecting u to like it when I was making it.
It finds out all files in a folder that matches the given date (can be set to today).
0
 
SeanAuthor Commented:
q,
As I said I love it and will use it else where,
How come it not returning any values.
I am doing this,
C:\rec   for the path
and 11/23/2010 for the date and I click on find. Nothing happens.


Thanks again
S
0
 
SeanAuthor Commented:
Imatix,

Please see attached.

Thanks
S

1.gif
0
 
gbanikCommented:
Click on browse .. select the directory
Add the date for search (search for files "created" on that date ... not the file name)
Click on "Find"
It enlists all files created on the selected date and in that particular folder.

ps. I just downloaded the file again and tested it.. it works. Actually it can be modified very easily to fit your need ... but alas too late ... there are so many who have already contributed so much!! ... But you are welcome to use it elsewhere :)
0
 
Justin MathewsCommented:
Oops! Dir is a function in Excel. Changing the variable to d:
Set fso = CreateObject("Scripting.FileSystemObject")
d = "C:\rec"

dt = replace(Date, "/", "-")

Set sf = fso.GetFolder(d).SubFolders

For Each f in sf
  If f.name = dt Then
      Set fc = f.Files
                 ActiveSheet.Cells(1, 1) = "Total files"
                 ActiveSheet.Cells(1, 2) = fc.Count
'      For Each f1 in fc
'            MsgBox f1.name
'      Next
   End If
Next

Open in new window

0
 
gbanikCommented:
Of course .. it also shows you the count of files ;)

Tell me if you succeeded in running it.
0
 
SeanAuthor Commented:
imatix,
worked.
Thanks a lot.
0
 
SeanAuthor Commented:
qbanik,

worked. I should have known network drives would take some time.
Thank you. wonderful tool.
Sean
0
 
gbanikCommented:
You are welcome :)
0
 
SeanAuthor Commented:
Thank you.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 11
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now