Folder Scan - GCC Compatible

Hi experts,

In the distant past, I got some great help with a recursive directory scan function - see link.

http://www.experts-exchange.com/Programming/Languages/CPP/Q_24391623.html

 Since then however, we have a requirement that the c++ application will sit server side and  thus will not be able to use windows data structures as part of the solution.

What i need is to return a container of filepaths that contain a keyword that is passed to the scanning function. So for example:

If the server contains the following files in the root directory:
file123.dat
file1234.dat
file1111.dat
file111122.dat

and i call the scanning function with the keyword "123":
path_array=filescan("123",root_path):

The i would have the complete filepaths for the following files returned in the path_array[]

file123.dat
file1234.dat

Any help would be appreciated.

Thanks
simondopickupAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

flob9Commented:
Instead of passing to "findfirstfile" fullpath + "*.*", give it :

fullpath + "*"+keyword+"*.*"

where fullpath is your current search directory, and keyword "123"

file1234.dat and file123.dat will match pattern *123*.*
0
flob9Commented:
... depending on how your "filescan" function is implemented, this can filter on directory too.
Please show us your current filescan code.
0
simondopickupAuthor Commented:
You have it in the link.

I cant have the solution have at the minute and stick it server side. It wont work. I need to remove all windows headers.
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

flob9Commented:
Just pass your keyword parameter to the WlakTree and HandleFile like this :
#include <windows.h> 
#include <conio.h> 
#include <time.h> 
#include <stdio.h> 
#include <string> 
#include <iostream> 
#include <fstream> 
 
using namespace std;
 
static int FileCount=0;									// Initialise parse counter to 0.
void HandleFile (WIN32_FIND_DATA *file_data, char* Keyword);			// Declarator to handle file function
void WriteToLog (string Message);						// Declarator for the WritetoLog function							
void WalkTree (char *Path, char *Base, char* Keyword);					// Declarator to the WalkTree Function
 
 
int main()											
{ 
char *filebase = "C:\\Temp\\";  
char *filepath = "";
char *keyword = "Plan";
 
char MessageBuffer[100];
char datebuf[9];												// stores the date obtained from _strdate() 
char timebuf[9];												// stores the time obtained from _strtime() 
string CurDate = _strdate(datebuf);								// get current date for log information
string CurTime = _strtime(timebuf);								// get current time for log information
string LogMessage; 
int i;
 
WalkTree(filepath,filebase,keyword);									// Start the process of scanning from the filebase above
 
if (FileCount > 0)
{ 
	i =  sprintf(MessageBuffer, "Total files parsed = %d", FileCount); 
	i =+ sprintf(MessageBuffer + i," %s %s\n", CurDate.c_str(), CurTime.c_str()); 
	LogMessage = MessageBuffer; 
	WriteToLog(LogMessage); 
} 
else
{
	i =  sprintf(MessageBuffer, "No files parsed"); 
	i += sprintf(MessageBuffer + i,"  (%s %s)\n", CurDate.c_str(), CurTime.c_str()); 
	LogMessage = MessageBuffer; 
	WriteToLog(LogMessage); 
}
 
return 0; 
}
 
