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.
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?
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.
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)
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.
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.
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.
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.