Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How to read file names in a directory as string? (http://www.go4expert.com/forums/read-file-names-directory-string-t26215/)

ahrouhi 6Jul2011 09:10

How to read file names in a directory as string?
 
Hi everybody
I am not expert in C, and i want to do somethings by help of c programming i hope you can help me.
I have a directory in my hard disk which containd thousands of video files. I want to extract the file names one by one and create a folder somewhere else and copy the file in that folder. Can you guide me how to do it in C language?:worried:

xpi0t0s 6Jul2011 12:28

Re: How to read file names in a directory as string?
 
There's nothing in C itself; you will have to use a platform-specific system call. Trouble is, you haven't said what platform.........

ahrouhi 7Jul2011 05:56

Re: How to read file names in a directory as string?
 
I am on windows platform. One solution that came in my mind is using "system" function in C to ask OS to do my request as can be seen below:
int result;
result = system("dir c:\\video > c:\\video\\dirlist.txt");
FILE *fp;
if((fp=fopen("c:\\video\\dirlist.txt", "r"))==NULL)
...
By parsing the content of txt file i can achieve my request. But C has other functions such as exec, fork and popen. I am looking for the most efficient one. I should do the following scenario:
1-pars the .txt file content and get the video file name.
2- run an exe file (a program that extract pictures from video) over the video file and send the resulted images in a directory.
3- process the images in the directory.
The above process should be in a loop and iterated for more that 1000 times. So the compiler should wait to finish each part of process and then continue next step.
I hope i have explained the problem well. Whats your advice?

xpi0t0s 7Jul2011 13:34

Re: How to read file names in a directory as string?
 
Well starting a new process to read a directory and write it to a file, then to read that file, is definitely not the most efficient approach. If you're prepared to put up with that overhead then it really doesn't matter which variant on system you use.

Anyway if you're on Windows you can use the WinAPI functions for parsing a directory, but they're tricky to figure out and even I just copy stuff from MSDN. Here's a function from one of my projects:
Code:

void recurseDir(const char *dir,int recfunc)
{
        WIN32_FIND_DATA FindFileData;
        HANDLE hFind;
        int bFin=0;
        char startDir[1024];
        strcpy_s(startDir,1024,dir);
        strcat_s(startDir,1024,"\\*.*");
        hFind=FindFirstFile(startDir,&FindFileData);
        while (!bFin)
        {
                if (hFind==INVALID_HANDLE_VALUE)
                        bFin=1;
                else
                {
                        if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                        {
                                // skip . and ..
                                if (strcmp(FindFileData.cFileName,".") && strcmp(FindFileData.cFileName,".."))
                                {
                                        char loc[1024];
                                        strcpy_s(loc,1024,dir);
                                        strcat_s(loc,1024,"\\");
                                        strcat_s(loc,1024,FindFileData.cFileName);
                                        if (recurseFuncData.skipSkipDirs && skipDir(FindFileData.cFileName,loc))
                                        {
                                                //printf("Skip dir '%s'\n",loc);
                                        }
                                        else
                                        {
                                                //printf("Recurse into directory '%s'\n",loc);
                                                recurseFuncData.dirCount++;
                                                recurseDir(loc,recfunc);
                                        }
                                }
                        }
                        else
                        {
                                //printf("Found file '%s'\n",FindFileData.cFileName);
                                //recurseFuncData.fileCount++;
                                strcpy_s(recurseFuncData.fromDir,1024,dir);
                                if (!recurseFunc(recfunc,&FindFileData))
                                        bFin=1;
                        }
                }
                if (!FindNextFile(hFind,&FindFileData))
                        bFin=1;
        }
        FindClose(hFind);
}

This won't compile as is - you don't have the recurseFuncData structure or the skipDir() and recurseFunc() functions. I've left them in so you can see how it works and you'll need to at least comment them out to make it work. If you don't want to recurse over a whole directory tree then just remove the if (strcmp...) block, leaving the FILE_ATTRIBUTE_DIRECTORY block empty (or negate the test and move the else block into the if block).

ahrouhi 8Jul2011 12:03

Re: How to read file names in a directory as string?
 
Thanks for your advice. The story is that i should use a video processing open source program called "ffmpe". it is a command line program. I want to run ffmpeg for more than one thousand time by "system" command in C in windows platform.
the model of calling ffmpeg is like below. IndexFunction is a very complicated and memory consumptive function.
while(){
system("d:\\ffmpeg -i ..........);
IndexFunction();
}
Do you think it will work? Do you have any suggestion and advice? Would you let me know how can i free the memory used by ffmpeg after each run?

xpi0t0s 8Jul2011 19:42

Re: How to read file names in a directory as string?
 
Well if you can construct programatically commands like "d:\\ffmpeg -i ..." and they work from the command line (which would be the place to test them) then there should be no problem launching them from system().

What makes you think the memory used by ffmpeg won't be freed?

ManzZup 8Jul2011 21:17

Re: How to read file names in a directory as string?
 
if you are going though all the 1000 files looping a ffmpeg, you'll run into memory i think
becuase somtimes ffmoeg has that bad habbit of locking files and not finishing the process when there are multiple instances
i dont know why, but better manage the code carefully

xpi0t0s 9Jul2011 00:43

Re: How to read file names in a directory as string?
 
Yeah but you would only hit that if you ran multiple system() calls simultaneously via multithreading, not sequentially in a loop like
Code:

for (i=0; i<1000; i++)
  system("ffmpeg...");

Locks and allocated memory are freed when the process exits, so this is a leak:
Code:

int main()
{
  char *foo=malloc(10000000);
  foo="";

only until this point:
Code:

  return 0;
}

Memory leaks and locked resources are only an issue when processes insist on not exiting.

ahrouhi 9Jul2011 14:23

Re: How to read file names in a directory as string?
 
thanks for giving advices. But i still need to know that when i run the below code:
Quote:

while(){
system("d:\\ffmpeg -i ..........);
IndexFunction();
}
when exe file is running and pass the stream process to the ffmpeg through "system" command, naturally then call "IndexFunction" and continue the program process to perform the function lines of codes. But i need a halt for finishing ffmpeg command then after finishing this process stream , the main program process continues and goes to "IndexFunction". I want something like interpreter here, step by step performing requestes. and after finishing one process the next step process start.
for this purpose what should i do in C?

xpi0t0s 11Jul2011 02:33

Re: How to read file names in a directory as string?
 
I've no idea what you're talking about. What doesn't the code you posted do?


All times are GMT +5.5. The time now is 10:36.