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

Deep search 1

Status
Not open for further replies.

Leibnitz

Programmer
Joined
Apr 6, 2001
Messages
393
Location
CA
When searching for files and folders in some directorie,how do you scan subfolders as well ?
 
I replied to this in a previous post... if you dont want to use mfc, i have not done it that way but i am sure there is a way to do it with chdir differently they I have

Matt

See Post thread116-169738
 
Well,it does'nt seems to work ?
Dont know why.I have use visual C++ to compile it as an MFC project,no errors but four warnings.
Here is the code with a some very little modifications.

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);
std::cout<<temp;
std::cout<<(rename((LPCTSTR) finder.GetFileName(),renameFile) ? &quot;COULD NOT RENAME&quot;:&quot;RENAMED&quot;)<<
std::endl;
}

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

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

return counter;

}


int main(int argc, char* argv[])
{
RenameFiles(&quot;C:\\Documents and Settings\\Gonzales Cenelia\\Mes documents\\advanced.txt&quot;,&quot;advanced5.txt&quot;,&quot;.txt&quot;);
return 0;
}
 
try this for a call to RenameFiles

SetCurrentDirectory(&quot;C::\\Documents and Settings\\Gonzales Cenelia\\Mes documents&quot;);

RenameFiles(&quot;advanced&quot;,&quot;advanced5&quot;,&quot;txt&quot;);


See if that produces the desired result.


Matt
 
Lets say that it's better than the first time but they are still some problems.

RenameFiles(&quot;advanced&quot;,&quot;advanced5&quot;,&quot;txt&quot;) gives the following result:advanced..50.txt.

i used the following lines:
_chdir(&quot;C:\\Documents and Settings\\Gonzales Cenelia\\Mes documents\\&quot;);
RenameFiles(&quot;advanced&quot;,&quot;advanced5&quot;,&quot;txt&quot;);

because this one:
SetCurrentDirectory(&quot;C::\\Documents and Settings\\Gonzales Cenelia\\Mes documents&quot;);

RenameFiles(&quot;advanced&quot;,&quot;advanced5&quot;,&quot;txt&quot;);

did not give any result.




 
I have finally succeded with some minor changes to the original code.Her it is:

#include &quot;stdafx.h&quot;
void RenameFiles(char* startingWith, char* renameTo)
{
CFileFind finder;
char renameFile[1028];
char temp[1028];
CString fileName;
static int counter;
bool bImagesExist;
bool bWorking;

sprintf(temp,&quot;%s*.*&quot;,startingWith);
(finder.FindFile(temp)) ?
bImagesExist=true : bImagesExist=false;

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

if(finder.IsDirectory())continue;

fileName = finder.GetFileName();

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

(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());
RenameFiles(startingWith,renameTo);
}
}
}
_chdir(&quot;..&quot;);

}


int main(int argc, char* argv[])
{
_chdir(&quot;c:\\&quot;);
RenameFiles(&quot;readme45.rtf&quot;,&quot;readme.txt&quot;);
return 0;
}
 
For people ho are interested here is some code(MFC code) four searching a specific file or folder in a directory or folder.It even search subfolders to.


using namespace std;

void 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;

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(Directory,&quot;%s&quot;,szDirectory);
sprintf(temp,&quot;Searching %s in %s : %d fil(s) searched\n&quot;,(LPCTSTR)fileName,Directory,counter++);
cout<<temp;

if(stricmp((LPCTSTR) finder.GetFileName(),szFilename))
{
cout<<&quot;NOT FOUND IN &quot;<< _getcwd(Directory,_MAX_PATH)<<endl<<endl;
}

else
{
match++;
cout<<&quot;FOUND IN &quot;<<_getcwd(Directory,_MAX_PATH)<<endl
<<match<<&quot; file(s) match your request&quot;<<endl<<endl;
}
}

(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();
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top