Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file count in VB 1

Status
Not open for further replies.

MattNewToVB

Technical User
Dec 23, 2009
3
AU
Hi, i am new to VBscript, i have a simple script to count files in set folders.

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder1 = "C:\f1"
strFolder2 = "C:\f2"
strFolder3 = "C:\f3"
Set objFolder1 = objFS.GetFolder(strFolder1)
Set objFolder2 = objFS.GetFolder(strFolder2)
Set objFolder3 = objFS.GetFolder(strFolder3)

WScript.Echo "1 ",objFolder1.Files.Count & vbCrLf & _
"2 " ,objFolder2.Files.Count -1 & vbCrLf & _
"3 " ,objFolder3.Files.Count & vbCrLf & vbCrLf & _
"Total Count " ,objFolder1.Files.Count + objFolder2.Files.Count + objFolder3.Files.Count -1



I would like the WSH popup to refresh it self after x amount of time, not close it down and reopen, is this possible ?

Thanks
 
Would something like this work for you?

Code:
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFolder1 = objFS.GetFolder("C:\1\")
Set objFolder2 = objFS.GetFolder("C:\2\")
Set objFolder3 = objFS.GetFolder("C:\3\")
set x = createobject("internetexplorer.application")
x.navigate2 "about:blank" : x.width = 350 : x.height = 150 : x.toolbar = false : x.menubar = false : x.statusbar = false

TestCondition = ""
Do
    x.visible = True
    x.document.title = "Folder Monitor"
    x.Document.Body.InnerHTML = "<font color=blue>"
    x.Document.Body.InnerHTML =  "Folder 1 Count = " & objFolder1.Files.Count  & "<br>" & _
                                 "Folder 2 Count = " & objFolder2.Files.Count & "<br>" & _
                                 "Folder 3 Count = " & objFolder3.Files.Count & "<br>" & "<br>" & _
                                 "Total Count = " & (objFolder1.Files.Count + objFolder2.Files.Count + objFolder3.Files.Count)
    wscript.sleep 5000
    x.visible = False
    wscript.sleep 5000
    x.Document.Body.InnerHTML = ""
Loop While TestCondition = ""

Swi
 
Hi Swi,

The code works well, is there a way to keep the HTML screen open with out it closing and reopening, to just do a refresh ?

Thanks

Matt
 
Sure. Actually it is just hiding itself and then making itself visible again. This will actually leave the window open and just refresh its contents.

Code:
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFolder1 = objFS.GetFolder("C:\1\")
Set objFolder2 = objFS.GetFolder("C:\2\")
Set objFolder3 = objFS.GetFolder("C:\3\")
set x = createobject("internetexplorer.application")
x.navigate2 "about:blank" : x.width = 350 : x.height = 150 : x.toolbar = false : x.menubar = false : x.statusbar = false : x.visible = True
TestCondition = ""
Do
    x.document.title = "Folder Monitor"
    x.Document.Body.InnerHTML = "<font color=blue>"
    x.Document.Body.InnerHTML =  "Folder 1 Count = " & objFolder1.Files.Count  & "<br>" & _
                                 "Folder 2 Count = " & objFolder2.Files.Count & "<br>" & _
                                 "Folder 3 Count = " & objFolder3.Files.Count & "<br>" & "<br>" & _
                                 "Total Count = " & (objFolder1.Files.Count + objFolder2.Files.Count + objFolder3.Files.Count)
    wscript.sleep 5000
    x.Document.Body.InnerHTML = ""
Loop While TestCondition = ""

Swi
 
Glad to hear. Have a great New Year!

Swi
 
You can also turn this the other way around and use a single process instead of automating an IE instance from a script:

Counts.hta
Code:
<html>
  <head>
    <hta:application
      applicationName="Folder Monitor"
      border=thin
      contextMenu=no
      maximizeButton=no
      minimizeButton=yes
      scroll=no
      singleInstance=yes>
    <title>Folder Monitor</title>
    <script language="VBScript">
      Option Explicit
      Dim FSO
      Dim StopRefresh

      Private Sub window_onload()
        window.resizeTo 240, 300
        Set FSO = CreateObject("Scripting.FileSystemObject")
        RefreshCounts
      End Sub

      Private Function CountFilesIn(ByVal FolderName)
        On Error Resume Next
        CountFilesIn = FSO.GetFolder(FolderName).Files.Count
        If Err Then CountFilesIn = 0
      End Function

      Private Sub RefreshCounts()
        Dim FileCount
        Dim Total

        FileCount = CountFilesIn("C:\1")
        lblCount1.innerText = CStr(FileCount)
        Total = FileCount
        FileCount = CountFilesIn("C:\2")
        lblCount2.innerText = CStr(FileCount)
        Total = Total + FileCount
        FileCount = CountFilesIn("C:\3")
        lblCount3.innerText = CStr(FileCount)
        Total = Total + FileCount
        lblTotal.innerText = CStr(Total)

        lblRefreshTime.innerText = CStr(Time())

        If Not StopRefresh Then
          window.setTimeout GetRef("RefreshCounts"), 5000
        End If
      End Sub

      Private Sub cmdStop_onclick()
        StopRefresh = True
        cmdStop.disabled = True
      End Sub
    </script>
    <style>
      body {background-color: lightsalmon; color: midnightblue;
            font: 12pt Tahoma}
    </style>
  </head>
  <body>
    <p>Counts at <span id="lblRefreshTime">00:00:00 AM</span></p>
    <p> <p>
    <p>Folder 1 Count = <span id="lblCount1">0</span></p>
    <p>Folder 2 Count = <span id="lblCount2">0</span></p>
    <p>Folder 3 Count = <span id="lblCount3">0</span></p>
    <p>Total Count = <span id="lblTotal">0</span></p>
    <p> </p>
    <p><button id="cmdStop">Stop Refreshing</button></p>
  </body>
</html>
 
Nice solution dilettante. Have a star.

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top