read through files of a directory name passed as an argument

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mmajumdar
    New Member
    • May 2014
    • 2

    read through files of a directory name passed as an argument

    can you help me a to write a code using vs studio 8 to read a diretory name as argument and then open the directory and read each file one at a time and find the match for a string passed as an nother argument in the lines of each file. When it matches create a file and write in that output file the file name whetre the match found, the line number and line which matched.

    My vis does not have dirent.h. so please use another ways to read directory call witha system acll and then then extract the fienames in a file(use some primitive way to open the the directory)
    I cant do this parts
    (1) read a directory
    (2) create a file with only file names
    (3) read file names ans open the files one at a time till the end of the file reached
  • divideby0
    New Member
    • May 2012
    • 131

    #2
    If you're using VS, then you should be able to make use of CreateFile, FindFirstFile and FindNextFile for processing directories and files. The links have examples for doing what you're trying to do.

    The only other thing I can think of is to build a command string for DIR, and pass the arugments using system. You can dump the result to a file instead of the console window, which you could then open, read, and process as needed.

    Mill over that stuff and you should be able to post some code if you need additional help. Good luck

    Comment

    • mmajumdar
      New Member
      • May 2014
      • 2

      #3
      Somehow I am unable to make that command string for dir command which will dump just the file names to an output file.

      system("dir > outfile")It will dump everything what is found in the directory with size, dates and everything along with file name.

      what code will do that or what code to extract the file name from the outfile and open it one by one to read it.

      findfirst file and find nextfile need dirent.h which visual studio does not have. I have version 8 and i am in a place I am unable to install. Can you provide me an ascii text file dirent.h or probably dir.h I can include in my code to have those dir command find first and find second can work
      Last edited by mmajumdar; May 3 '14, 02:52 AM. Reason: clear thought process

      Comment

      • divideby0
        New Member
        • May 2012
        • 131

        #4
        With VS, you should be able to include windows.h and link kernel32.lib to your application for FindFirstFile, FindNextFile, etc.

        Code:
        #include <windows.h>
        
        #pragma comment(lib, "kernel32")
        
        // program code
        For the other, I cannot recall if you mentioned whether or not you were using c or c++, but you can build the dir command using sprintf or sprintf_s

        Code:
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        
        int main(int argc, char *argv[])
        {
           char cmd[200] = { 0 };
        
           // make sure to check argc for
           // expected number of cmd line args
        
           sprintf(cmd, "dir %s > outfile", argv[1]);
           // if arg 1 was the full path
        
           system(cmd);
           // other bits of code
        }
        To process the outfile using default dir output, you'll need to use the spaces as a deliminator. strtok will be useful

        Code:
        char buffer[200];
        while(fgets(buffer, sizeof buffer, file_handle) != NULL)
        {
           char *p = strtok(buffer, " ");
        
           while(p)
           { 
              // p has each word of current buffer
              // test and process p
        
              p = strtok(NULL, " ");
           }
        
           // other code
        }
        Hope that helps.

        Comment

        Working...