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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CODE: Reading in Mutliple Files (even from multiple folders)

Status
Not open for further replies.

Insider1984

Technical User
Feb 15, 2002
132
US
This is a common clip of code I created.
It work in two ways:

pass in a single dir to file_search and the program runs through the dir allowing you to manipulate any file you choose.

pass in a single dir to dir_search and it will first find all the folders inside the dir then inside each folder allow you to manipulate the files anyway you see fit.

This code was created for a MFC C++ Dialog program. Although it should work with any MFC C++ program.

A Few tips:
==========
Change *.* to *.txt to only read in txt files in file_search.
I have a MFC dialog pass in variables to the functions. This allows a gui and open dialog box which people prefer over entering a long dir.

I havn't fully tested the code I'm giving (since I removed all my specifics) but everything should be working. Let me know if not...


///////////Begin Code here
void dir_search(CString dir, CString Flag)
{
////////////////////////////////////////////Variables///////////////////////////////
CString filename;//files inside the current DIR
CStringArray dirs;//array of stored folders inside the current DIR
CFileFind file;//the file crap that is needed

int count = 0; // used to count dirs in the array
////////////////////////////////////////////Variables END////////////////////////////

dir = dir + "\\*.*";//allows us to usedir with a minor adjustment

if (file.FindFile(dir)!=0)
{
while(file.FindNextFile())//Obtain the first file
{
filename = file.GetFilePath();
if (filename.Find('.') == -1)//if file is dir it's added to our list of dirs
{
dirs.Add(filename);
}
}

//because of the way this function works we need to call it one more time before leaving to get last DIR
filename = file.GetFilePath();

if (filename.Find('.') == -1)//if file is dir it's added to our list of dirs
{
dirs.Add(filename);
}
}
while (count != dirs.GetSize())//will call search until all dirs have been read
{
file_search(dirs.GetAt(count), Flag);
count++;
}

}//END dir_search function


void file_search(CString dir, CString Flag)
{
////////////////////////Variables////////////////////
CString filename;//files inside the current DIR
CFileFind file;//the file crap that is needed
/////////////////////END Variables////////////////////
dir = dir + "\\*.*";//this allows us to use the dir passed in to us

if (file.FindFile(dir)!=0)
{
while(file.FindNextFile())//Obtain the first file
{
filename = file.GetFilePath();//full file name with 100% dir
file_input(filename);
}

//because of the way this function works we need to call it one more time before leaving to get last file
filename = file.GetFilePath();//full file name with 100% dir
file_input(filename);
}
}//end file_search function

void file_input(filename)
{

//enter what you want to do here with your file open change the name etc.


}//END file_input function

/////////End Code Here
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top