Solved

Finding file names in a folder/directory

Posted on 1998-08-04
14
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

624 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