?
Solved

Program to read filenames in a directory

Posted on 2003-03-12
8
Medium Priority
?
233 Views
Last Modified: 2010-04-01
Hi,

I am trying to write a code (using the vc++ compiler) to read the filenames in any given directory in the windows system and then write the list of filenames in a text file.

Example-
Suppose i have a directory "c:\temp" with the files a.txt,b.dll,c.jpg etc.

i give the program the directory to read as "c:\temp" , then i want my code to read the file names in this directory and then output a file list.txt containing the names of the files ( that is a.txt, b.dll, c.jpg.....)

I guess it will be a small piece of code but i do not know how to do it on windows.

[ i know it can be done on unix using opendir(),readdir() functions but these do not seem to be available in vc++ and do not know the equivalents]

Thanks

0
Comment
Question by:rsalunkhe
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 8

Accepted Solution

by:
akshayxx earned 400 total points
ID: 8124433
This is from a different question...

*******************

You can use the windows API functions FindFirstFile() and FindNextFile().

FindFirstFile() will find the first file in a directory that matches the search string (if there is one) and will fill in a WIN32_FIND_DATA structure with information about the file found.  It will also return a handle to information that windows needs to record so that it can find additional files.   You can then call FindNextFile() to find then next file that matches the search string.  You pass the handle returned from FindFirstFile() to FindNextFile() so it knows what to look for and it will fill in another  WIN32_FIND_DATA structure for the file found.  You then continue calling FindNextFile() until no more files are found.  You can then close the find handle using FindClose().

An example might be

  BOOL  Done;                     // Done searching for files?
  HANDLE  FndHnd = NULL;   // Handle to find data.
  WIN32_FIND_DATA FindDat;  // Info on file found.

  FndHnd = FindFirstFile("C:\\*.txt", &FindDat);
  Done = (FndHnd == NULL);       // Indicate we are done if nothing was found.

  while (!Done)
  {
     cout <<  FindDat.cFileName << endl; // Print name of file found.
     Done = !FindNextFile(FndHnd, &FindDat);
  }
  if (FndHnd)            // If there was anything found, then
     FindClose(FndHnd); // Close the find handle
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8124435
that i found from some Previously asked questions, and it was acceptable to questioner , i hope this works for u also
0
 
LVL 2

Expert Comment

by:DarthNemesis
ID: 8124849
The opendir(), readdir() etc. functions are available in c++ if you include the dirent.h library.

#include <stdio.h>
#include <dirent.h>
#include <vector.h>
#include <string.h>
#include <fstream.h>

int main() {
       DIR *dir_p;
       struct dirent *dir_entry_p;
       dir_p = opendir("c:\\temp\\");
       vector<string> filelist(0);
       while (NULL != (dir_entry_p = readdir(dir_p)))
       {
               if (strcmp(dir_entry_p->d_name,".") && strcmp(dir_entry_p->d_name,".."))
                       filelist.push_back(dir_entry_p->d_name);
       }
       ofstream outfile("list.txt");
       for (int i=0; i<filelist.size(); i++) {
               outfile << filelist[i] << endl;
       }
       outfile.close();
       return 0;
}
0
Technology Partners: 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!

 

Author Comment

by:rsalunkhe
ID: 8125000
Hi,

DarthNemesis , i do not have dirent.h but it does seem ur code will do the trick. Thanks for your feedback.

However akshayxx i used ur code and it worked for me. Thanks for your feedback too.

Just one question.... below is the code i am using with the extra lines of writing to a file. I was just wondering how can i give another location for the detination file list.txt?

If i use     fopen("list.txt","w")     it creates it in the location of the code directory. How do i write the output file to a location C:\Backup ?

fopen("C:\backup\list.txt","w") does NOT do the trick!

-------------------
#include<stdlib.h>
#include<stdio.h>
#include<iostream.h>
#include<windows.h>

main()
{

BOOL  Done;                    // Done searching for files?
 HANDLE  FndHnd = NULL;   // Handle to find data.
 WIN32_FIND_DATA FindDat;  // Info on file found.
 
FILE *f;

 if ((f=fopen("list.txt","w")) == NULL)
    {
        cout<<"Unable to open the file";
        return 0;
    }

 f=fopen("c:\list.txt","w");

 FndHnd = FindFirstFile("E:\\*", &FindDat);
 Done = (FndHnd == NULL);       // Indicate we are done if nothing was found.

 while (!Done)
 {
    cout <<  FindDat.cFileName << endl; // Print name of file found.
     fputs(FindDat.cFileName,f);
     fputs("\n",f);
    Done = !FindNextFile(FndHnd, &FindDat);
 }
 if (FndHnd)        // If there was anything found, then
    FindClose(FndHnd); // Close the find handle

  fclose(f);
     
}

---------------

Thanks,
Ranjit
0
 
LVL 2

Expert Comment

by:DarthNemesis
ID: 8125219
> fopen("C:\backup\list.txt","w") does NOT do the trick!
\ is the escape character, and it has special meaning to a text string. If you want to use a literal \ character, you need to double up on the backslashes like so: fopen("C:\\backup\\list.txt","w").
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8125243
DarthNemesis already answered the '\\' problem .. that will work ..
0
 

Author Comment

by:rsalunkhe
ID: 8125272
Hi,

DarthNemesis , i do not have dirent.h but it does seem ur code will do the trick. Thanks for your feedback.

However akshayxx i used ur code and it worked for me. Thanks for your feedback too.

Just one question.... below is the code i am using with the extra lines of writing to a file. I was just wondering how can i give another location for the detination file list.txt?

If i use     fopen("list.txt","w")     it creates it in the location of the code directory. How do i write the output file to a location C:\Backup ?

fopen("C:\backup\list.txt","w") does NOT do the trick!

-------------------
#include<stdlib.h>
#include<stdio.h>
#include<iostream.h>
#include<windows.h>

main()
{

BOOL  Done;                    // Done searching for files?
 HANDLE  FndHnd = NULL;   // Handle to find data.
 WIN32_FIND_DATA FindDat;  // Info on file found.
 
FILE *f;

 if ((f=fopen("list.txt","w")) == NULL)
    {
        cout<<"Unable to open the file";
        return 0;
    }

 f=fopen("c:\list.txt","w");

 FndHnd = FindFirstFile("E:\\*", &FindDat);
 Done = (FndHnd == NULL);       // Indicate we are done if nothing was found.

 while (!Done)
 {
    cout <<  FindDat.cFileName << endl; // Print name of file found.
     fputs(FindDat.cFileName,f);
     fputs("\n",f);
    Done = !FindNextFile(FndHnd, &FindDat);
 }
 if (FndHnd)        // If there was anything found, then
    FindClose(FndHnd); // Close the find handle

  fclose(f);
     
}

---------------

Thanks,
Ranjit
0
 

Author Comment

by:rsalunkhe
ID: 8125284
thanks guys i appreciate the help.
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

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…
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 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 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.
Suggested Courses

800 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