 |
|
|
 |
|
 |
it cant help when u r dealing with unicode
|
|
|
|
 |
|
 |
Not just unicode! How would you possibly use dir /s /b in a unmanaged c/c++ program in which you need to find or use file paths internally?
Regards,
Shup
Mind & Machines
|
|
|
|
 |
|
 |
Aha, you want to make the data available within your program ? My idea: - make your program spawn a system command: dir /s/b>file.txt - process this file.txt within your program What would be wrong with that ?
|
|
|
|
 |
|
 |
Obviously, the demo application and source code are there to demonstrate how the function in question can be used within a program, so that people understand its usage in code better. The purpose of this forum is not for posting free applications, its for helping developers. So I recommend reading an article properly before voting and also read some of the useful comments from people who used it.
Besides the Unicode problem, there are several more with your approach:
1) File I/O operations are slow, so doing that will take a toll on performance.
2) When we read the text file, it has to be parsed, this will make it even less efficient.
3) What if you are not allowed in your application to invoke a system command?
Think about writing, reading and parsing a list of millions of files!
Regards,
Shup
Mind & Machines
|
|
|
|
 |
|
 |
Well, if you make and change the rules on the fly to suit your purpose, OK.
|
|
|
|
 |
|
 |
The material is basic, but the article is clearly written and could be useful for a beginner.
Just because the code works, it doesn't mean that it is good code.
|
|
|
|
 |
|
 |
It is meant for beginners If you read my comment on the previous comment on the "My vote of 1" you'll get a clearer idea of why I write these kind of articles.
Regards,
Shup
Mind & Machines
|
|
|
|
 |
|
|
 |
|
 |
too basic/painfully obvious. This is 101 stuff. If someone were struggling trying to implement this, they're in the wrong profession.
|
|
|
|
 |
|
 |
Hi,
Thanks for your comment.
This article isn't directed to advanced developers like yourself, probably that is why you think its silly. Right now after 10 years of experience, it does look and sound silly, I will agree with you on that. But back when I was still a beginner, I remember spending days (my be I wasn't smart enough) just trying to find out how to get this right using unmanaged code which was the only option back then.
And now running my own software development company, I see a lot of new and even advanced developers spending hours to entire days to get this sort of simple thing working right when unmanaged C++ is the only option available for a project, so I thought even though this is simple and silly, its worth an article because there are a lot of developers still needed and therefore specializing in unmanaged C++ who have to waste a lot of time finding out just this sort of silly stuff.
Therefore, I will be posting more articles, when ever I have the time, with simple functions that are equally or even more silly than this one just to help those developers who are too shy to ask questions that, to a lot of experienced developers like yourself, sound too silly.
Regards,
Shup
Mind & Machines
|
|
|
|
 |
|
 |
i second it....when u r dealing with the problem...any sort of solution helps to make ur day...
this code helps me when i was dealing with the unicode and working in console application.
DO What Never had Done....Nothing is Impossible
|
|
|
|
 |
|
 |
Thanks
Regards,
Shup
Mind & Machines
|
|
|
|
 |
|
 |
hi,
i am replacing the following code:
_stprintf_s(strDesktopPath, _MAX_PATH, _T("%s\\*.*"), strCurrentDirectory.c_str());
with
_stprintf_s(strDesktopPath, _MAX_PATH, _T("%s\\*.txt"), strCurrentDirectory.c_str());
it gives the results but didn't include the results from sub-directory although the checkbox is checked.
basically i need an option to search based on extenions like txt doc ppt mpg jpg...please help me in this regard
|
|
|
|
 |
|
 |
I just found a mistake in the code that's preventing sub-directory listing to work. Give me an hour or so to update the article so that the sub-directory listing works properly.
Then you can add a separate parameter to the function for the wild cards to work.
e.g. void GetFileList(const stlString& strTargetDirectory, bool bLookInSubdirectories, vector& vecstrFileList, const stlString& strWildCard);
On second thought, let me add that feature for you. Give an hour or so!
Regards,
Shup
Mind & Machines
|
|
|
|
 |
|
 |
