Solved

Finding file names in a folder/directory

Posted on 1998-08-04
14
217 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

807 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