Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Finding file names in a folder/directory

Posted on 1998-08-04
14
Medium Priority
?
231 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 680 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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.

704 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