Results 1 to 14 of 14

Thread: File information of the folder content.

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink File information of the folder content.

    Hi all,

    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

    if(FileInformation.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)

    of code one stuck me with.

    Can you guys just look at it.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: File information of the folder content.

    Your logic is all screwed up.

    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.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: File information of the folder content.

    Yep, I got it and corrected. One thing to know.

    I want to get the file created time. Here what I have try.

    Code:
    		FILETIME fileName2 = FileInformation.ftCreationTime;
    		cout << fileName2 << endl;
    This give a compile error as follows.

    Code:
    binary '<<' : no operator found which takes a right-hand operand of type 'FILETIME' (or there is no acceptable conversion)
    Here FileInformation is the instance of WIN32_FIND_DATA api. Why it is not work.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: File information of the folder content.

    Do you understand what overloading is?

    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..
    Code:
    FILETIME fileName2 = FileInformation.ftCreationTime;
    SYSTEMTIME stUTC, stLocal;
    
    FileTimeToSystemTime(&fileName2, &stUTC);
    SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
    
    std::ostringstream os;
    os << "File Creation Time Is: " << stLocal.wMonth << "/" << stLocal.wDay << "/" << stLocal.wYear << " " << stLocal.wHour << ":" << stLocal.wMinute
    
    cout << os;
    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).

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: File information of the folder content.

    Should I include iosfwd to use ostringstream? Compiler says that class is undefined.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: File information of the folder content.

    Code:
    #include <sstream>
    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: File information of the folder content.

    How did you go?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  8. #8

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: File information of the folder content.

    I follows what you say. But the ostringstream gives some characters as the date. I'm working out on it now.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  9. #9
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: File information of the folder content.

    This is what includes the date:
    Code:
    << stLocal.wMonth << "/" << stLocal.wDay << "/" << stLocal.wYear
    ..remove that and it will be what you want.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  10. #10

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: File information of the folder content.

    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

  11. #11
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: File information of the folder content.

    Strange.. post your code and I can have a look.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  12. #12

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: File information of the folder content.

    Code:
    	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(bSearchSubdirectories)// Specified directory
    				{
    				// Search the subdirectories
    					int iRC = SearchDirectory(refvecFiles, strFilePath, refcstrExtension, bSearchSubdirectories);
    					if(iRC)
    					{
    						return iRC;
    					}
    					else
    					{
    						cout << "Error_One" << endl;
    					}
    				}
    				else
    				{
    					strExtension = FileInformation.cFileName;// Use the file name
    					strExtension = strExtension.substr(strExtension.rfind(".") + 1);
    					if(strExtension == refcstrExtension)
    					{
    					// Save full file path for later use
    						refvecFiles.push_back(strFilePath);
    
    					// File name
    						ftName = FileInformation.cFileName;
    						cout << ftName << " ";
    
    					// File time created
    						ftCreate = FileInformation.ftCreationTime;
    						FileTimeToSystemTime(&ftCreate, &stUTC);
    						SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
    
    						ostringstream osCre;
    						osCre << stLocal.wMonth << "/" << stLocal.wDay << "/" << stLocal.wYear 
    							<< " " << stLocal.wHour << ":" << stLocal.wMinute << "\t";
    						cout << osCre.str();
    					}
    				}
    			}
    		}while(::FindNextFile(hFile, &FileInformation) == TRUE);
    
    		::FindClose(hFile);// Close the handle
    	}
    Here is a part of my code. Hope it is enough, because my code is too bulky, it will mess here in the forum.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  13. #13
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: File information of the folder content.

    Attach your project.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  14. #14

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: File information of the folder content.

    Ok, here it is. I used binary files to check it on GetStart(). You can easily see it.

    And also I attached .cpp files only, because of the attach file size limitation.
    Attached Files Attached Files
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width