Get filenames DESC/ASC

Hello guys!

I have a folder filled with txt files(no content), and every file is named with a unique number.

I want to get every filename starting with the highest name(number). I tried doing this with findfirstfile, findnextfile, but it always starts with the lowest name.

I know that I can read everything in to RAM, and then sort it. But I wonder if there is a way to start reading backwards right away from the disc? (Or maybe change a ground/core-setting for the folder, so it keeps the right order internally? Because then it would work with findfirst/nextfile)

I also tried to put a minus infront of the names, but it didn't do the trick. -1 is still lesser than -2 (in the folder) and therefore it comes first.


Any ideas or tips ?
Last edited on
I'm not a Windows guy, but I'm kind of surprised that it would be returning you the files in lexicographical order at all. Did you happen to create all of those numbered files in order? Just for a test, try creating them in a new directory in the reverse order. What happens then?
Last edited on
They are reorganized by name, same order as before
In that case it seems like you must sort them yourself.
Anyone else who has any thoughts about this?
In Windows Explorer (at least in Windows 7) choose to view "By Details"; that will put them in text columns. Click on the heading tag for a column and it will sort by that column (e.g. name, type, size etc.) Click again on that column and it will sort them in reverse order.

No idea why you want to do this. It's occasionally useful for dates, almost never for names.
Hello lastchance.

I tried that, but it does not keep its order when I use findfirstfile / nextfile to get every file from that folder.
Open a command Window. Use cd ("change directory") to navigate to the folder you want.

Enter
dir /O-n > MyList
and it will list the files in "MyList", which you can edit to do what you like with them.

I've no idea what you mean by "use findfirstfile / nextfile" or even "get every file".

I've no idea what you mean by "use findfirstfile / nextfile" or even "get every file".


They are functions from winapi to retrive files from a folder. When I use them they always get the files from the same order. (It doesn't matter if I press on the "name column" to change it in the windows explorer, because it doesn't keep that order later when I use the functions in my c++ program to get every file from that folder)
Last edited on
Why don't you read the file names into a vector and sort them, or maybe read them into a sorted container. You can't force windows(and probably no other OS) to store your files in a certain order on the disc.
Last edited on
As lastchance said, it would be very easy to do in DOS.
Create a bat file, type :
dir /O-n/B >list.txt
to create a list of just the file names sorted in order backwards.

If you find a way to list and sort faster than DOS can do it I would love to hear about it.
If you do this in a program then you'll have to read the file names and sort them yourself. The problem is that you have no guarantee what type of filesystem you're reading from. If it's a network mounted drive then it could be practically anything, and that means findFirst/findNext might present the files practically any order.

There's no guarantee that files are stored in any particular order.
Anyone else who has any thoughts about this?

It's so easy to suck them into a vector and sort them. What's the problem?

Or, if they are actually a bunch of numbered files then don't you basically know the filenames already (or are they not consecutive)?
Last edited on
dunno what you have but I am absolutely certain that you can set the file open reader widget in the GUI tools to sort the files by name. You can get the list of files from this. Its going to be slower than a system(..) or similar (shellexecute etc) call using the above command line invocation.

you could read the filesystem entries directly, I suppose -- defrag tools do it that way.

You cannot read a rotating disk backwards. I don't think you really meant that, but its a no-go. Flash disks, you probably can, but maybe not with the standard driver.

I am pretty sure that military format timestamps will sort properly and easily.
eg now for me is 2018110811143000filename.dat
the next one you write might be at 11:45 etc... it works.
Last edited on
Thank you all for your answers. I will probably choose one of the suggestions above

you could read the filesystem entries directly, I suppose -- defrag tools do it that way.


Do you mean like arrays? e.g infolder[0], infolder[1] and so on? This would be great
not really. Your hard disk has a table of what is where on it. You can get the list of files from it, but its rather low level, unportable, and significantly more work. I haven't done it since fat32 -- NTFS is a mystery to me. I wouldn't go here, unless you are trying to do something in real time, and if that is the case, there is probably a better approach. But it is possible!
I still don't understand why you don't just read the filenames into a collection and sort it into whatever order you need. This isn't evil, it's actually a perfectly reasonable way to do it and it's what any program that lists files in a predefined order must do.
Topic archived. No new replies allowed.