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

How can I read data from all files in certain directory?

Status
Not open for further replies.

Insider1984

Technical User
Feb 15, 2002
132
US
Basically I want to find all cases of a *.rcs file and read in some data. I want this to happen up to 4 level deep(the program would need to look for directories inside the selected directory and also find .rcs files)

I know the basics in Visual Studio 6 of how to read in files one at a time (knowing the file name or selecting from dialog) but I do not know how to read in all files in a directory (and for that matter swtich to a lower directory and continue searching).


Please help me out.
 
Try FindFirstFile, FindNextFile and FindClose API calls. This is documented in the MSDN Library. These functions also return the directories, which you can process recursively.

Marcel
 
See this thread. In it I talked about removing the read only bit from each file, a simple modification and you can do what you need.

Matt

thread116-226192
 
Thank you.


My trouble was not knowing which class members to use.

I'll check it out.

Thanks
 
OKay thanks for the link.. I cut what code I think I'll need:

I currently pass in the directory name of which they want to start looking:

CString dir = "C:\WIndows\BITMAPS\";

Now I only want to read .rcs files (so I changed that below also)

I'm a very basic programmer so I can't follow what the variables are. I want to use fopen and fclose to read the files once (your) search function finds a valid file. Can you help me identify where you grabed a file name? (this way I can figure out where to insert my code to read in the file).


//CODE HERE//////////////////////////////////////////
int RemoveReadOnlyBit()//I'll change the function name later
{
CFileFind finder;
CString fileName;

bool bImagesExist = (bool)finder.FindFile("*.rcs");


while(bImagesExist)
{
bImagesExist = finder.FindNextFile();

if(finder.IsDirectory())continue;

fileName = finder.GetFileName();
/*I assume this is checking the single bit locking the file
CFileStatus fs;
CFile::GetStatus(fileName,fs);

fs.m_mtime=0;
fs.m_attribute &= ~(CFile::readOnly);

CFile::SetStatus(fileName,fs);

cout<<&quot;Removed read only bit on &quot;<<(LPCTSTR)fileName<<endl;
*/ And this is the end of the bit changing (where I can start with reading in the file?
}

bool bWorking = finder.FindFile(&quot;*.rcs&quot;);
while(bWorking)
{
bWorking = finder.FindNextFile();

if(finder.GetFileName() != &quot;.&quot; && finder.GetFileName() != &quot;..&quot;)
{
if(finder.IsDirectory())
{
chdir((LPCTSTR)finder.GetFileName());
RemoveReadOnlyBit();
}
}
}
chdir(&quot;..&quot;);
return 0;
}
 
Okay I'm back. I'm still trying to read what is going on here (since I'm a C programmer recently converted to Visual C... I assume much of this is MFC stuff. Here is what I come up with:

//CODE///////////////////////

int filefind()
{
CFileFind finder;
CString fileName;

bool bImagesExist = (bool)finder.FindFile(&quot;*.rc&quot;);


while(bImagesExist)
{
bImagesExist = finder.FindNextFile();

if(finder.IsDirectory())continue;

fileName = finder.GetFileName();

ProductVersionDlg::process(filename);//this is where I would open read and close file????

}

bool bWorking = finder.FindFile(&quot;*.rc&quot;);
while(bWorking)
{
bWorking = finder.FindNextFile();

if(finder.GetFileName() != &quot;.&quot; && finder.GetFileName() != &quot;..&quot;)
{
if(finder.IsDirectory())
{
chdir((LPCTSTR)finder.GetFileName());
filefind();
}
}
}
chdir(&quot;..&quot;);
return 0;
}


//END OF CODE///////////////////////////

Basically I just got rid of what I thought was you setting a bit and placed my process function inside assuming the variable &quot;filename&quot; is a file found with the extension .rcs.

I assume I have issues here so if someone can help me out.... I would really be thankful. Which variable stores the CString location of where in the dir structure you start.


Thank you again!
 
HI still looking for help on this issue. I can't seem to figure out the fuctions.

If anyone could step me through a loop that can gather file names (I can even search for the extension manually since it is not a time issue) AND (the hard part in my eyes) follow down into depper directories.

For example:

If I have:

C:\Testnamesand inside a have a bunch of folders

C:\Testnames\Test1C:\Testnames\Test2etc etc

basically I want to be able to grab files from a directory and then do what I want with them.

Also if it matters the user will need to select the directory to start.

Perhaps a basic structure to get me started with comments would be all I need.

Thanks again for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top