Read Find Write to Text File

Read Find Write to Text File

DGCSCAD
Advisor Advisor
768 Views
6 Replies
Message 1 of 7

Read Find Write to Text File

DGCSCAD
Advisor
Advisor

I'm looking to open a text file, search for a string, then write a line of text to the next line after finding that string. Basically, adding a note to a section of notes. See example below.

 

Example: Find "-Section 2-" and add a line of text under it:

 

-Section 1-

This is a note

This is another note

 

-Section 2-

*add new text here*

This is a note

This is another note

 

-Section 3-

This is a note

This is another note

 

I can open a file and read it, and also write to it, but haven't found or figured out a way to read, find, then write to it. Yet. Any help is always appreciated.

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Accepted solutions (3)
769 Views
6 Replies
Replies (6)
Message 2 of 7

paullimapa
Mentor
Mentor
Accepted solution

What I typically would do in this case is create another text file in the temp folder.

As each line is read from the source file the same lines are written to the text file in the temp folder.

Then when the text is added in the found location, copy the text file from the temp location overwriting the original source file and then delete the temporary text file.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 7

DGCSCAD
Advisor
Advisor
Accepted solution

I had considered something along those lines, but wanted to see if there was a more straight-forward approach without rebuilding the contents. Copilot got it right on the first try (Huzzah!), but uses a list (go figure) instead of a temp file:

 

(defun c:UpdateTextFile ( / filePath searchText insertText contents updatedLines file line)
  ;; Set the file path and target strings
  (setq filePath "C:/temp/sample.txt") ; Change to your file location
  (setq searchText "TargetText")        ; Text to search for
  (setq insertText "New line inserted") ; Text to insert below match

  ;; Open the file and read all lines into a list
  (setq file (open filePath "r"))
  (while (setq line (read-line file))
    (setq contents (append contents (list line)))
  )
  (close file)

  ;; Search and insert new text
  (foreach ln contents
    (setq updatedLines (append updatedLines (list ln)))
    (if (= ln searchText)
      (setq updatedLines (append updatedLines (list insertText)))
    )
  )

  ;; Write the modified lines back to the file
  (setq file (open filePath "w"))
  (foreach ln updatedLines
    (write-line ln file)
  )
  (close file)

  (princ "\nFile update complete.")
  (princ)
)

 

Thank you for the nudge, Paul.

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 4 of 7

Moshe-A
Mentor
Mentor
Accepted solution

@DGCSCAD hi,

 

Here is another method using one file, reading all lines in memory than adding new text line then rewrite same file.

 

enjoy

Moshe

 

(defun c:insert-string-to-file (/ text_to_find text_to_insert fname f rec orgRec^ modRec^)
 (if (and
       (/= (setq text_to_find (getstring t "\nEnter text to find: ")) "")
       (/= (setq text_to_insert (getstring t "\nEnter text to insert: ")) "")
       (setq fname (getfiled "Select txt data file" "" "txt" 8))
       (setq fname (findfile fname))
     )
  (progn
   (if (setq f (open fname "r")) ; open data file
    (progn
     (while (setq rec (read-line f))
      (setq orgRec^ (cons rec orgRec^))
     ); while

     (setq f (close f))
    ); progn
   ); if

   (setq modRec^
	  (mapcar
     	    (function
       	      (lambda (rec)
                (cond
                  ((vl-string-search text_to_find rec)
                   (list rec text_to_insert)
	          ); case
                  ( t
                   (list rec)
	          ); case
	        ); cond
	 
              ); lambda
            ); function
     	    orgRec^
          ); mapcar
   ); setq

   (if (setq f (open fname "w"))
    (progn
     (foreach item (reverse modRec^)
      (foreach rec item
       (write-line rec f)
      )
     ); foreach

     (setq f (close f))
     (prompt (strcat "\nfile \"" fname "\" is update."))
    ); progn
   ); if

  ); progn
 ); if

 (princ)
); c:insert-string-to-file 

 

 

0 Likes
Message 5 of 7

DGCSCAD
Advisor
Advisor

Thanks Moshe!

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 6 of 7

CodeDing
Mentor
Mentor
Message 7 of 7

DGCSCAD
Advisor
Advisor

Thanks for that info CodeDing. It does look like something I could use as a framework for other things down the road. For my purposes though, under the current circumstances, what I've pieced together from above works well.

 

AutoCad 2018 (full)
Win 11 Pro