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!

Updating HTA Display

Status
Not open for further replies.

MasterRacker

New member
Oct 13, 1999
3,343
US
I have a script that scans for files whose FQN is over a specified length. It works fine except that I want some kind of progress indicator and I can't get the display to update. Everything I've found suggests all you need to do is set a timer that calls an update routine. In my ast the update does not fire when I call before starting the scan or when the timer fires, only at the end of the process. What am I missing here?

Code:
<!--
'===============================================================================
' NAME: ListLongFilenames.hta
' AUTHOR: Ano Nymous
' DATE: 02/25/2011
' DESCRIPTION: Find file with longest full name plus count files over specified limit.
'              Logs filenames over limit to a text file.
'===============================================================================
-->
<HEAD>
    <title>List Files</title>
    <HTA:APPLICATION 
         APPLICATIONNAME="List Files Over Limit"
         SCROLL="no"
         SINGLEINSTANCE="yes"
    >
</HEAD>

<!-- ************************************************************************************** -->
<SCRIPT LANGUAGE="VBScript">
dim FSO, outFQN, filecnt, longcnt, outFile, file, maxlen, slen


Sub CloseScript
   Self.Close()
End Sub

Sub Window_OnLoad
    window.resizeTo 800,300
    iTimer = window.setInterval("UpdateStatus", 500)
End Sub
 
Sub UpdateStatus
        fileCount.InnerHTML = CStr(filecnt)
        longCount.InnerHTML = CStr(longcnt)
        maxLength.InnerHTML = CStr(maxlen)
End Sub
 
Sub RunScript
    set FSO = CreateObject("Scripting.FileSystemObject")
    
    outFQN = outfiletxt.Value
    '----- If the output file does not exist, create it
    if not FSO.FileExists(outFQN) then
       FSO.CreateTextFile(outFQN)
    end if

    '----- Open file for append
    const ForReading = 1
    const ForWriting = 2
    const ForAppending = 8
    set outFile = FSO.OpenTextFile (outFQN, ForWriting, true)
    '========================= Logfile Initialization    - End

    longcnt = 0
    filecnt = 0
    maxlen = 0
    UpdateStatus
    
    ListFiles FSO.GetFolder(startfolder.Value)
    outFile.WriteLine ("Longest name found: " & CStr(maxlen))
    UpdateStatus
    
    '----- Cleanup
    set FSO = nothing
    outFile.Close
End Sub