The changes have been made to the article and submitted. Probably in a few hours it will be updated on site. If you can't wait that long replace the relevant section of the mentioned files with the new code below:
CUtil.h
#ifdef UNICODE
#define _tstring wstring
#else
#define _tstring string
#endif
#ifdef WIN32
#define DIRECTORY_SEPARATOR_W L"\\"
#define DIRECTORY_SEPARATOR_W_C L'\\'
#define DIRECTORY_SEPARATOR_A "\\"
#define DIRECTORY_SEPARATOR_A_C '\\'
#else
#define DIRECTORY_SEPARATOR_W L"/"
#define DIRECTORY_SEPARATOR_W_C L'/'
#define DIRECTORY_SEPARATOR_A "/"
#define DIRECTORY_SEPARATOR_A_C '/'
#endif
#ifdef UNICODE
#define DIRECTORY_SEPARATOR DIRECTORY_SEPARATOR_W
#define DIRECTORY_SEPARATOR_C DIRECTORY_SEPARATOR_W_C
#else
#define DIRECTORY_SEPARATOR DIRECTORY_SEPARATOR_A
#define DIRECTORY_SEPARATOR_C DIRECTORY_SEPARATOR_A_C
#endif
// Get list of all files in the target directory
static void GetFileList(const _tstring& strTargetDirectoryPath, const _tstring& strWildCard, bool bLookInSubdirectories, vector<_tstring>& vecstrFileList);
CUtil.cpp
void CUtil::GetFileList(const _tstring& strTargetDirectoryPath, const _tstring& strWildCard, bool bLookInSubdirectories, vector<_tstring>& vecstrFileList)
{
if(strTargetDirectoryPath.compare(_T("")) == 0)
{
return;
}
_tstring strCurrentDirectory = RemoveDirectoryEnding(strTargetDirectoryPath);
WIN32_FIND_DATA fdDesktop = {0};
_tstring strDesktopPath = _T("");
strDesktopPath += _T("\\\\?\\");
strDesktopPath += strCurrentDirectory;
strDesktopPath = AddDirectoryEnding(strDesktopPath);
if(strWildCard.compare(_T("")) == 0)
{
strDesktopPath += _T("*.*");
}
else
{
strDesktopPath += strWildCard;
}
// Finds the first file and populates the WIN32_FIND_DATA data structure with its information
// The return value is a search handle used in subsequent calls to FindNextFile or FindClose functions
HANDLE hDesktop = ::FindFirstFile(strDesktopPath.c_str(), &fdDesktop);
// If an invalid handle is returned by FindFirstFile function, then the directory is empty, so nothing to do but quit
if(hDesktop == INVALID_HANDLE_VALUE)
{
return;
}
// Do this on the first file found and repeat for every next file found until all the required files that match the search pattern are found
do
{
// Reconstruct the path
_tstring strPath = _T("");
strPath += strCurrentDirectory;
strPath = AddDirectoryEnding(strPath);
strPath += fdDesktop.cFileName;
// Check if a directory was found
if(fdDesktop.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// Get the name of the directory
_tstring strCurrentDirectoryName = GetDirectoryName(strPath);
// If its a current (.) or previous (..) directory indicator, just skip it
if((strCurrentDirectoryName.compare(_T(".")) == 0) || (strCurrentDirectoryName.compare(_T("..")) == 0))
{
continue;
}
// Other wise this is a sub-directory
else
{
// Check whether function was called to include sub-directories in the search
if(bLookInSubdirectories)
{
// If sub-directories are to be searched as well, recursively call the function again, with the target directory as the sub-directory
GetFileList(strPath, strWildCard, bLookInSubdirectories, vecstrFileList);
}
}
}
// A file was found
else
// if(fdDesktop.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
{
// Add the string to the vector
vecstrFileList.push_back(_tstring(strPath));
}
}
// Search for the next file that matches the search pattern
while(::FindNextFile(hDesktop, &fdDesktop) == TRUE);
// Close the search handle
::FindClose(hDesktop);
}
Regards,
Shup
Mind & Machines
|
|
|
|
 |
|
 |
thnx alot dear it helps me alot...tell u u shud put some sleep() in the code..because when it runs with a string like *.txt;*.doc;*.jpg; on drives c,d,e it consumes more than 30% cpu...memory managment is good...overall i rate it 5....thnx dude
|
|
|
|
 |
|
 |
Syed haseeb baber wrote: u shud put some sleep() in the code..because when it runs...it consumes more than 30% cpu So putting it to sleep will make it run faster? What a novel concept.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
 |
|
 |
so whats the novel approach when a code is consuming cpu????
|
|
|
|
 |
|
 |
I work primarily in C#, and had to do some directory parsing in unmanaged C++ - this definitely helped me get going in the right direction.
|
|
|
|
 |
|
 |
You're welcome
Regards,
Shup
Mind & Machines
|
|
|
|
 |
|
|
 |
|
 |
I'm glad its helpful to you! Use it in anyway you like and thanks a lot for a comments
A good word goes a long way into providing inspiration to write more articles in the hope of it being useful to someone.
Regards,
Shup
Mind & Machines
|
|
|
|
 |
|
 |
I also request you to go through my articles at code project
http://www.codeproject.com/KB/openGL/stlviewer.aspx
http://www.codeproject.com/KB/openGL/cadsurf.aspx
http://www.codeproject.com/KB/library/Python_Embedded_Dialog.aspx
Regards
N. Sharjith
|
|
|
|
 |
|
 |
Hi,
I get the compile error:
c:\temp\GetFileList\GetFileList\Util.cpp(103) : error C3861: '_stprintf_s': identifier not found, even with argument-dependent lookup
please help me !
thanks
Saffarian
|
|
|
|
 |