I'm looking to read a folder which contain number of files. I want to get some details of those files, like Name, Size, Date Created. Those details I can see from the details view of the folder.
To start I try to find the file name as follows.
Try to find the paths of all files included in a folder as follows.
Code:
int CCountOne::SearchDirectory(vector &refvecFiles,const string &refcstrRootDirectory, const string &refcstrExtension, bool bSearchSubdirectories)
{
bSearchSubdirectories = true; // Handle the directories and subdirectories
string strFilePath; // Filepath
string strExtension;// Extension
string strPattern; // Pattern
HANDLE hFile; // File handler
WIN32_FIND_DATA FileInformation; // File information, use relevent structure of WinAPI
// Set the path pattern
strPattern = refcstrRootDirectory + "\\*.*";
hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);// Get the handler pointed
int iCount = 0;// Number of iterations
if(hFile != INVALID_HANDLE_VALUE)
{
do
{
if(FileInformation.cFileName[0] != '.')
{
strFilePath.erase();// Clear the exsisting path
strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;// Set the new path
if(FileInformation.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
{
if(bSearchSubdirectories)
{
// Search the subdirectories
int iRC = SearchDirectory(refvecFiles, strFilePath, refcstrExtension, bSearchSubdirectories);
if(iRC)
{
return iRC;
}
}
else
{
// Extension check
strExtension = FileInformation.cFileName;// Use the file name
strExtension = strExtension.substr(strExtension.rfind(".") + 1);
cout << strExtension << endl;
if(strExtension == refcstrExtension)
{
// Save the file name
refvecFiles.push_back(strFilePath);
}
}
}
else
{
cout << "Error_Two" << endl;
}
}
}while(::FindNextFile(hFile, &FileInformation) == TRUE);
// Close the handle
::FindClose(hFile);
DWORD dwError = ::GetLastError();
if(dwError != ERROR_NO_MORE_FILES)
{
return dwError;
}
}
return 0;
}
In this code set the folder path and start work.
Code:
void CCountOne::GetStart(void)
{
int iRC = 0;
vector vecSrfFiles;// bin file name
// Search "C:\Bin Files\Bin" for "*.srf" files
// Subdirectories also included
iRC = SearchDirectory(vecSrfFiles, "C:\\Bin Files\\Bin", "srf", true);
// Check the error
if(iRC != 0)
{
cout << "Error_One " << iRC << endl;
}
// Print the result
for(vector::iterator iterSrf = vecSrfFiles.begin(); iterSrf != vecSrfFiles.end(); ++iterSrf)
{
cout << *iterSrf << endl;
}
}
But it don't give any file path. Try to debug and check, seems
In your above code, you say, "if this file is a directory.. then do this, if it isn't, its an error". You have your file handling code underneath the directory handling code. IE:
Code:
if (directory) {
if (search_sub_directories) {
else {
// do stuff with files.. but it can't.. it already assumes this is a directory, not a file
}
etc.
Also, make sure theres a space between the ampersand and FILE_ATTRIBUTE_DIRECTORY.. just in case.
The cout class, doesn't support outputting types of FILETIME. cout supports all of your basic types, like, chars, char pointers, integers, floats, aswell as any type supported by the STL (like string for example).
To display the time properly, you need to convert the FILETIME variable to a normal time..
That _should_ work. If it doesn't, wait until I get home to actually be on a computer with a compiler (typing it into notepad isn't the best way to test something).
Actually this is wired me. As you said in your 2nd replay I test it, one thing is change. Define the variable 'os' very beginning of the of the application, but it don't work. There is no issue with the scope of the variable.
Then I define it just before use it. It works. I'm checking it why it is happened.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha