Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Searching through a directory structure

Is there any way to search through a directory structure and get back all occurances of a particular string? Not just the file name, but the full line of text where the string was found?

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Is there any way to search through a directory structure and get back all occurances of a particular string? Not just the file name, but the full line of text where the string was found?
    :

    Well, you just have to use Win32 API to do that: scan folders, load files, search for text... it is too big to explain it right now, but if you go to MSDN and find info about these babies:

    1. FindFirstFile
    2. FindNextFile
    3. FindClose
    4. CreateFile (or fopen)
    5. CloseHandle (or fclose)
    6. ReadFile (or fgets)
    7. strstr

    If you connect all that you can do it.

  • DariusDarius Member Posts: 1,666
    : Is there any way to search through a directory structure and get back all occurances of a particular string? Not just the file name, but the full line of text where the string was found?
    :

    You can use the ideas of pipes (an idea prevalent in *nix) and you are already done. The grep program does exactly that (well more than that). So, you just run grep with the right commandline and pipe the input to your program.

    If all you want to do is print out the text then you are completely done, just use grep. However, I am assuming (possibly fallaciously) that you want to process the string some way. The simplest way to setup the system for that would be to make a batch file that pretty much called grep with the correct commandline arguments and piped the output of grep into your program. You can call grep from inside your program, as well, with CreateProcess, and you can use Get/SetStdHandle() to redirect the IO.

    Inside your program you would simply act as if the information was being typed in at a console, so you can use scanf,cin, or ReadFile.

    Of course for all this to work you need the grep executable, however it's readily available online for a few (probably many) OSes, including Linux and DOS/Windows. You may already have it, the grep executable I have came with my Borland compiler. If you intend to distribute this program you can include grep along with your executable.

    "Faith alone, won't sustain us anymore."
    -Faith Alone, Bad Religion

Sign In or Register to comment.