Get File count and found matching file name VB script

Posted by rajivkumarnandvani on January 7, 2010

Dim fs ‘ variable declared outside a procedure (this is a global variable)
‘ this is hold a reference to the file system object
”create an instance
On Error Resume Next
Set fs = CreateObject(“scripting.filesystemobject”)
Set MyFile = fs.CreateTextFile(“c:\Filepath.txt”,True,True)

‘ count files in windows directory
folderpathvalue = inputbox (“Plese Enter Folder path” ,”Folder”,”C:\WINDOWS\”)
FileNamevalue = inputbox (“Plese Enter File name/extension of file” ,”FileName”,”.txt”)
Filecountvalue = CountFiles (folderpathvalue , FileNamevalue )
Set WshShell = CreateObject(“WScript.Shell”)
Set MyFile = nothing
Set fs = nothing
rem it will return the total file count value not the matching file count value
msgbox “Total File count is ” & Filecountvalue
rem it will show the how many file found with matching criteria“c:\Filepath.txt”)
Set WshShell =nothing

‘ takes a string argument containing the name of the directory

‘ returns an integer contiang the nubmer of files in that direcrectory

‘ and all sub directories

Function CountFiles (ByVal StrFolder ,byval FileNamevalue)
Dim ParentFld
Dim SubFld
Dim IntCount
‘ note the use of the fs global variable
Set ParentFld = fs.GetFolder (StrFolder)
‘ count the number of files in the current directory
IntCount = ParentFld.Files.Count
For Each oFile In ParentFld.files
On Error Resume Next
If InStr(1, lcase(oFile.path),FileNamevalue) > 0 then
MyFile.writeline oFile.path
end If
For Each SubFld In ParentFld.SubFolders
‘ count all files in each subfolder – recursion point
IntCount = IntCount + CountFiles(SubFld.Path ,FileNamevalue)
‘ return counted files
CountFiles = IntCount
End Function


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: