Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Directory list using API

Status
Not open for further replies.

LoneRanger123

Programmer
Dec 19, 2000
62
GB
Is there any way to use the Windows API to get a directory list? I want to use this so that it hides hidden files.

Failing that does anyone know how to use the GetFileAttributes(PATH) Function? Like what Header file its in, and how do I get the specific parts of the return value, as in the Hidden part.

:)
 
Did you try using the FindFirstFile and FindNextFile API functions with the dwFileAttributes member of WIN32_FIND_DATA structure set to FILE_ATTRIBUTE_NORMAL?

The header file for the GetFileAttributes function is winbase.h (kernel32.lib). You can use it like this:

Code:
     DWORD dwType;
     dwType = GetFileAttributes("c:\\myfile.txt");
     if (dwType & FILE_ATTRIBUTE_HIDDEN)
          Application->MessageBoxA("HIDDEN", "Debug", MB_OK);


 
I didnt understand that first paragraph, but the second is helpful, ty :)

DWORD ImageHandle = SHGetFileInfo(FilePath.c_str(),
0,
&info,
sizeof(info),
SHGFI_ICON |
SHGFI_SHELLICONSIZE | SHGFI_SMALLICON |
SHGFI_SYSICONINDEX);

I use that with a loop through a directory to get the icon for each file, but after a few times (going through a few different Directories) info.iIcon is always set to 4 (the folder icon).
 
I'm not sure how you're looping through the directories, but the code you have seems to work for me (with the exception that the SHGetFileInfo does not return an Image Handle).

BTW, what are you doing with info.iIcon? Are you using the system image list?

Just curious...
 
here it is :)

Code:
//This Scans a given Directory, puts all files into the Dir_Entries Array
//It calls the Direntry::SetExt function to set  the files EXT and whether its a folder or not
//At end it sets the current Directory to the Directory just Scanned
int TSidebar::scandir(char *dirname) {
        DIR *dir;
        struct dirent *ent;
        SHFILEINFO info;
        String FilePath;
        int ct = 0;
          DWORD dwType;


   if ((dir = opendir(dirname)) == NULL) //FAILED TO OPEN DIRECTORY
   {
     MessageDlg(&quot;Could not open Dir!&quot;,mtError,TMsgDlgButtons() << mbOK,0);
          MessageDlg(dirname,mtError,TMsgDlgButtons() << mbOK,0);
   return 0;
   }
   rewinddir(dir);

   Dir_No_Entries = 0; // RESET ENTRY NUMBER

   //Clear Directory List
   while (ct < 200) {
   Dir_Entries[ct].Name = &quot;&quot;;
   Dir_Entries[ct].ext = &quot;&quot;;
   Dir_Entries[ct].isfolder = false;
   ct++;
   }

   while ((ent = readdir(dir)) != NULL){
   if (Dir_No_Entries < 200) {

     FilePath = dirname;
     FilePath = FilePath + ent->d_name;

     dwType = GetFileAttributes(FilePath.c_str());
     if (dwType & FILE_ATTRIBUTE_HIDDEN) { }
     else {

            Dir_Entries[Dir_No_Entries].Name = ent->d_name; //Place name in Array

     Dir_Entries[Dir_No_Entries].SetExt(Dir_Entries[Dir_No_Entries].Name); //Put Extension and see if folder.


     DWORD ImageHandle = SHGetFileInfo(FilePath.c_str(),
                                        0,
                                        &info,
                                        sizeof(info),
                                        SHGFI_ICON |
                                        SHGFI_SHELLICONSIZE | SHGFI_SMALLICON |
                                        SHGFI_SYSICONINDEX);



        if (ImageHandle != 0)
        {
                ImageList1->Handle = ImageHandle;
                ImageList1->ShareImages = true;
        }

                Dir_Entries[Dir_No_Entries].IconIndex = info.iIcon;


        Dir_No_Entries++;                            //Increment array pointer
     }
     }
   }

   if (closedir(dir) != 0)
          MessageDlg(&quot;Could not close Dir!&quot;,mtError,TMsgDlgButtons() << mbOK,0);  //Could not close??

            strcpy(Cur_Dir,dirname);
        return 1;
}


Decipher that if u like :)
 
Hmm ... it looks like it should work. However, SHGetFileInfo may not always return the same ImageListHandle. This is because there are many different System Image Lists. You are setting handle of your ImageList1 to this returned value each time through the loop. When the ImageList Handle is different than before, this may have an adverse affect on the outcome...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top