Solved

VBscript needed to count files in a folder

Posted on 2010-11-22
23
800 Views
Last Modified: 2012-08-13
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
Comment
Question by:Sean
  • 11
  • 5
  • 4
  • +2
23 Comments
 
LVL 1

Expert Comment

by:MotifIndia
ID: 34194133
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
 
LVL 16

Expert Comment

by:jmatix
ID: 34194524
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
 
LVL 13

Expert Comment

by:gbanik
ID: 34195269
Here is an Excel file... start it with macro enabled. It does the needful using FileSystemObject
GetFileNames.xlsm
0
 
LVL 3

Expert Comment

by:raiERB
ID: 34197150
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
 
LVL 3

Expert Comment

by:raiERB
ID: 34197189
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
 
LVL 1

Author Comment

by:Sean
ID: 34198392
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
 
LVL 16

Expert Comment

by:jmatix
ID: 34198417
That is exactly what my solution above does.
0
 
LVL 1

Author Comment

by:Sean
ID: 34200085
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
 
LVL 1

Author Comment

by:Sean
ID: 34200104
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
 
LVL 1

Author Comment

by:Sean
ID: 34200154
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
 
LVL 1

Author Comment

by:Sean
ID: 34200185
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:Sean
ID: 34200253
qbanik,

Love your sheet but it is not returning any info.
S
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34200287
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
 
LVL 13

Expert Comment

by:gbanik
ID: 34200317
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
 
LVL 1

Author Comment

by:Sean
ID: 34200387
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
 
LVL 1

Author Comment

by:Sean
ID: 34200436
Imatix,

Please see attached.

Thanks
S

1.gif
0
 
LVL 13

Accepted Solution

by:
gbanik earned 250 total points
ID: 34200448
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
 
LVL 16

Assisted Solution

by:jmatix
jmatix earned 250 total points
ID: 34200509
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
 
LVL 13

Expert Comment

by:gbanik
ID: 34200524
Of course .. it also shows you the count of files ;)

Tell me if you succeeded in running it.
0
 
LVL 1

Author Comment

by:Sean
ID: 34200715
imatix,
worked.
Thanks a lot.
0
 
LVL 1

Author Comment

by:Sean
ID: 34200718
qbanik,

worked. I should have known network drives would take some time.
Thank you. wonderful tool.
Sean
0
 
LVL 13

Expert Comment

by:gbanik
ID: 34200738
You are welcome :)
0
 
LVL 1

Author Closing Comment

by:Sean
ID: 34234818
Thank you.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now