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

How to write the code to list all of files and directories in a driver

Status
Not open for further replies.

hungnguyen

Programmer
Sep 22, 2000
43
US
Hi all,

I have a question. How to write a code to list all of files and sub-foles in a driver. Like in DOS OS, from C:\ prompt, type DIR, the result will list all files and folders in C driver. How can I implement this code??
Please help.
Thanks
Hung
 
Hi

You have to use the class CFileFind.
The first parameter of the function FindFIle is the root directory of your search. You put what you want but don't forget the double \ if you hardcode the path.

HTH

Thierry
Email: Thierry.Marneffe@swing.be

CFileFind finder;

if ( finder.FindFile( "c:\\*.*"))
{

while ( finder.FindNextFile())
{
if ( finder.IsDirectory())
m_List.AddString( "Dir " + finder.GetFilePath());
else if ( finder.IsDots())
m_List.AddString( "Dots ...");
else if ( finder.IsSystem())
m_List.AddString( "Sys " + finder.GetFilePath());
else
m_List.AddString( "File " + finder.GetFilePath());
}
}
 
use FindFirstFile and FindNextFile John Fill
1c.bmp


ivfmd@mail.md
 
Here is something that u can do w/o using MFC or FindFileFirst
//***** START OF UR PROGRAM *****
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <iostream.h>
//main(int argc, char *argv[]) -- original
int main()
{
char *instruction;
char path[50], filename[15];

//long *instruction;
cout << &quot;Enter Drive or path that you want (ie. c: or c:\\mypath):\t&quot;;
cin >> path;
cout <<&quot;File name (ie. myfile.txt)\t&quot;;
cin >> filename;
cout <<&quot;\n&quot;;
if (path== NULL || filename == NULL)
{
printf(&quot;\nYou miss path or filename\n\n&quot;);
printf(&quot; Drive/Path it is the drive/path in which you want\n&quot;);
printf(&quot;to search for your file or directory.\n&quot;);
printf(&quot; filename : it is the name of the file (or part of\n&quot;);
printf(&quot;the name) or of the directory that you\n&quot;);
printf(&quot;are searching for.\n\n&quot;);
return -1;
}
instruction = (char *)calloc(101, sizeof(char));
strcat(instruction, &quot;dir &quot;);
strcat(instruction, path);
strcat(instruction, &quot;\\&quot;);
strcat(instruction, filename);
strcat(instruction, &quot; /s /l /b > outdir.txt&quot;); // dd
printf(&quot;%s \n&quot;, instruction);
system(instruction);
return 0;
}
//****END OF UR PROGRAM
Let me know if u have any issue
Hope this helps
GH
 
I forgot that the result will be stored in the outdir.txt file. You can display it in the prompt by eliminate the &quot;>outdir.txt &quot; in the line that has comment &quot;//dd&quot;

Hope this helps
GH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top