Solved

Finding file names in a folder/directory

Posted on 1998-08-04
14
211 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 149
Focus not getting shifted out of  editbox 2 63
getting a fire fox page (view page source ) in c++ 6 42
Problem with SqlConnection 4 169
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

773 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