Link to home
Start Free TrialLog in
Avatar of kallekalle
kallekalle

asked on

Finding file names in a folder/directory

Hi experts,
I'm working on an app in which I need to find all the filenames in a directory. How Can I achieve that?
If you don't know that, maybe my problem can be solved anyway if you know how to copy  a folder with all of is content.

Thanks,

KalleKalle
Avatar of tuvi
tuvi
Flag of United States of America image

You just have to read in one file (the name) at a time. And do it recursively for the subdirectories.

BTW, which Operating System environment?
ASKER CERTIFIED SOLUTION
Avatar of agreen
agreen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fkurucz
fkurucz

The implementation depends on the plattform and libraries you are using. For DOS you can use the FindFirst() and FindNext()functions. For Windows there are FindFirstFile() and FindNextFile() and with the MFC (Microsoft Foundation Classes) you can use the CFileFind-class.

There is no simple way to copy a file, like CopyFile(szDst, szSrc). So you have to open the input-file, create the output file and then read and write blocks of data in a loop.

bye
  feri
Also you can use the CFileFind MFC's class.
Avatar of kallekalle

ASKER

I'm working in windows NT, Visual C++ 5.0
fcurucz:
In Win32 API exists function CopyFile, that copies an existing file to a new file.
Then you use FindFirstFile and FindNextFile of Win32 API. At each call, check if the file is a directory or not by using GetFileAttributesEx. Keep track of these directories and recursively go through them with the above steps.
>There is no simple way to copy a file, like CopyFile(szDst, szSrc). So you have to open the input-file, create the output file and then read and write blocks of data in a loop.

There are simple ways to copy one or more files in Win32.
// One file
CopyFile
CopyFileEx
LZCopy
// One or more files
SHFileOperation

How do I "read in one file (the name) at a time". The FindNextFile() function needs a previous call to the FindFile() function. How can I make FindNextFile() not to find the same file every time I call it?
Use wild card characters. "*.*"
HANDLE handle;
WIN32_FIND_DATA fdata;
...

handle = FindFirstFile("*.*", &fdata);
if(handle == INVALID_HANDLE_VALUE)
        return;

printf("FileName - %s\n", fdata.cFileName);

while(FindNextFile(handle, &fdata)) {
   printf("FileName - %s\n", fdata.cFileName);
}

FindClose(handle);
Thanks agreen, that did it!
Thanks to all of you for helping me out!

I don't think that "*.*" work in the FindFile() and FindNextFile() functions.

Again, many thanks,

KalleKalle
Please close the discussion by grading the answer :)
Of course, sorry.