Sub ListFiles(folder)
    For Each file In folder.Files
        filecnt = filecnt + 1
        slen = len(file.Path)
        if slen > maxlen then
            maxlen = slen
        end if
        if slen > CInt(cutofflen.value) then
            longcnt = longcnt + 1
            outFile.WriteLine ("""" & file.Path & """," & CStr(slen))
            outFile.WriteLine ("")
        end if
    Next
    For Each subfolder In folder.SubFolders
        ListFiles(subfolder)
    Next
End Sub
</SCRIPT>

<!-- ************************************************************************************** -->
<BODY>
    Starting Folder: <input type="text" name="startfolder" style="width:300px" /><BR />
    Output File: <input type="text" name="outfiletxt" style="width:300px" value="C:\test\filelist.txt" /><BR />
    Cutoff: <input type="text" name="cutofflen" style="width:50px" value="240" /><BR />
    
    <BR />
    <input id=runbutton class="button" type="button" value="Run" name="run_button" onClick="RunScript">
    &nbsp;&nbsp;&nbsp;
    <input id=runbutton class="button" type="button" value="Close" name="close_button" onClick="CloseScript">
    
    <BR /><BR />
    Files Scanned: <span ID="fileCount"></span><BR />
    Files Over Limit: <span ID="longCount"></span>&nbsp;&nbsp;Max Length Found: <span ID="maxLength"></span>
</BODY>

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
I think it's because updateStatus is called by Window_onLoad before the namespace elements (fileCount, longCount, maxLength) are defined in the HTA.

Instead of Window_onLoad (call it startScan), call the sub by clicking a button. That way, the namespaces will exist before they are called upon.

Code:
<input "type="button" onClick="startScan()">

Also, I'm sure you're aware, but, it looks like your updateStatus routine is in the improper space. Because listFiles is recursive, updateStatus will only run before and after the listFiles. Move the updateStatus line to within the listFiles files loop (not the folder loop).

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding
 
I'm not quite sure what you're getting at, however I tried moving the timer set up outside the OnLoad with no change. I then got the time out of the way completely and simply tried calling UpdateStatus after every file still with no updates until the scan finishes.

Even after a scan, if I try entering a different path and click Run again, it doesn't reset the display to 0 before calling ListFiles again.
Code:
<!-- ************************************************************************************** -->
<SCRIPT LANGUAGE="VBScript">
dim FSO, outFQN, filecnt, longcnt, outFile, file, maxlen, slen


Sub CloseScript
   Self.Close()
End Sub

Sub Window_OnLoad
    window.resizeTo 800,300
End Sub
 
Sub UpdateStatus
        fileCount.InnerHTML = CStr(filecnt)
        longCount.InnerHTML = CStr(longcnt)
        maxLength.InnerHTML = CStr(maxlen)
End Sub
 
Sub RunScript
[B][I]'    iTimer = window.setInterval("UpdateStatus", 500)[/I][/B]
    set FSO = CreateObject("Scripting.FileSystemObject")
    
    outFQN = outfiletxt.Value
    '----- If the output file does not exist, create it
    if not FSO.FileExists(outFQN) then
       FSO.CreateTextFile(outFQN)
    end if

    '----- Open file for append
    const ForReading = 1
    const ForWriting = 2
    const ForAppending = 8
    set outFile = FSO.OpenTextFile (outFQN, ForWriting, true)
    '========================= Logfile Initialization    - End

    longcnt = 0
    filecnt = 0
    maxlen = 0
    UpdateStatus
    
    ListFiles FSO.GetFolder(startfolder.Value)
    outFile.WriteLine ("Longest name found: " & CStr(maxlen))
    UpdateStatus
    
    '----- Cleanup
    set FSO = nothing
'    set iTimer = nothing
    outFile.Close
End Sub

Sub ListFiles(folder)
    For Each file In folder.Files
[B][I]        UpdateStatus[/I][/B]
        filecnt = filecnt + 1
        slen = len(file.Path)
        if slen > maxlen then
            maxlen = slen
        end if
        if slen > CInt(cutofflen.value) then
            longcnt = longcnt + 1
            outFile.WriteLine ("""" & file.Path & """," & CStr(slen))
            outFile.WriteLine ("")
        end if
    Next
    For Each subfolder In folder.SubFolders
        ListFiles(subfolder)
    Next
End Sub
</SCRIPT>

<!-- ************************************************************************************** -->
<BODY>
    Starting Folder: <input type="text" name="startfolder" style="width:300px" /><BR />
    Output File: <input type="text" name="outfiletxt" style="width:300px" value="C:\test\filelist.txt" /><BR />
    Cutoff: <input type="text" name="cutofflen" style="width:50px" value="240" /><BR />
    
    <BR />
    <input id=runbutton class="button" type="button" value="Run" name="run_button" onClick="RunScript">
    &nbsp;&nbsp;&nbsp;
    <input id=runbutton class="button" type="button" value="Close" name="close_button" onClick="CloseScript">
    
    <BR /><BR />
    Files Scanned: <span ID="fileCount"></span><BR />
    Files Over Limit: <span ID="longCount"></span>&nbsp;&nbsp;Max Length Found: <span ID="maxLength"></span>
</BODY>

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
I just ran this script as an HTA and it ran wonderfully from folder to folder. Make sure you uncomment iTimer under RunScript.

It seems as though when assigning cstr(var) to var.innerHTML, cstr(var) evaluates to "". I've had similar issues.


-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding
 
Taking that exact script and uncommenting the timer does not work for me. It does not update the display until the scan is done. Here's exactly what I ran:
Code:
<HEAD>
    <title>List Files</title>
    <HTA:APPLICATION 
         APPLICATIONNAME="List Files Over Limit"
         SCROLL="no"
         SINGLEINSTANCE="yes"
    >
</HEAD>

<!-- ************************************************************************************** -->
<SCRIPT LANGUAGE="VBScript">
dim FSO, outFQN, filecnt, longcnt, outFile, file, maxlen, slen


Sub CloseScript
   Self.Close()
End Sub

Sub Window_OnLoad
    window.resizeTo 800,300
End Sub
 
Sub UpdateStatus
        fileCount.InnerHTML = CStr(filecnt)
        longCount.InnerHTML = CStr(longcnt)
        maxLength.InnerHTML = CStr(maxlen)
End Sub
 
Sub RunScript
    iTimer = window.setInterval("UpdateStatus", 500)
    set FSO = CreateObject("Scripting.FileSystemObject")
    
    outFQN = outfiletxt.Value
    '----- If the output file does not exist, create it
    if not FSO.FileExists(outFQN) then
       FSO.CreateTextFile(outFQN)
    end if

    '----- Open file for append
    const ForReading = 1
    const ForWriting = 2
    const ForAppending = 8
    set outFile = FSO.OpenTextFile (outFQN, ForWriting, true)
    '========================= Logfile Initialization    - End

    longcnt = 0
    filecnt = 0
    maxlen = 0
    UpdateStatus
    
    ListFiles FSO.GetFolder(startfolder.Value)
    outFile.WriteLine ("Longest name found: " & CStr(maxlen))
    UpdateStatus
    
    '----- Cleanup
    set FSO = nothing
    set iTimer = nothing
    outFile.Close
End Sub

Sub ListFiles(folder)
    For Each file In folder.Files
        UpdateStatus
        filecnt = filecnt + 1
        slen = len(file.Path)
        if slen > maxlen then
            maxlen = slen
        end if
        if slen > CInt(cutofflen.value) then
            longcnt = longcnt + 1
            outFile.WriteLine ("""" & file.Path & """," & CStr(slen))
            outFile.WriteLine ("")
        end if
    Next
    For Each subfolder In folder.SubFolders
        ListFiles(subfolder)
    Next
End Sub
</SCRIPT>

<!-- ************************************************************************************** -->
<BODY>
    Starting Folder: <input type="text" name="startfolder" style="width:300px" /><BR />
    Output File: <input type="text" name="outfiletxt" style="width:300px" value="C:\test\filelist.txt" /><BR />
    Cutoff: <input type="text" name="cutofflen" style="width:50px" value="240" /><BR />
    
    <BR />
    <input id=runbutton class="button" type="button" value="Run" name="run_button" onClick="RunScript">
    &nbsp;&nbsp;&nbsp;
    <input id=runbutton class="button" type="button" value="Close" name="close_button" onClick="CloseScript">
    
    <BR /><BR />
    Files Scanned: <span ID="fileCount"></span><BR />
    Files Over Limit: <span ID="longCount"></span>&nbsp;&nbsp;Max Length Found: <span ID="maxLength"></span>
</BODY>

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Screwing around some more, I found a hack for the DoEvents function that does work if I call the update explicitly. I put a counter in the update and called it only from a timer and it appears like my timer does not fire. This version that updates explicitly every 200 files scanned works perfectly. so the problem appears to be the timer. It's annoying but I can probably live with this version.
Code:
<HEAD>
    <title>List Files</title>
    <HTA:APPLICATION 
         APPLICATIONNAME="List Files Over Limit"
         SCROLL="no"
         SINGLEINSTANCE="yes"
    >
</HEAD>

<!-- ************************************************************************************** -->
<SCRIPT LANGUAGE="VBScript">
dim FSO, outFQN, filecnt, longcnt, outFile, file, maxlen, slen, SH


Sub CloseScript
   Self.Close()
End Sub

Sub Window_OnLoad
    window.resizeTo 800,300
End Sub
 
Sub UpdateStatus
        fileCount.InnerHTML = CStr(filecnt)
        longCount.InnerHTML = CStr(longcnt)
        maxLength.InnerHTML = CStr(maxlen)
        DoEvents
End Sub
 
Sub DoEvents()
   SH.run "%comspec% /c exit", 0, True
End Sub

Sub RunScript
    iTimer = window.setInterval("UpdateStatus", 250)
    set SH = CreateObject("WScript.Shell")
    set FSO = CreateObject("Scripting.FileSystemObject")
    
    outFQN = outfiletxt.Value
    '----- If the output file does not exist, create it
    if not FSO.FileExists(outFQN) then
       FSO.CreateTextFile(outFQN)
    end if

    '----- Open file for append
    const ForReading = 1
    const ForWriting = 2
    const ForAppending = 8
    set outFile = FSO.OpenTextFile (outFQN, ForWriting, true)
    '========================= Logfile Initialization    - End

    longcnt = 0
    filecnt = 0
    maxlen = 0
    UpdateStatus
    
    ListFiles FSO.GetFolder(startfolder.Value)
    outFile.WriteLine ("Longest name found: " & CStr(maxlen))
    UpdateStatus
    
    '----- Cleanup
    set FSO = nothing
    window.clearInterval(iTimer)
    set SH = nothing
    outFile.Close
End Sub

Sub ListFiles(folder)
    For Each file In folder.Files
        if filecnt Mod 200 = 0 then
            UpdateStatus
        end if
        filecnt = filecnt + 1
        slen = len(file.Path)
        if slen > maxlen then
            maxlen = slen
        end if
        if slen > CInt(cutofflen.value) then
            longcnt = longcnt + 1
            outFile.WriteLine ("""" & file.Path & """," & CStr(slen))
            outFile.WriteLine ("")
        end if
    Next
    For Each subfolder In folder.SubFolders
        ListFiles(subfolder)
    Next
End Sub
</SCRIPT>

<!-- ************************************************************************************** -->
<BODY>
    Starting Folder: <input type="text" name="startfolder" style="width:300px" /><BR />
    Output File: <input type="text" name="outfiletxt" style="width:300px" value="C:\test\filelist.txt" /><BR />
    Cutoff: <input type="text" name="cutofflen" style="width:50px" value="240" /><BR />
    
    <BR />
    <input id=runbutton class="button" type="button" value="Run" name="run_button" onClick="RunScript">
    &nbsp;&nbsp;&nbsp;
    <input id=runbutton class="button" type="button" value="Close" name="close_button" onClick="CloseScript">
    
    <BR /><BR />
    Files Scanned: <span ID="fileCount"></span><BR />
    Files Over Limit: <span ID="longCount"></span>&nbsp;&nbsp;Max Length Found: <span ID="maxLength"></span>
</BODY>

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Sorry, too many editor tabs open. This version with the timer commented out, runs perfectly.
Code:
<HEAD>
    <title>List Files</title>
    <HTA:APPLICATION 
         APPLICATIONNAME="List Files Over Limit"
         SCROLL="no"
         SINGLEINSTANCE="yes"
    >
</HEAD>

<!-- ************************************************************************************** -->
<SCRIPT LANGUAGE="VBScript">
dim FSO, outFQN, filecnt, longcnt, outFile, file, maxlen, slen, SH


Sub CloseScript
   Self.Close()
End Sub

Sub Window_OnLoad
    window.resizeTo 800,300
End Sub
 
Sub UpdateStatus
        fileCount.InnerHTML = CStr(filecnt)
        longCount.InnerHTML = CStr(longcnt)
        maxLength.InnerHTML = CStr(maxlen)
        DoEvents
End Sub
 
Sub DoEvents()
   SH.run "%comspec% /c exit", 0, True
End Sub

Sub RunScript
'    iTimer = window.setInterval("UpdateStatus", 250)
    set SH = CreateObject("WScript.Shell")
    set FSO = CreateObject("Scripting.FileSystemObject")
    
    outFQN = outfiletxt.Value
    '----- If the output file does not exist, create it
    if not FSO.FileExists(outFQN) then
       FSO.CreateTextFile(outFQN)
    end if

    '----- Open file for append
    const ForReading = 1
    const ForWriting = 2
    const ForAppending = 8
    set outFile = FSO.OpenTextFile (outFQN, ForWriting, true)
    '========================= Logfile Initialization    - End

    longcnt = 0
    filecnt = 0
    maxlen = 0
    UpdateStatus
    
    ListFiles FSO.GetFolder(startfolder.Value)
    outFile.WriteLine ("Longest name found: " & CStr(maxlen))
    UpdateStatus
    
    '----- Cleanup
    set FSO = nothing
'    window.clearInterval(iTimer)
    set SH = nothing
    outFile.Close
End Sub

Sub ListFiles(folder)
    For Each file In folder.Files
        if filecnt Mod 200 = 0 then
            UpdateStatus
        end if
        filecnt = filecnt + 1
        slen = len(file.Path)
        if slen > maxlen then
            maxlen = slen
        end if
        if slen > CInt(cutofflen.value) then
            longcnt = longcnt + 1
            outFile.WriteLine ("""" & file.Path & """," & CStr(slen))
            outFile.WriteLine ("")
        end if
    Next
    For Each subfolder In folder.SubFolders
        ListFiles(subfolder)
    Next
End Sub
</SCRIPT>

<!-- ************************************************************************************** -->
<BODY>
    Starting Folder: <input type="text" name="startfolder" style="width:300px" /><BR />
    Output File: <input type="text" name="outfiletxt" style="width:300px" value="C:\test\filelist.txt" /><BR />
    Cutoff: <input type="text" name="cutofflen" style="width:50px" value="240" /><BR />
    
    <BR />
    <input id=runbutton class="button" type="button" value="Run" name="run_button" onClick="RunScript">
    &nbsp;&nbsp;&nbsp;
    <input id=runbutton class="button" type="button" value="Close" name="close_button" onClick="CloseScript">
    
    <BR /><BR />
    Files Scanned: <span ID="fileCount"></span><BR />
    Files Over Limit: <span ID="longCount"></span>&nbsp;&nbsp;Max Length Found: <span ID="maxLength"></span>
</BODY>

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
It must be a timing issue. Perhaps the .innerhtml doesn't have time to update before it is called to update next.

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding
 
I think it's partially that, but also, in one of the tests I put a counter in UpdateStatus and if I only called UpdateStatus from the SetInterval line, it never incremented, so it seems, in my case the timer never fires.

In any case, while the timed version is more elegant, updating every X files will work for showing progress.


Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top