CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2004
    Posts
    41

    collecting file names

    HI i was hoping some one could tell me how to write some code to collect the file names from a folder and place them into an array or list so that they can be compared
    thanks

  2. #2
    Join Date
    Jul 2004
    Posts
    142

    Re: collecting file names

    Code:
    CStringArray FileNameArray;
    HANDLE hFileFind=NULL;       //handle of file found by FindFile function
    WIN32_FIND_DATA FileFindData;
    int ReturnCode;
    
    hFileFind=FindFirstFile("C:\\MyDirectory\\*.*, &FileFindData);  //note \\ is C syntax for a backslash char
    if(hFileFind==INVALID_HANDLE_VALUE)
       return;
    
    //make sure that only files, and not folders, are put in the list
    if((FileFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==0)
      FileNameArray.Add(FileFindData.cFileName);
    
    while(TRUE){
       ReturnCode = FindNextFile(hFileFind,&FileFindData);
       if(ReturnCode==0){   //0 means no more files or some other error
         FindClose(hFileFind);
         return;
         }
      //make sure that only files, and not folders, are put in the list
      if((FileFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==0)
        FileNameArray.Add(FileFindData.cFileName);
      }

  3. #3
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: collecting file names

    I think Hacker2 has provided some overly complex C# code. The System.IO.Directory.GetFiles method returns a string array containing the fully qualified names of all files in the specified folder:
    Code:
    Dim fileNames As String() = IO.Directory.GetFiles("fully qualified folder name here")
    Note that GetFiles is overloaded and you can also provide a filter to get only certain files.

  4. #4
    Join Date
    Jul 2004
    Posts
    142

    Re: collecting file names

    jmcilhinney is right. I put up an example oft C code.
    I didn't notice the VBNet topic heading : 0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured