Question

I'm not sure if this is the correct place to post this question because I have a hunch that the behavior I witness will also be observed using other methods. But anyway, here it goes.

I have a VBscript that contains code like this:

For Each objFile In colFiles
...
Next

I've been running this code for quite some time on many different systems. I never bothered to order the files alphabetically. But today I found out by accident that the logic of my program depends on it. I ran the code on a new system (under Citrix) and the files were returned in a seemingly random order.

Does anybody know why Windows sometimes returns the files sorted alphabetically while sometimes it doesn't?

Added note: It might be relevant to note that the script as well as the input folder are on a network share (where my script outputs randomly ordered files).

Was it helpful?

Solution

Ordering is not supported for FileSystemObject. See KB 189751 http://support.microsoft.com/kb/189751/en-us

Also check out an answer on how to deal with that on SO Order of Files collection in FileSystemObject

OTHER TIPS

The docs do not specify an ordering. Thus, you cannot depend on it to have an order. The Files property needs to ask the underlying file system for the files, and then gives it to you as it, without any processing. If that file system happens to return the files in order, that's great. If not, you'll have to sort it. Regardless of whether it is in order, you should always order it if you expect it in a certain order because the implementation may change tomorrow (as you've just witnessed).

It depends on what data structure you are looping through. You will obviously get a different order if you use foreach loop in an array and a hashset, for example. Personally, I don't know anything about VB. But it does work this way in C#.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top