From: Giff on
Hi all,

So far, when I had to load a bunch of files from a directory, I always
used the functions _findfirst() and _findnext().

Now, however, I don't seem to be able to make them read some files in
the right order. The files are named something like CCC_NNN_NNNNNN.ext
where C are letters and N numbers. The last sequence of numbers tells
me the order in which to read the files (not necessarily starting from
0) but _findnext() does not seem to care and loads the files in a
different order.

Any ideas on how to have the job done the right way?

Thanks a lot,
G
From: Victor Bazarov on
Giff wrote:
> So far, when I had to load a bunch of files from a directory, I always
> used the functions _findfirst() and _findnext().
>
> Now, however, I don't seem to be able to make them read some files in
> the right order. The files are named something like CCC_NNN_NNNNNN.ext
> where C are letters and N numbers. The last sequence of numbers tells
> me the order in which to read the files (not necessarily starting from
> 0) but _findnext() does not seem to care and loads the files in a
> different order.
>
> Any ideas on how to have the job done the right way?

Read the entire directory (using '_findfirst' and '_findnext') into
a vector (or a list) and *sort* it before processing. If your files
are numbered correctly (with leading zeros where required), you
should be able to get them in the needed order.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


From: Igor Tandetnik on
Giff <Giffnews(a)gmail.com> wrote:
> So far, when I had to load a bunch of files from a directory, I always
> used the functions _findfirst() and _findnext().
>
> Now, however, I don't seem to be able to make them read some files in
> the right order. The files are named something like CCC_NNN_NNNNNN.ext
> where C are letters and N numbers. The last sequence of numbers tells
> me the order in which to read the files (not necessarily starting from
> 0) but _findnext() does not seem to care and loads the files in a
> different order.
>
> Any ideas on how to have the job done the right way?

Enumerate all the file names, sort them any way you want, then read the
files in the sorted order.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


From: Tom Serface on
Giff,

I see some others have given you suggestions about reading the files in
first then sorting. The files are read from the file system based on a
create order and often fill in holes from previous files that were deleted
so there is no way to presuppose how they will be listed. Depending on the
number of files you have in the folder reading them all into a container
(vector or CObList or something like that) should be pretty easy to do then
you can sort them any way you want and draw from your own list.

Another thing you could do, if you know the files that are supposed to be
there, is just create the names in your program and attempt to open them in
the order you want.

Tom

"Giff" <Giffnews(a)gmail.com> wrote in message
news:27355b07-f879-4072-a49f-59c3c2df974f(a)y38g2000hsy.googlegroups.com...
> Hi all,
>
> So far, when I had to load a bunch of files from a directory, I always
> used the functions _findfirst() and _findnext().
>
> Now, however, I don't seem to be able to make them read some files in
> the right order. The files are named something like CCC_NNN_NNNNNN.ext
> where C are letters and N numbers. The last sequence of numbers tells
> me the order in which to read the files (not necessarily starting from
> 0) but _findnext() does not seem to care and loads the files in a
> different order.
>
> Any ideas on how to have the job done the right way?
>
> Thanks a lot,
> G

From: Giovanni Dicanio on

"Giff" <Giffnews(a)gmail.com> ha scritto nel messaggio
news:27355b07-f879-4072-a49f-59c3c2df974f(a)y38g2000hsy.googlegroups.com...

> Now, however, I don't seem to be able to make them read some files in
> the right order. The files are named something like CCC_NNN_NNNNNN.ext
> where C are letters and N numbers. The last sequence of numbers tells
> me the order in which to read the files (not necessarily starting from
> 0) but _findnext() does not seem to care and loads the files in a
> different order.
>
> Any ideas on how to have the job done the right way?

As general guidelines, I agree with what others wrote.

You may find here a sample C++ code that I would use (compiled fine on
VS2008):

Note that AfxMessageBox is an MFC function to display text in message-boxes,
remove that and substitute with whatever you want.

I used std::vector as container of CString's (I like CString class).

I also used std::sort for sorting algorithm (defining a custom compare
function, named "FileNameLesser, which does sorting basing only on last 6
numbers of file names NNNNNN - I'm not sure if that is what you asked in
your original post... if not, you can just modify the implementation of
FileNameLesser.)

I used _tfindfirst instead of findfirst, to make code Unicode-aware (it
compiled fine in VS2008 default Unicode builds).

There are comments in the code, to explain things.


<code>

//////////////////////////////////////////////////////////////////////////


//
// Returns whether first filename string is lesser than the second,
// basing on custom sorting method.
//
bool FileNameLesser( const CString & filename1, const CString & filename2 )
{
//
// Strings are in this format:
//
// CCC_NNN_NNNNNN.ext
// 012345678 <-- start position for CString.Mid = 8
// ****** <-- count for CString.Mid = 6
//
// Sort based on last sequence of numbers: NNNNNN
//

// Extract the sorting-numbers (substring) from original strings
CString pos1 = filename1.Mid(8, 6);
CString pos2 = filename2.Mid(8, 6);

// Compare the sorting numbers of each string
return ( pos1 < pos2 );
}


//
// Filenames Sorting Test
//
void SortTest()
{
// Where to find files
CString fileSpec = _T("I:\\Test\\*.txt");


//
// Init find job
//
_tfinddata_t fileInfo;
intptr_t findHandle = _tfindfirst( fileSpec, &fileInfo );
if ( findHandle == -1 )
{
// No file or error...
// ...
// ...

AfxMessageBox( _T("No file or error...") );
return;
}

//
// Will store file names in vector<CString> container
//
typedef std::vector<CString> StringList;
StringList fileNames;

//
// Find loop
//
do
{
// Add file name to file name list
fileNames.push_back( fileInfo.name );

} while ( _tfindnext( findHandle, &fileInfo ) == 0 );

// Close find job
_findclose( findHandle );


//
// Sort the string in the array - using custom sorting
//
std::sort( fileNames.begin(), fileNames.end(), FileNameLesser );


//
// Do whatever on sorted filename strings:
//
// - just display them here
CString sortedFileNames;
for ( size_t i = 0; i < fileNames.size(); i++ )
{
sortedFileNames += fileNames.at(i);
sortedFileNames += CString(_T("\n") );
}
AfxMessageBox( sortedFileNames );

return;
}

//////////////////////////////////////////////////////////////////////////

</code>



HTH,
Giovanni