Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find the Files/Directory in a location

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
For sometime now I've been trying to find a way to get a string that contains: a list of files in a location and a list of directories(folders)OR a way to get both but be able to tell the difference between them.

Is there a way to do this without using system("dir.. to file") Thanks
 
I've cut+pasted this code from something I'd written, some of this may help you.

Code:
//	Check to see that this is a valid directory.
	String sDummy = String(_T("0"));
	int iDirchk = CheckForFile(rsDIR, sDummy, true);

	if (!iDirchk)
	{
		fprintf(f,"0,%s\n",rsDIR.c_str());

		//	We are now in a Directory.
		String sDirectory = rsDIR;
		sDirectory += String(_T("\\"));
		
		String sSearch = sDirectory;
		sSearch += String(_T("*.*"));

		//	We want to scan through this directory and extract all of the files.
		if ( (h=FindFirstFile(sSearch.c_str(),&finfo))!=INVALID_HANDLE_VALUE)
		{
			do
			{
				if(!(finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
				{
					//	Yipeee we have found a file.
					//	Construct the full name for this file.
					String sFullPath = sDirectory;
					String sVersion;
					sFullPath += String(finfo.cFileName);
					if (GetVersionStringForFile(sFullPath,sVersion))
					{
						//	File got version info.
						fprintf(f,"1,%s,%s\n",sFullPath.c_str(),sVersion.c_str());
					}
					else
					{
						//	File not got version info.
						fprintf(f,"1,%s,0\n",sFullPath.c_str());
					}
				}
			}while (FindNextFile(h,&finfo));			
			FindClose (h);
		}

		//	Search for Directories.
		if ( (h=FindFirstFile(sSearch.c_str(),&finfo))!=INVALID_HANDLE_VALUE)
		{
			do
			{
				if(finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				{
					//	Yipeee we have found a directory.
					//	Make sure it is not '.' or '..'

					if (_tcsncmp(finfo.cFileName ,_T("."),1) && 
						_tcsncmp(finfo.cFileName,_T(".."),2))
					{
						//	Construct the full name for this directory.
						String sFullPath = sDirectory;
						sFullPath += String(finfo.cFileName);

						//	Lets call ourselves with this new directory.
						iRet = BuildCSVFile(sFullPath, f);
					}
				}
			}while (FindNextFile(h,&finfo));			
			FindClose (h);
		}
 
look into CFileFind. It is an MFC class that could do what you are asking. It is pretty straight forward to use as well.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top