Tuesday, October 2, 2007

Count number of lines in a project with VBScript

'
' www.pullnews.com
' Save this code as file LineCount.vbs
'

If WScript.Arguments.Count= 0 Then
      MsgBox "Usage instruction:" & vbCrLf & _
      "Set folder as argument, or Drag source folder and drop on vbs file"
      WScript.Quit
End If
nCounter = 0
FolderName = WScript.Arguments(0)
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(FolderName) Then
    WScript.Echo "Invalid folder name"
Else
    CheckSubfolders fso.GetFolder(FolderName)
    WScript.Echo "Number of lines in all files is = " & nCounter
End If
' Subfolder recursion
Sub CheckSubfolders(thisFolder)
    Dim subFolder
    CountLinesInFolder thisFolder
    For Each subFolder In thisFolder.SubFolders
        CheckSubfolders subFolder
    Next
End Sub
Sub CountLinesInFolder(thisFolder)
    Const ForReading = 1
    Dim File, theFile
    For Each File In thisFolder.Files
        Set theFile = fso.OpenTextFile(File.Path, ForReading, False)
        Do While theFile.AtEndOfStream <> True
            theFile.ReadLine
            nCounter = nCounter + 1
        Loop
        theFile.Close
    Next
End Sub

No comments: