From: Electronic75 on
Hello, I have a program that has to auto save some data automatically. I want
to list autosave directory content and find the file with biggest serial
number e.g. AutoSave_1, AutoSave_2, AutoSave_3,... and then create a file
that its name is one greater than biggest value. I have to list directory
content and then look for names. Problem is I couldn't find any info on MSDN
on how I cando this in MFC. the only enumeration method I could find was
under .Net How should I do this in MFC?
thanks a lot.
From: David Lowndes on
>Hello, I have a program that has to auto save some data automatically. I want
>to list autosave directory content and find the file with biggest serial
>number e.g. AutoSave_1, AutoSave_2, AutoSave_3,... and then create a file
>that its name is one greater than biggest value. I have to list directory
>content and then look for names. Problem is I couldn't find any info on MSDN
>on how I cando this in MFC. the only enumeration method I could find was
>under .Net How should I do this in MFC?

Have a look at the FindFirstFile API (wrapped as CFileFind in MFC).

Dave
From: Giovanni Dicanio on

"Electronic75" <Electronic75(a)discussions.microsoft.com> ha scritto nel
messaggio news:32B26F76-F1B6-420D-9F02-CE798989EC03(a)microsoft.com...

> have to list directory
> content and then look for names.

You may consider the following simple code I developed.
The function ListFilesInDirectory takes as input the directory path, and
returns the list of files in the directory (using std::vector as a container
for filenames).

You can use like this:

<code>

CString directory = _T("C:\\SomeData");
std::vector< CString > filenames;

ListFilesInDirectory( directory, filenames );

// filenames contain the list of files in input directory

</code>

ListFilesInDirectory implementation follows:


<code>

//========================================================================
// Given a directory pathname, returns the list of files (full pathname)
// present in that directory. The returned list can be empty, if the
// directory is empty.
//
//
// Note - subdirectories:
// ----------------------
// If there are subdirectories under specified directory, they are
// ignored by this function.
//
//
// Note - vector:
// --------------
// The file path list is returned in a std::vector container, so
// <vector> STL header must be #included in StdAfx.h.
//
//========================================================================
void ListFilesInDirectory(
LPCTSTR dirName,
std::vector< CString > & filepaths )
{
// Check input parameters
ASSERT( dirName != NULL );

// Clear filename list
filepaths.clear();

// Object to enumerate files
CFileFind finder;

// Build a string using wildcards *.*,
// to enumerate content of a directory
CString wildcard( dirName );
wildcard += _T("\\*.*");

// Init the file finding job
BOOL working = finder.FindFile( wildcard );

// For each file that is found:
while ( working )
{
// Update finder status with new file
working = finder.FindNextFile();

// Skip '.' and '..'
if ( finder.IsDots() )
{
continue;
}

// Skip sub-directories
if ( finder.IsDirectory() )
{
continue;
}

// Add file path to container
filepaths.push_back( finder.GetFilePath() );
}

// Cleanup file finder
finder.Close();
}

</code>


HTH,
Giovanni


From: Electronic75 on
thanks Giovanni and Dave
problem solved!
unfortunately MSDN(microsoft in general) search capability is so poor that
sometimes one has to be ultra creative and lucky to find something that he
looks for. I tried "directory mfc" folder mfc" "directory" "folder " and
many other keywords but instead of answering my query MSDN results were
singing about .Net
Thanks a lot guys, you both helped much.
Thanks for the code Giovanni.

Best regards



"Giovanni Dicanio" wrote:

>
> "Electronic75" <Electronic75(a)discussions.microsoft.com> ha scritto nel
> messaggio news:32B26F76-F1B6-420D-9F02-CE798989EC03(a)microsoft.com...
>
> > have to list directory
> > content and then look for names.
>
> You may consider the following simple code I developed.
> The function ListFilesInDirectory takes as input the directory path, and
> returns the list of files in the directory (using std::vector as a container
> for filenames).
>
> You can use like this:
>
> <code>
>
> CString directory = _T("C:\\SomeData");
> std::vector< CString > filenames;
>
> ListFilesInDirectory( directory, filenames );
>
> // filenames contain the list of files in input directory
>
> </code>
>
> ListFilesInDirectory implementation follows:
>
>
> <code>
>
> //========================================================================
> // Given a directory pathname, returns the list of files (full pathname)
> // present in that directory. The returned list can be empty, if the
> // directory is empty.
> //
> //
> // Note - subdirectories:
> // ----------------------
> // If there are subdirectories under specified directory, they are
> // ignored by this function.
> //
> //
> // Note - vector:
> // --------------
> // The file path list is returned in a std::vector container, so
> // <vector> STL header must be #included in StdAfx.h.
> //
> //========================================================================
> void ListFilesInDirectory(
> LPCTSTR dirName,
> std::vector< CString > & filepaths )
> {
> // Check input parameters
> ASSERT( dirName != NULL );
>
> // Clear filename list
> filepaths.clear();
>
> // Object to enumerate files
> CFileFind finder;
>
> // Build a string using wildcards *.*,
> // to enumerate content of a directory
> CString wildcard( dirName );
> wildcard += _T("\\*.*");
>
> // Init the file finding job
> BOOL working = finder.FindFile( wildcard );
>
> // For each file that is found:
> while ( working )
> {
> // Update finder status with new file
> working = finder.FindNextFile();
>
> // Skip '.' and '..'
> if ( finder.IsDots() )
> {
> continue;
> }
>
> // Skip sub-directories
> if ( finder.IsDirectory() )
> {
> continue;
> }
>
> // Add file path to container
> filepaths.push_back( finder.GetFilePath() );
> }
>
> // Cleanup file finder
> finder.Close();
> }
>
> </code>
>
>
> HTH,
> Giovanni
>
>
>
From: Giovanni Dicanio on

"Electronic75" <Electronic75(a)discussions.microsoft.com> ha scritto nel
messaggio news:260AFA4B-F4FC-4815-9077-401220E2BDD5(a)microsoft.com...

> unfortunately MSDN(microsoft in general) search capability is so poor that
> sometimes one has to be ultra creative and lucky to find something that he
> looks for. I tried "directory mfc" folder mfc" "directory" "folder " and
> many other keywords but instead of answering my query MSDN results were
> singing about .Net

You have a good point :)

I think that Microsoft got several feedbacks about that, and I hope they fix
that in the future.


> Thanks for the code Giovanni.

You're welcome.

Giovanni