Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Program to read filenames in a directory

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
rsalunkhe
Asked:
rsalunkhe
  • 3
  • 3
  • 2
1 Solution
 
akshayxxCommented:
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
 
akshayxxCommented:
that i found from some Previously asked questions, and it was acceptable to questioner , i hope this works for u also
0
 
DarthNemesisCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rsalunkheAuthor Commented:
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
 
DarthNemesisCommented:
> 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
 
akshayxxCommented:
DarthNemesis already answered the '\\' problem .. that will work ..
0
 
rsalunkheAuthor Commented:
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
 
rsalunkheAuthor Commented:
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!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now