How to select text from various files in a folder

Hello I have several plain text UTF8 files.
I keep them them in diferent folders

Each folder contaINS VARIOUS FILES consisting

a - an exact same unique beginning which I can use as a pointer " CR Grade tab tab CR"
b - a following text of variable length which I do not need,
c - another exact same unique word which I can use as a pointer again " CR Student_ID tab tab CR"
d - the relevant part in need till the end of the file

How can I write a script which will only return a text file with only part D going through the various files within a folder I can choose

Thanks a lot for any help

I’m Trying to extract specific parts of text in various folders. Each folder is within one Main folder.
The text structure is the same in every file so the delimiters to extract the text I need should work for each of them.
Someone kindly helped me with the following script
However the script doesnt compile:

tell application "System Events"
  -- gather lists of files from subfolders
          set textFileLists to POSIX path of (files of every folder of folder "/Users/delta/Documents/Phsi/PhsiWork2" whose name extension is "txt")
end tell

set flattenedFileList to {}
--flatten into a single list
repeat with thisFileSubList in textFileLists
          set flattenedFileList to flattenedFileList & thisFileSubList
end repeat

set extractedTextList to {}
-- loop through and extract relevant text using text item delimiters
repeat with thisTextFile in flattenedFileList
          set fullText to read thisTextFile
          set textParts to tid({input:fullText, delim:return & "Student_ID & tab & tab & return})
          set end of extractedTextList to (last item of textParts)
end repeat

--save to output file
set output to tid({input:extractedTextList, delim:return})

set fp to open for access "/Users/delta/Documents/Phsi/PhsiWork2/" & "Test0.txt" with write permission
write output to fp
close access fp

on tid({input:input, delim:delim})
  -- handler for text items

          set {oldTID, my text item delimiters} to {my text item delimiters, delim}
          if class of input is list then
                    set output to input as text
                    set output to text items of input
          end if
          set my text item delimiters to oldTID
          return output
end tid

this line
set fp to open for access “/Users/delta/Documents/Phsi/PhsiWork2/” & “Test0.txt” with write permission

gives this error: Expected “,” or “}” but found identifier.

How can I fix it?

the error occurs some lines above, there a quote missing after Student_ID
You can even see it in the MacScipter code view :wink:

set textParts to tid({input:fullText, delim:return & "Student_ID & tab & tab & return})

I recommend to work always with HFS paths in AppleScript.
Some apps accept POSIX paths, but all accept HFS paths