Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Assistance appreciated with STATA Program for Searching Do-Files

    Hello,

    I am currently working on my first program in STATA as part of a programming course I am taking. The objective of the program is to search through all folders and subfolders within a given directory for a specific string ("search"). The program successfully identifies all the do-files in the specified directory and processes the first file, but it encounters an issue when it reaches a specific line in the second do-file containing a path.

    The program halts at the following line of code:

    stata
    if regemx(lower(trim("`line'")), lower("`search'")) {

    I am receiving the following error message when the program reaches a line like this:

    stata
    X not found, r(111) (where X is "cd E:/X/y/z" //text in the do-file)

    Despite using the capture command, the program stops executing after encountering this path line. I have trace turned on, which indicates that the issue arises at the line mentioned above.

    I would appreciate any assistance in identifying the cause of this error. Specifically, I am curious why the program stops even with capture in place, and how I can handle this scenario more gracefully.

    I am using: Windows, STATA 18.0

    The program

    clear all
    set trace on
    cap program drop finder
    program define finder
    version 18.0
    syntax, folder(string) search(string)

    tempname dofilelist
    !dir /s /b "`folder'\*.do" > `dofilelist'

    local i = 1
    file open listfile using `dofilelist', read
    file read listfile line
    while r(eof) == 0 {
    local filepath`i' = subinstr("`line'", "", "/", .)
    //display as text "Found file: `filepath`i'"
    local ++i
    file read listfile line
    }
    file close listfile

    local found = 0
    forvalues j = 1/`=`i'-1' {
    local path = "`filepath`j''"

    tempname checkfile
    cap file close `checkfile'
    cap file open `checkfile' using "`path'", read text

    local line_number = 0
    local file_match = 0

    file read `checkfile' line
    while r(eof) == 0 {
    local ++line_number
    if regexm(lower(trim("`line'")), lower("`search'")) {
    if `file_match' == 0 {
    display as text "Match found in: " as result "`path'"
    local file_match = 1
    }
    display as result "`line_number': " as text `"`line'"'
    local found = 1
    }
    cap file read `checkfile' line
    }
    cap file close `checkfile'
    }

    if `found' == 0 {
    display as error "No matches found for '`search'' in the given folder."
    }
    end

    //Excecute the program
    finder, folder(E:/path) search("hello")

    Thank you in advance for your help.

    Kind regards, Dina

  • #2
    The code is lengthy and somewhat complicated, so I have not made a serious attempt to review it all. One thing that strikes me as very problematic in the line that is throwing an error is:
    Code:
    if regexm(lower(trim("`line'")), lower("`search'"))
    will produce a syntax error if the contents of `line' itself contains quotes. And such lines are common in do-files. A similar problem can arise if `search' contains quotes. So I suggest as a start changing it to:
    Code:
    if regexm(lower(trim(`"`line'"')), lower(`"`search'"'))
    and, in fact, replacing "`line'" and "`search'" (and "`anything_at_all'") with the corresponding strings wrapped in `" "' throughout the do-file.

    Also, in future posts here, when displaying code, please be sure to wrap it between code delimiters so that it will show up more readably (in particular, indentation will be preserved.) If you are not familiar with code delmiters, read the Forum FAQ, which provides other excellent advice on ways to maximize your chances of getting a timely and helpful response to your question.

    Comment


    • #3
      Dear Clyde,

      Thank you for your prompt response. That was indeed the issue.

      I will review the FAQ before posting again.

      Wishing you a great day.

      Best regards, Dina

      Comment

      Working...
      X