Jump to content

Open text file and jumping to a line


Steven P

Recommended Posts

good afternoon,

 

I think the answer is probably no for this, but is it possible to open a text file and scroll down to a certain line of text?

 

For example,

I have a text file with project numbers and some details for each (lets call them projects 100, 101, 102, 103 and so on). So the goal would be to have a routine that can open the text file and scroll down to show the details for, say project 150 (or another as chosen by the user).

 

i think you can get a LISP to read the line you want but I want to read it in the actual text file.

Link to comment
Share on other sites

Whats the problem just read each line and when "project" = 100 stop. (close fname)

 

eg 100,BIGAL,September

eg2 100 BIGAL September

 

; thanks to Lee-mac for this defun
; convert csv to a list
; comma is character 44
; space is 32
(defun _csv->lst ( str / pos )
(if (setq pos (vl-string-position 44 str))
(cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2))))
(list str)
)
)

Link to comment
Share on other sites

Thanks.

Roy - that looks promising, I haven't had chance to look into this since I asked but that could be what I want.

 

Similarly BigAl, thanks - Useful I think, again I haven't had time yet to look at this,

 

I'll probably have more questions on this when I get 10 minutes free...

Link to comment
Share on other sites

Thanks for the replies.. now as the office empties for Christmas lunch I have had a chance to look at this again.

 

I got so far with Sendkeys and it looks promising, but the one thing I am struggling with is that when the file opens AutoCAD stops the command until I go back into it and then resumes... so ctrl+F (for 'find;) then toggles AutoCAD OSNAP.

 

Right so how do I keep AutoCAD executing the LISP when a text file opens up?

 

So far my quick code is (and with thanks to another Lee Mac peie of genius)

 

 (startapp "notepad" "c:\\Users\\SP\\Desktop\\LISPTEST.LSP")

 (setq sendkeyscommand "^Ffindtext{enter}")
;; ^F = Find
;;Findtext = text to find
;;{enter} = enter


 (LM:SendKeys sendkeyscommand) ;;^ = ctrl
)


;;Lee Mac Code
(defun LM:sendkeys ( keys / wsh ) ;;note can also be used to run a LISP....
 (setq wsh (vlax-create-object "wscript.shell"))
 (vl-catch-all-apply 'vlax-invoke (list wsh 'sendkeys keys))
 (vlax-release-object wsh)
 (princ)
)

Link to comment
Share on other sites

Hi RonJon, I was just wanting to view the line / text in the file. Indtead of just opening the text file and manually scrolling down to the part I want I was wondering if a routine can do both

Link to comment
Share on other sites

If you go back to my post and just simply read a text file look for some variable = value and stop then just use ALERT. Copy this to command line (Alert "this is a test")

 

; crude version
; (setq lineread etc.... from file

(if (= val variable)
(alert (strcat "Found the line \n" lineread))
)

Link to comment
Share on other sites

Hi BigAl, Many thanks for your suggestion though its not quite what I am looking for.

 

Very useful if I want to read something from a text file and use it within a LISP routine later - and I'll make a note of it because I think it will be usefull another time. I was wondering if it was possible to open the text file, perform a 'find' within the text file and rather than grab the text to use in AutoCAD but to view the text in the file via notepad (or similar).

Link to comment
Share on other sites

The code below works for Notepad++ and Notepad (remove "++" twice).

Note 1: In some languages the title bar name of Notepad is not "Notepad".

Note 2: You have to dial in the delay.

; (NotepadFind "C:\\Downloads\\test 123.txt" "findtext" 300)
(defun NotepadFind (fnm str delay / obj)
 (vl-catch-all-apply
   (function
     (lambda ()
       (setq obj (vlax-get-or-create-object "WScript.Shell"))
       (vlax-invoke obj 'Run (strcat "Notepad++.exe \"" fnm "\""))
       (setvar 'cmdecho 0)
       (command "_.delay" delay) ; Milliseconds. For BricsCAD users: (sleep delay) does not work.
       (setvar 'cmdecho 1)
       (vlax-invoke obj 'AppActivate "Notepad++") ; Title bar name of application.
       (vlax-invoke obj 'SendKeys (strcat "^f" str "{ENTER}{ESC}"))
     )
   )
 )
 (if obj (vlax-release-object obj))
)

Link to comment
Share on other sites

Thanks!!

 

That loos like it works exactly as I want it to, many thanks - and I was just expecting a 'no it can't be done'.

 

 

Right, now to break it and work out and learn from what you did (though maybe do that between Christmas and new year when its very quiet).

Link to comment
Share on other sites

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