Solved

Finding file names in a folder/directory

Posted on 1998-08-04
14
192 Views
Last Modified: 2010-04-10
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
0
Comment
Question by:kallekalle
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 5

Expert Comment

by:tuvi
ID: 1169313
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?
0
 
LVL 4

Accepted Solution

by:
agreen earned 170 total points
ID: 1169314
You need two functions FindFirstFile and FindNextFile.
0
 
LVL 1

Expert Comment

by:fkurucz
ID: 1169315
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
0
 
LVL 4

Expert Comment

by:agreen
ID: 1169316
Also you can use the CFileFind MFC's class.
0
 

Author Comment

by:kallekalle
ID: 1169317
I'm working in windows NT, Visual C++ 5.0
0
 
LVL 4

Expert Comment

by:agreen
ID: 1169318
fcurucz:
In Win32 API exists function CopyFile, that copies an existing file to a new file.
0
 
LVL 5

Expert Comment

by:tuvi
ID: 1169319
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.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 23

Expert Comment

by:chensu
ID: 1169320
>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
0
 

Author Comment

by:kallekalle
ID: 1169321

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?
0
 
LVL 5

Expert Comment

by:tuvi
ID: 1169322
Use wild card characters. "*.*"
0
 
LVL 4

Expert Comment

by:agreen
ID: 1169323
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);
0
 

Author Comment

by:kallekalle
ID: 1169324
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
0
 
LVL 4

Expert Comment

by:agreen
ID: 1169325
Please close the discussion by grading the answer :)
0
 

Author Comment

by:kallekalle
ID: 1169326
Of course, sorry.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now