while(!FolderEnum.complete) throws "A method was called unexpectedly"

Trying to iterate through files in selected folders.
Could've sworn this should work, however, the while loop evaluation while (!contents.complete) throws

A method was called unexpectedly (0x8000ffff)

ReadDir does not return any error (see

function OnClick(clickData){
	var enumDirs = new Enumerator(clickData.func.sourcetab.selected_dirs);
    enumDirs.moveFirst();
    var path = clickData.func.sourcetab.path;
	var done = false;
	df = DOpus.Create();
    while (enumDirs.atEnd() == false){
    	var cf = enumDirs.item();
		var folder = cf.path + "\\" + cf.name;
		var contents = DOpus.FSUtil.ReadDir(folder);
		if(contents.error){ // checking for errors, but everything seems to be OK
			log("ERROR: " + contents.error);
			continue;
		}
		log("in folder " + folder); // yup, correct, existing folder
    	while (!contents.complete){ // <-- this somehow throws the above error
			var file = contents.next();
			if(file.is_dir()){
				log("not file, skip");
        		enumDirs.moveNext();
				continue;
			}
			var fpath = file.path + "\\" + file.name;
			log(fpath);
		}
		done = true;
        enumDirs.moveNext();
    }
	if(!done)
	msg("no file selected!");
}

What am I doing wrong?

Is that first enumDirs.moveNext(); supposed to be there within the while(!contents.complete) loop? It looks quite odd.

You're also missing an enumDirs.moveNext(); in the error case above there. I suspect it was inserted into the wrong place.

(Best to use a for loop over the Enumerator like most of the examples, if you're going to use continue.)

Oh! Exactly, that was meant to go into the error check above.

Hmmm... changing the continue behavior to an else block doesn't change anything.

I tried a for loop with for (var fEnum = new Enumerator(contents); !fEnum.atEnd(); fEnum.moveNext() ) (and to be sure, also with var fEnum = new Enumerator(DOpus.FSUtil.ReadDir(folder)); ), as in this example, but that loop never executed its block. Just for completeness sake, how would I enumerate the collection with a for-loop?

But mainly: I found the cause of the issue:

I was checking whether the item was a directory with file.is_dir(), but it needs to be file.is_dir: the property is not a function. I got confused there.

the console was reporting all errors 2 lines off, because my script had its first meaningful declaration at line 3.
I noticed that something was wrong when I commented something out in favor of an alternative, and suddenly the error was reported to be in a line that contained only a comment.

When the script starts like this:

var a = "b"; //<-- meaningful line
// var b = "c";
var c = "d";

function OnClick(ClickData){
...

, the compiler(?) starts counting at line 1.
But, treacherously, when it starts like this:

// var a = "b"; //<-- not meaningful line
// var b = "c"; //<-- not meaningful line
var c = "d"; //<-- first meaningful line

function OnClick(ClickData){
...

, the compiler(?) starts counting at line 3.
That's why I thought the error was happening in the evaluation of .complete :man_facepalming:

If that's not desired behavior, it might need fixing. Otherwise: thank you as always for your time and patience, I got it working now :slight_smile:

I meant using a for loop for the Enumerator you're already using over selected_dirs:
new Enumerator(clickData.func.sourcetab.selected_dirs)

You wouldn't use a for-loop or Enumerator with the ReadDir object, since that works differently. (It usually returns the items on-the-fly for faster performance, and where the total item count isn't known up-front, which isn't compatible with the way standard Enumerators work in scripting, so that has to be different.)

That does seem wrong. We'll take a look.

That has been fixed for the next beta.

1 Like