Jump to content

Performing a Find and Replace within a txt file.


Recommended Posts

Posted

I am putting together a routine which would ask a user what string to replace.

Then select the txt file where the user can browse for the file.

Make changes and save it.

 

There is a place holder (*) within the text file. 

 

This is currently what I have put together, But I not certain how to perform the find and replace.

 

(defun c:FindReplace ( / file )
;;https://www.theswamp.org/index.php?topic=40111.0
    (if (setq file (getfiled "Select Text file To Open" "" "txt" 16))
        (startapp "notepad" file)
    )
;;Need to Ask User What to Replace? (i.e. 123412341234*123123 find (*) then Replace with Users Input (abc) = 123412341234abc123123

;;Once Replaced, Save Txt File.

(princ)
)

 

Hope this makes any sense. Thank you for the direction.

Posted

I think Lee Mac has this somewhere but I can't remember the name of the reference, if it was his website or on a forum.

 

So to search through a text file I think you need to go line by line, this was taken from his code but I didn't keep a reference to it. Open file, loop through each line and then my part, here substitute character code 92 (  \ ) with nothing ( "" ). I use the full code to report variables in a LISP (good for checking the variables are defined in the header row), usually checks a full file in less than a second with code that isn't particularly optimised,.

 

Don't think you need to start notepad, if you switch programs the LISP might not function after

 

  (if (setq f (open file "r"))
     (progn
      (while (setq line (read-line f))

;;Do stuff here
        (setq line (stringsub line "" (strcat (chr 92)(chr 92)))) ; remove \\
;; and of doing stuff

      ) ; end while
      (close f)
    ) ; end progn
  ) ; end if

 

I'm not sure how to replace that line in the text file, not something I have done before, probably for speed to create the LISP I'd make a new text file and save as at the end of the routine to the other file name.

 

Might be enough here to give you something to work from?

Posted

Agree "I'd make a new text file and save as at the end of the routine to the other file name." 

 

You can pop open Notepad as per above but can not call the find and replace function, but may be possible if you use MS Word as Cad can talk to Word. But honestly you have file open in Notepad why not just use it.

 

(setq myxl (vlax-get-or-create-object "Excel.Application"))

(setq myword (vlax-get-or-create-object "Word.Application"))

 

 

Posted

Thanks for the feedback. I can see that would make sense to just perform the find and replace within the notepad.

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...