I am trying to search for files in Directories.
like, i have 10 directories..... and consider directory 1, which has 2 files..
this is same with all the files...
i want to get a string from each of these files from 10 directories... to be written to a seperate file, in C++.
I have been trying to do this.... any help will be greatly appreciated.
Thank you

Recommended Answers

All 15 Replies

Can you get a string from one file? If so then can you get a string from more than one file in the same directory? Do you have to search for the files according to some pattern, or do you have a list of filenames? I see this question a lot, and nobody gives enough detail. I need to know what you already know how to do. If you don't know how to open and read a file then my answering the question assuming your problem is with reading from multiple directories wouldn't help much. If you don't know how to open and read from different directories then I need to know what operating system and compiler you use because there's no portable solution for that problem.

Yes, i can get the string i want from one file, and from multiple files in one directory.
i have to search for the files, based on one String, and then get the data from those files.

i worked with files before, but no with searching for files in directories and arranging them in an array. :sad:
coz, there is parent directory, which has subfolders.these subfolders has files.
the OS is Winxp, VC++ compiler.

In that case the Win32 API would probably be your easiest solution. You can start browsing from here.

how about this function... any further help plzz

private void GetFileInDirectory()
{
    textfile = new string[200];
    try
    {
        string [] files;
        string path = currentPictureFile.Substring(0, currentPictureFile.LastIndexOf("\\") + 1);
        
        if ( Directory.Exists(path) )
        {
            files = Directory.GetFiles(path);
            int i = 0;
            foreach ( string fileName in files )
            {
                int lastIndex = fileName.LastIndexOf(".");
                if ( fileName.Length - lastIndex - 1 == 3 )
                {
                    string ext = fileName.Substring(lastIndex + 1, 3).ToUpper();
                    if ( (ext.Equals("txt" ) ) 
                    {
                        textfile[i++] = fileName;
                    }
                }
                
            }
            
        }
    }
	
	catch (IOException ioE)
	{
        MessageBox.Show("Error - wrong path");
	}
	
	catch (UnauthorizedAccessException unE)
	{
        
	}
}

Uh, that's not C++, it's Java.

yeah... right...

i was trying to do it in C++... just found this piece of code from my frnd...

I have never worked with file search and directory structure :sad:

am very Stuck at this point... it wont let me further... :confused:

any help will be appreciated....

Thnx alotttttttttt in advance

In another forum I posted a link to an FAQ: "Accessing a directory and all the files within it". Did you follow the link?

yes sir, i did that,...... but that doesnt work with VC++ :sad:

i tried those... plz help

Another option if MSDN scares you is to download the Boost::filesystem library. If you can figure out how to build it and link with it then writing your program would be a breeze. :) But I still recommend using the Win32 API, and my link is a great start.

yes sir, i did that,...... but that doesnt work with VC++ :sad:

i tried those... plz help

Hmm. The last one I copied and pasted and compiled with no mods using VC6...

--------------------Configuration: testpp - Win32 Debug--------------------
Compiling...
testpp.cpp
Linking...

testpp.exe - 0 error(s), 0 warning(s)

Seems just fine to me.

Plz let me know ... which code you compiled... i will be grateful... if you reply soon.. thnx a lott

>The last one I [...]

>The last one I [...]

the Last one u mean ... the Getdirectory function... did u compile it in VC++ ?? am lost .. sorry

The following code is from the msdn library, slightly modified and tested with Dev-C++

// create a list of files in a given directory
// http://msdn.microsoft.com/library/en-us/fileio/fs/listing_the_files_in_a_directory.asp
// tested with Dev-C++

#define _WIN32_WINNT 0x0501

#include <windows.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind = INVALID_HANDLE_VALUE;
   char DirSpec[MAX_PATH];
   DWORD dwError;

   argv[1] = "C:\\";   // for testing only
   
   printf ("A list of files and subfolders in folder %s\n", argv[1]);
   strncpy (DirSpec, argv[1], strlen(argv[1])+1);
   strncat (DirSpec, "\\*", 3);

   hFind = FindFirstFile(DirSpec, &FindFileData);

   if (hFind == INVALID_HANDLE_VALUE) 
   {
      printf ("Invalid file handle. Error is %u\n", GetLastError());
      return (-1);
   } 
   else 
   {
      printf ("%s\n", FindFileData.cFileName);
      while (FindNextFile(hFind, &FindFileData) != 0) 
      {
         printf ("%s\n", FindFileData.cFileName);
      }
    
      dwError = GetLastError();
      FindClose(hFind);
      if (dwError != ERROR_NO_MORE_FILES) 
      {
         printf ("FindNextFile error. Error is %u\n", dwError);
         return (-1);
      }
   }
   
   getchar();  // wait
   return (0);
}

You will have to work on this, separate the files from the subfolders and put them into an array or vector.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.