0

This code is for internal, offline, single user use, IE only. The code looks at a folder, and lists all files including those in subfolders. It sorts through the data based on some date fields and datelastmodified. It also uses and if to throw out thumbs.db entries. All of the data is put into a table.

My issue is that this script can take a long time to get the data. I would like to add a progress bar but the progress bar cant update while the script is running. After some research it looks like SetTimeOut can allow the page elements to be updated as the script runs, therefore allowing the progress bar to work and looking overall cleaner. However I can not figure out of to implement SetTimeOut into my existing code.

<script type="text/javascript">
var fso = new ActiveXObject("Scripting.FileSystemObject");

function ShowFolderFileList(folderspec) {
    var beginningdate = new Date(startdate.value);
    var finishdate = new Date(enddate.value);
    var s = "";
    var f = fso.GetFolder(folderspec);
    var subfolders = new Enumerator(f.SubFolders);
    for (subfolders.moveFirst(); !subfolders.atEnd(); subfolders.moveNext()) {
        s += ShowFolderFileList(subfolders.item().path);
    }
    // display all file path names.
    var fc = new Enumerator(f.files);
    for (i = 0; !fc.atEnd(); fc.moveNext()) {
        if (fc.item().name != "Thumbs.db") {
            var dateModified = fc.item().DatelastModified;
            if (dateModified >= beginningdate && dateModified <= finishdate) {
                Date.prototype.toDateString = function () {
                    return [this.getMonth() + 1, '/', this.getDate(), '/', this.getFullYear()].join('');
                }
                var dateModifiedClean = (new Date(fc.item().DatelastModified).toDateString());
                s += "<table border=0 width=100% cellspacing=0><tr " + ((i % 2) ? "" : "bgcolor=#EBF1DE") + "><td width=75%><font class=find><b>" + fc.item().ParentFolder.name + "</b>" + " - " + fc.item().name + "</font></td><td width=25% align=right><font class=find>" + dateModifiedClean + "</font></td></tr>";
                i++;
            }
        }
    }
    var results = s + "</table>";
    return results;
}

function listFiles() {
    outPut.innerHTML = ShowFolderFileList('*Path to scan*');
}
</script>

outPut is the ID of a div tag where the results table is displayed. A button calls the listfiles function.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • using a settimeout would not help you as javascript is synchronous so while you are in a loop no other parts of the code will run till it is done and the page will not be able to re-render during a loop either. Which is why you try to stay away from long loops as it blocks the UI – Patrick Evans Jan 30 '14 at 23:44
  • This is the first time I have tried to use JavaScript in this way. Is there a way I could implement a progress bar in this code at all? Thanks. – KCraig530 Jan 30 '14 at 23:58
  • git rid of the for loop and make it's code a function you can call. then call setTimeout(theFunction, 100) from the end of the function if(!subfolders.atEnd()). you can do both loops like that to update upon each file, or just the outer loop to update each folder. – dandavis Jan 31 '14 at 00:18
  • @PatrickEvans: `setTimeout` will help to make the loop (chunk processing) asynchronous? – Bergi Jan 31 '14 at 00:42
  • possible duplicate of [JavaScript Performance Long Running Tasks](http://stackoverflow.com/questions/6864397/javascript-performance-long-running-tasks) – Bergi Jan 31 '14 at 00:47

0 Answers0