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
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
Comment