Archive: how to list subdirectories in order


how to list subdirectories in order
Hello. :)

I need to generate a list of subdirectories in a CAD folder... oh well, no need for those silly details, let me simplify it.

OK - I have a directory on the system somewhere called 'CAD' whose contents I do not know in advance. I need to list the subdirectories of 'CAD' in a .txt file, and they must be listed in order.

Alphabetical order, or the order in which windows lists them (which isn't quite alphabetical). Either way will do fine, but it has to be ordered. :(

I have seen this thread, saying that NSIS just uses Windows API calls and that's why there's no order that NSIS can control but it's almost a year old and I was hoping perhaps there was some change here. I'm no Windows coder at all, but wouldn't there be some API call for getting the directory contents in order that NSIS could use?

I have written a script that lists the subdirectories of a specified folder. The results can be viewed in the installer details page or in the output file created by the installer. It would be great if the attached script could be made to output the subdirs in order. It's only 65 lines long (the script). not complicated. ;)


If there is absolutely no way that NSIS can do this :( then would it be possible somehow to sort the output file itself, i.e. write the unordered file, then call a sort function that sorts the file and outputs to a new, sorted file?

I thank you for any help you can provide. :)


There is no such API function I know of. You can try using DOS sort command. If it's not on every version of Windows, you might be able to simply include it with your installer, and if not DOS sort, then Win32GNU sort.exe.

Use something like:

sort file > newfile
del file
ren newfile file


Wow - thanks for the quick reply kichik. :)

Ok, I'll look around and see if i can find an external sort program that i can use.

Man - I just tried the 'sort' command on xp and it worked beautifully, I wonder if it'll work on other win32 though: win95 and up.

I was trying to think of how to do an insertion sort from within NSIS on my unordered output file, but wasn't gettng very far... ;) weird without arrays of strings. :P

Do you think it would be possible to efficiently sort an unordered file from NSIS kichik (i.e. with fileread, strcmp etc.), or no?

I like to do everything from NSIS as much as possible, plus I'll have to try and hide the DOS window with external 'sort' - I think I saw some threads about that.

Anyway, thanks again for your help, I appreciate it. :)

(Edit: win32GNU sort works great too, plus the syntax is more familiar to me ;))
(Edit: nsExec seems to be the trick to hiding dos windows - this forum is great. :))


Sorting a list in alphabetical order is possible with NSIS scripting. You can use the stack and a sort algorithm like bubble sort.


thanks for the tip joost! :)
(and thanks for your work on MUI :D)

unfortunately, bubble sort is inefficient :( (might be a problem with a big file), and if i recall correctly, one needs to know the number of objects (though i suppose this could be determined by couting lines in the file).


I mentioned bubble sort because it's a simple one. You can also use a more advaned algorithm, but you will only notice a difference when you have to sort thousands of lines. Having to know the number of objects should not be a problem. If you are using the script to get the items, there is no need to write them to a file before sorting.


Hmmm, I'm sure there will never be thousands of lines to sort. ;)

If efficiency is not a problem, then I want to do it from 'inside' NSIS. :D

Could I trouble you to perhaps point me in the right direction or get me started? I must confess that I am having trouble envisioning how to do a bubble sort with NSIS. Your comment that I don't even need to write to a file before sorting surprises me (pleasntly). ;)

I would greatly appreciate any insight you could provide to me in implementing bubblesort.


if you don't mind adding 9kb to your installer that will do the thing (you'll need math plugin).


Sorry: second math call need no /NOUNLOAD. Updated script attached.


thank you brainsucker!

amazing, simply amazing work...

it's great to be able to do this from 'inside' nsis, with your math plugin of course. :D

now i just need to sit down and carefully look at all the math examples and documentation to see how this gem actually does it's magic. :)