NO

Author Topic: Get specific filenames out of one folder  (Read 5305 times)

pitter2206

  • Guest
Get specific filenames out of one folder
« on: August 05, 2012, 09:50:29 PM »
Hallo together,

I want to read out all names of files with *.txt of one folder. These names have to be saved in an array so that I can use this array to work with.
Can someone please help me with this array?

Thanks in advance.

CommonTater

  • Guest
Re: Get specific filenames out of one folder
« Reply #1 on: August 05, 2012, 11:33:24 PM »
Hallo together,

I want to read out all names of files with *.txt of one folder. These names have to be saved in an array so that I can use this array to work with.
Can someone please help me with this array?

Thanks in advance.
Every operating system handles file directories a little differently so C doesn't really include much to help you with this.  However; the Windows API has just what you need and more...

Study the MSDN information for SetCurrentDirectory(), FindFirstFile(), FindNextFile() and FindClose()  also take a look at the struct WIN32_FIND_DATA ... you will need to understand all these to make this work.
 
PellesC also has a group of non-standard C functions you can use.  For these you need to look at the dirent.h header in the help file. 
(help -> Contents -> Private #include files -> Dirent.h)
 
I would suggest you learn the Windows way because it is faster and provides more information (File size, attributes, last modification date, creation date, and more).
 
The basic sequence is ... 
  • Create an instance of WIN32_FIND_DATA.
  • Create a HANDLE to hold your search handle.
  • Set your current directory to the folder that holds the files.
  • Call FindFirstFile() to initiate your directory search.
  • Begin a do/while loop in which you deal with your files.
  • Do what you need to do with your file....
  • Loop back to step 5 repeatedly calling FindNextFile()
  • When done call FindClose() to destroy the search handle.
Ok that sounds really complicated but it isn't...
Here's an example that prints mp3 filenames on the screen from d:\myfolder\music
 
Code: [Select]
#define WIN32_DAFAULT_LIBS          // automatically find libraries
#include <stdio.h>
#include <windows.h>
 
// entry point
int main (void)
  {
    WIN32_FIND_DATA fd;  // directory data
    HANDLE fh;           // find handle
 
    // set the working directory
    SetCurrentDirectory("D:\\MyFolder\\Music");
 
    // start the search
    fh = FindFirstFile("*.mp3",&fd)
 
    // deal with the results
    do
       {
          printf("%s\n",fd.cFileName);   // here is where you process the files
        }
    while (FindNextFile(fh,&fd) != 0);   // exits when no more files
 
    // close the find handle
    FindClose(fh);
 
    return 0;
  }

 For the most part it's better to deal with each file separately on the fly, inside the do/while loop than to try to harvest a linked list or array of filenames (the former is complicate, the latter has to be of a fixed size).  By working on the fly, you can call a function, put extra code in there whatever you need. 


Hope that helps.
 
 
 

 

pitter2206

  • Guest
Re: Get specific filenames out of one folder
« Reply #2 on: August 06, 2012, 06:06:40 PM »
Great thanks to you CommonTater!
Your description is perfect for me and I´ll give it a try.

Greets

CommonTater

  • Guest
Re: Get specific filenames out of one folder
« Reply #3 on: August 06, 2012, 07:03:22 PM »
Great thanks to you CommonTater!
Your description is perfect for me and I´ll give it a try.

No worries... just be sure to do your homework at MSDN first.


Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Get specific filenames out of one folder
« Reply #4 on: August 06, 2012, 08:52:22 PM »
Great thanks to you CommonTater!
Your description is perfect for me and I´ll give it a try.

No worries... just be sure to do your homework at MSDN first.
Maybe that's the keyword here?  ;)

Ralf  8)

CommonTater

  • Guest
Re: Get specific filenames out of one folder
« Reply #5 on: August 06, 2012, 09:31:46 PM »
Great thanks to you CommonTater!
Your description is perfect for me and I´ll give it a try.

No worries... just be sure to do your homework at MSDN first.
Maybe that's the keyword here?  ;)

Ralf  8)

Maybe so... but it's not an easy concept for beginners.  The first time I tried to do directory lists under Windows API half my hair turned gray, I went through most of a case of beer and we never did see the neighbor's cat again...  ??? 

pitter2206

  • Guest
Re: Get specific filenames out of one folder
« Reply #6 on: August 07, 2012, 08:13:16 PM »
Yes... I understood your broad hint and kick  ;)

...and of course, I know that these basics are very important.



CommonTater

  • Guest
Re: Get specific filenames out of one folder
« Reply #7 on: August 07, 2012, 08:49:00 PM »
Yes... I understood your broad hint and kick  ;)

...and of course, I know that these basics are very important.

The real deal is not to know this stuff, it is to understand it.  I never try to memorize any of the 30,000 plus functions in the Windows API  (disclosed on MSDN).  Instead I work on understanding the underlying principles (handles, info structs, loops etc.)  and I make a point of knowing how to look the actuall functions up when I need to use them.  It should not be any surprise that I had MSDN open the whole time I was writing my first reply to you.

I trust you got it working?

pitter2206

  • Guest
Re: Get specific filenames out of one folder
« Reply #8 on: August 07, 2012, 10:10:49 PM »
Hihi... I know what you want to say.  ;D

My script doesn´t work yet, but I think I am headed in the right direction...  ;)

The really tricky thing for me was, that the script has to run on WinCE 5....

Code: [Select]
snip...
            wcscpy (sDirectory, L"\\Storage Card\\test\\*.exe\0");
            hSearch = FindFirstFile(sDirectory, &FindFileData);
            if (hSearch){
                wcscpy(sFileName,FindFileData.cFileName );
                if (wcscmp(sFileName, L"Menu") != 0) { index = SendDlgItemMessage(hwndDlg, ID_BOX, LB_ADDSTRING, 0, (LPARAM)sFileName);}
            }
            else    {return 0;    }

            while (FindNextFile(hSearch, &FindFileData) != 0){
                wcscpy(sFileName,FindFileData.cFileName );
                if (wcscmp(sFileName, L"Menu") != 0) {index = SendDlgItemMessage(hwndDlg, ID_BOX, LB_ADDSTRING, 0, (LPARAM)sFileName);}
            }
        return TRUE;

snap..




CommonTater

  • Guest
Re: Get specific filenames out of one folder
« Reply #9 on: August 07, 2012, 10:36:27 PM »
Hihi... I know what you want to say.  ;D

My script doesn´t work yet, but I think I am headed in the right direction...  ;)

Well yes you are heading in the right direction but a simple while loop is not the way to do it.  Take a closer look at my example, track it line by line through a half dozen iterations of the loop... you'll see why I used do/while instead.  You can still test hsearch for errors, in fact that's a good idea... but what you should do is bracket it around the do/while construct so the loop only happens if you get a valid search handle.
 
Also because the returned filenames have no path information you are much smarter to set the current directory to your work folder... This way you can use GetFullPathName() to expand the filename to the correct path.

Although your snippet doesn't show it, I'm guessing you have this in a switch() chain... it would be better to re-write it as a function and call the function from the switch().  When writing message handlers I seldom --prefereably never-- put code in a switch(), I write functions and call them from it.  The reasoning is two fold... 1) It compartmentalizes your code, allowing you to re-write even replace entire functions with little or no consequence to the message tosser, 2) It makes troublshooting a misbehaving switch() about 100 times easier.

Hope that helps.
« Last Edit: August 07, 2012, 10:40:59 PM by CommonTater »