I am trying to create a file search program that when encountering a directory will recurse into it.
My question is: how can I tell if the first item found is a file or a directory, and what its name is.
(FindOpen returns a handle only but no name nor attributes?)
I understand how this can be done with FindNext, but I also need to do the same with FindOpen.
Please help.
Haim
FindOpen opens a directory. FindNext retrieves the first and successive entries in that directory. It also returns the file attributes of each entry which you can test to see if it is a directory:
/*
Aurora example program
Shows all of the directory names on root of the C drive
Compile as a CONSOLE target
*/
global sub main()
{
int dir,attrib;
string filename;
dir = FINDOPEN("c:\\*.*");
IF(dir)
{
DO {
filename = FINDNEXT(dir,attrib);
IF( attrib & 0x10) print(filename);
} UNTIL filename = "";
FINDCLOSE( dir );
}
print( "Press Any Key" );
while GetKey() = "";
return 0;
}
0x10 = FILE_ATTRIBUTE_DIRECTORY
And this example recurses through all directories listing files and returning the total number of files on the drive.
//compile as console target.
global sub main()
{
int count;
//recursively print all directories
count = printdir("d:\\");
PRINT( count," Files" );
PRINT( "Press Any Key" );
while GetKey() == "";
}
SUB printdir(string path),int
{
int count = 0;
int dir,attrib;
dstring filename[260];
dstring fullname[260];
count = 0;
dir = FINDOPEN(path + "*.*");
if(dir)
{
do
{
filename = FINDNEXT(dir,attrib);
if len(filename)
{
if attrib & 0x10
{
if(filename <> ".") && (filename <> "..")
{
print( path + "[" + filename + "]" );
fullname = path + filename + "\\";
count = count + printdir(fullname);
}
}
else
{
count++;
print( path + filename );
}
}
//the exit case is when there are no more entries
//in the current directory
}
until filename = "";
FINDCLOSE( dir );
}
return count;
}
Thanks,
lol, I did not know that FINDNEXT will get the first occurence.
How do I look for specific file names such as *.src etc?
Do I have to check for them within the internal [findope..findnext] loop?
Haim
dir = FINDOPEN(path + "*.src");
dir = FINDOPEN(path + "*.src"); will not work if we want to be able to recurse through the directory tree starting at path.
It seems that FINDOPEN as implemented in Aurora looks for a directory and not a file. so any call to findopen should be with "*.*"
Still confused...
Haim
This example uses recursion and seaches for all files with a specific extension:
//compile as console target.
global sub main()
{
int count;
//recursively print all files containing a specific extension.
count = printdir("c:\\","");
PRINT( count," Files" );
PRINT( "Press Any Key" );
while GetKey() == "";
}
SUB printdir(string path,string ext),int
{
int count = 0;
int dir,attrib;
dstring filename[260];
dstring fullname[260];
count = 0;
if(len(ext))
dir = FINDOPEN(path + "*." + ext)
else
dir = FINDOPEN(path + "*.*");
if(dir)
{
do
{
filename = FINDNEXT(dir,attrib);
if len(filename)
{
if attrib & 0x10
{
if(filename <> ".") && (filename <> "..")
{
//print( path + "[" + filename + "]" );
fullname = path + filename + "\\";
count = count + printdir(fullname,"");
count = count + printdir(fullname,"src");
}
}
else if(len(ext))
{
count++;
print( path + filename );
}
}
//the exit case is when there are no more entries
//in the current directory
}
until filename = "";
FINDCLOSE( dir );
}
return count;
}
There is a faster way to do this. I will leave that up to you to figure out ;). HINT: Use two subroutines.
Here is a better way:
//compile as console target.
global sub main()
{
int count;
//recursively print all files containing a specific extension.
count = printdir("c:\\","zip");
PRINT( count," Files" );
PRINT( "Press Any Key" );
while GetKey() == "";
}
SUB printdir(string path,string ext),int
{
int count = 0;
int dir,attrib;
dstring filename[260];
dstring fullname[260];
count += findfiles(path,ext); //print files matching extension.
dir = FINDOPEN(path + "*.*");
if(dir)
{
do
{
filename = FINDNEXT(dir,attrib);
if len(filename)
{
if attrib & 0x10
{
if(filename <> "." && filename <> "..")
{
fullname = path + filename + "\\";
count += printdir(fullname,ext); //recurse directories
}
}
}
//the exit case is when there are no more entries
//in the current directory
}
until filename = "";
FINDCLOSE( dir );
}
return count;
}
SUB findfiles(string path,string ext),int
{
int count = 0;
int dir,attrib;
dstring filename[260];
count = 0;
dir = FINDOPEN(path + "*." + ext);
if(dir)
{
do
{
filename = FINDNEXT(dir,attrib);
if len(filename)
{
count++;
print( path + filename );
}
//the exit case is when there are no more entries
//in the current directory
}
until filename = "";
FINDCLOSE( dir );
}
return count;
}
The second version works. (I did not get correct results with the first one)
I'll study this code.
Thanks to all who responded.
Haim