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!

chdir() failed after the first attempt, please help!!!

Status
Not open for further replies.

programmer2002

Programmer
Apr 29, 2002
6
SG
Hi gurus,

I adapted codes from one member of this forum for my application with the following function.

I try to search for files with special extension recursively in a particular directory.

In the function, the program should chdir to 'root'. Then scan the folder for files. If the file is a directory, then recursively search for files in the directory.

But when recursively calling the function, the directory is not changed into 'root' (chdir((char *)(LPCTSTR)root);). That means the program all the way searches for the files in the same directory, it never goes down into subdirectories.

Anybody can help me on this? It's really appreciated.

Regards.

int CTutPanel::findTutorials(CTutFile* fileGroup, CString root)
{
CFileFind finder;
CString fileName;
static int count;

//find files in a folder
chdir((char *)(LPCTSTR)root);
BOOL bWorking = finder.FindFile(_T("*.*"));
while (bWorking && (root.CompareNoCase((LPCTSTR)m_path) != 0))
{
bWorking = finder.FindNextFile();
fileName = finder.GetFileName();
if(finder.IsDirectory() || (fileName.Find(_T(".Tut")) == -1)) continue;
if(fileName.Find(_T(".Tut")) != -1){
fileGroup->tutFileName = fileName;
count++;
}
else if(fileName.Find(_T(".ico")) != -1 || fileName.Find(_T(".bmp")) != -1) fileGroup->iconFileName = fileName;
}

//chdir if FindFile returns a directory
BOOL traverse = finder.FindFile(_T("*.*"));
while(traverse)
{
traverse = finder.FindNextFile();
CString tmp = finder.GetFileName();

if(tmp != "." && tmp != "..")
{
if(finder.IsDirectory())
{
CTutFile* temp = new CTutFile();
fileGroup->nextFile = temp;
findTutorials(temp, finder.GetFilePath());
}
}
}
return count;
}
 
Here is the original code that you copied from

WHat i noticed is you are using chdir with get file path and not get file name like I had done. I think this may be the problem... also note that I do a chdir(".."); to go back up to the starting directory incase more then one dir existed.

Matt
========================================================================================================================

Code:
int RenameFiles(char* startingWith, char* renameTo, char* extension)
{
	CFileFind finder;	
	char renameFile[1028];
	char temp[1028];
	CString fileName;
	static int counter;

	sprintf(temp,"%s*.*",startingWith);
	bool bImagesExist = (bool)finder.FindFile(temp);


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

		if(finder.IsDirectory())continue;

		fileName = finder.GetFileName();

		sprintf(renameFile,"%s%d.%s",renameTo,counter++,extension);
		sprintf(temp,"Renaming %s to %s\n",(LPCTSTR)fileName,renameFile);
		cout<<temp;
		cout<<(rename((LPCTSTR) finder.GetFileName(),renameFile) ? &quot;COULD NOT RENAME&quot;:&quot;RENAMED&quot;)<<endl;
	}

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

		if(finder.GetFileName() != &quot;.&quot; && finder.GetFileName() != &quot;..&quot;)
		{
			if(finder.IsDirectory())
			{
				chdir((LPCTSTR)finder.GetFileName());
				RenameFiles(startingWith,renameTo,extension);
			}
		}
	}
	chdir(&quot;..&quot;);

	return counter;
	
}


 
Hi Matt,

Thanks for your help.

I tried both cases, either with file path or file name. I failed because the program cannot change into the subdirectory. My program keeps scanning the same directory recursively.

Any help?

Lijun
 
I had use the same code but make some little modifications in it.It compiles without any errors or warnings and it gives the appropriate result.

using namespace std;

BOOL SearchDirectory(char *szFilename,char *szDirectory)
{
CFileFind finder;
char Directory[_MAX_PATH];
char temp[1028];
CString fileName;
static int counter = 1;
static int match = 0;
static int repeat = 0;
bool bImagesExist;
bool bWorking;
BOOL result;

if(repeat==0)
{
_chdir(szDirectory);
}
repeat++;
sprintf(temp,&quot;%s&quot;,szFilename);
(finder.FindFile(temp)) ?
bImagesExist=true : bImagesExist=false;

while(bImagesExist)
{
(finder.FindNextFile()) ?
bImagesExist=true : bImagesExist=false;

fileName = finder.GetFileName();
sprintf(temp,&quot;Searching %s in %s : %d file(s)searched\n&quot;,(LPCTSTR)fileName,szDirectory,counter++);
cout<<temp;
if(stricmp((LPCTSTR) finder.GetFileName(),szFilename))
result = false;
else
{
match++;
cout<<&quot;FOUND IN &quot;<<_getcwd(Directory,_MAX_PATH)<<endl
<<match<<&quot; file(s) match your request&quot;<<endl<<endl;
result=TRUE;
}
}
(finder.FindFile(&quot;*.*&quot;)) ?
bWorking=true : bWorking=false;
while(bWorking)
{
(finder.FindNextFile()) ?
bWorking=true : bWorking=false;

if(finder.GetFileName() != &quot;.&quot; && finder.GetFileName() != &quot;..&quot;)
{
if(finder.IsDirectory())
{
_chdir((LPCTSTR)finder.GetFileName());
SearchDirectory(szFilename,szDirectory);
}
}
}
_chdir(&quot;..&quot;);
finder.Close();
if( result && counter > 1 )
return TRUE;
else
return FALSE;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
BOOL result;
char fName[30];
char dName[30];
cout <<&quot; //-------------------------FindSomeFiles---------------&quot;<<endl <<endl;
cout <<&quot; Enter the name of the file: &quot;;
cin >>fName;
cout << endl<<&quot; Enter the directory: &quot;;
cin >>dName;
cout << endl;
result = SearchDirectory(fName,dName);
if(! result)
{
cout <<&quot; Search finish without any result.&quot;<<endl;
}
system(&quot;pause&quot;);
return 0;
}
 
in order to make this code compile properly,you need to include the following heade:

#include <iostream>
#include <direct.h>
#include <string.h>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top