void WalkTree (char *Path,char *Base, char* Keyword)
{
	WIN32_FIND_DATA FindDat;
	HANDLE FndHnd;
	DWORD dwAtt;
	char acPath[MAX_PATH] ;
	char acBase[MAX_PATH] ;
	 
	printf ( "WalkTree()was called with <%s> <%s>\n",Path,Base);		// Print Output that walktree was called with the path and base
	 
	//if ( lstrcmp(".",Path)==0 ||  lstrcmp("..",Path)==0)							// If last character of path is a '.' then return <--- WHY IS THIS HERE?!?!?!
	//	return;
	 
	if(Base)															// Do if Base is occupied with characters
	sprintf (acPath, "%s\\%s",Base,Path);								// If base path exists then append base and path to acpath buffer
	else
	lstrcpy (acPath, Path);												// Make acPath buffer a copy of Path should Base be empty
	 
	printf ( "path is %s\n",    acPath);								// Print the path to the output										
	lstrcpy (acBase, acPath);											// Make acBase the current path
		
	dwAtt = GetFileAttributes (acPath);									// Get file attributes of the new path
	 
	if ( 0xffffffff == dwAtt)											// If the path can not be found then signal an error and write to log.
	{
	 // Write an error to the log here
	}
	 
	if (FILE_ATTRIBUTE_DIRECTORY & dwAtt)								// If NEW path is a directory
	{
		if  ( '\\'== acPath[lstrlen(acPath)-1])							// If the end 2 characters of acPath are '\\' then
		{lstrcat (acPath, "*.*");}										// append a *.* to the end of the filepath to indicate a path for all files
		else
		{lstrcat(acPath, "\\*.*");}										// or this to ensure filepath for acpath ends in \\*.*
		printf ( "path is now %s\n",acPath);							// print the new file path to the output device.
	}
	 
	FndHnd = FindFirstFile(acPath, &FindDat);							// Now look for the first file in the new file path. 
	 
	if (INVALID_HANDLE_VALUE == FndHnd)									// If no handle is found - or no files at all then throw an error
	{
	 // Write an error to the log here
	printf ("ERROR %d\n",GetLastError());								// Error Printed and returned
	return;
	}
	 
	if ( FILE_ATTRIBUTE_DIRECTORY == FindDat.dwFileAttributes)			// IF the FindFirstFile FINDS a directory check (different from 
	{																	// deciphering if the current path is a directory (above)
		if ( !lstrcmp(FindDat.cFileName,".")==0 &&  !lstrcmp(FindDat.cFileName,"..")==0)
			WalkTree (FindDat.cFileName,acBase,Keyword);							// Pass the filename of the handle to the top of the function to redefine the path for the search
	}
	else
	 HandleFile (&FindDat,Keyword);												// If file found, pass to the handler ( the IF function to examine the name
																		// and to enter into the logs. 
	while ( FindNextFile(FndHnd, &FindDat))
	{
		if ( FILE_ATTRIBUTE_DIRECTORY == FindDat.dwFileAttributes)		// IF the FindNextFile FINDS a directory check (different from
		{				
			if ( lstrcmp(FindDat.cFileName,".")==0 ||  lstrcmp(FindDat.cFileName,"..")==0)
				continue;
			WalkTree (FindDat.cFileName,acBase,Keyword);							// then recurse the walk tree function 						
		}
		else
		  HandleFile (&FindDat,Keyword);										// If FILE_ATTRIBUTE_DIRECTORY is not equal to FileAttributes then you have 
	}																	// found a viable file - so handle it.
	 
	if ( ERROR_NO_MORE_FILES != GetLastError())
	{
	 // error
	}
	FindClose (FndHnd);
}
 
void HandleFile (WIN32_FIND_DATA *file_data, char* Keyword)						// Pass the win32_find_data structure by reference
{																	// to the log if the name viable against the check.
  string keyword = Keyword;											// keyword to check each handle file before opening.
  char datebuf[9];													// stores the date obtained from _strdate() 
  char timebuf[9];													// stores the time obtained from _strtime() 
  char MessageBuffer[100];											// holds the message which is converted into a string
  string LogMessage;												// holds the converted message to be written in the log
  string file_name = file_data->cFileName;
  string CurDate = _strdate(datebuf);								// get current date for log information
  string CurTime = _strtime(timebuf);								// get current time for log information
  int sc,i;															// holds result of search term and character count
 
 
 
		sc=	file_name.find(keyword);								// Check for the correct string 'audio' within the filename			
		if(sc!=-1)													// If sc returned -1, then the string has not been found				
		{ 
		//Add the filename to the vector list and write to log that this has happened
		FileCount++;
		}	// end of if
		else
		{
		string fileprint =file_name;
		i =  sprintf(MessageBuffer, "Following file was not opened: %s", fileprint.c_str());
		i += sprintf(MessageBuffer + i,"  %s %s", CurDate.c_str(), CurTime.c_str());
		i += sprintf(MessageBuffer + i,"  Number of characters are %d\n", i);
		LogMessage = MessageBuffer; 
		WriteToLog(LogMessage); 
		}	// end of else
	return;
	} 
 
void WriteToLog(string Message)						// This a function that writes to the log whenever the WritetoLog function is called
{ 
string UtilityLog  = "C:\\Temp\\AudioParseLog.txt"; 
ofstream WriteFile; 
WriteFile.open(UtilityLog.c_str(), ios::app);		// Open the log file to write the input and append to the end of the file
if(WriteFile.fail())								// If the opening of the parselg fails then throw a message box to the user
{ 
	MessageBox(NULL,"Error writing to AudiParseLog!  Contact Support.", 
	   "ParsingInfo", MB_OK | MB_ICONERROR);
	return;
} 
else
{WriteFile << Message;}
WriteFile.close(); 
return; 
} 

Open in new window

0
AndyAinscowFreelance programmer / ConsultantCommented:
>>>I need to remove all windows headers.

Do you mean you are targeting another operating system eg. UNIX on the server ?
0
simondopickupAuthor Commented:
Do you mean you are targeting another operating system eg. UNIX on the server ?
>>exactly
0
flob9Commented:
findfirstfile and other functions you use are windows specific ... you will need to write it another way from scratch.
0
flob9Commented:
You can use opendir and readdir under linux.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Let_Me_BeCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.