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
WshShell.run(“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
Err.clear
Next
For Each SubFld In ParentFld.SubFolders
‘ count all files in each subfolder – recursion point
IntCount = IntCount + CountFiles(SubFld.Path ,FileNamevalue)
Next
‘ return counted files
CountFiles = IntCount
End Function
Err.clear