Jump to content

open file and close it immediately


Manuel_Kunde

Recommended Posts

Hi, I need a small lisp that opens and closes a file for me in the background. Nothing needs to be done.

In this lisp the file is opened but not closed.

 

(defun c:open_close_explorer ()
  
  (setq Path "Y:\\Folder\\Data\\Test.txt")
  (setq File (StartAPP "Explorer.Exe" Path))
        
        (close File)
  
  (princ)

)

 

Does anyone have an idea?

Link to comment
Share on other sites

When executing the startapp function, the instance of explorer runs within a separate asynchronous process from the evaluation of the AutoLISP program, and, as such, you would need to use a function to interface with the set of running Windows processes, identify the instance of explorer (or more likely, Windows Notepad, though the program used to open the file would ultimately depend on the .txt file association configured on the user's PC), and terminate such process. However, the whole thing seems rather pointless.

  • Like 1
Link to comment
Share on other sites

15 hours ago, Manuel_Kunde said:

"opens and closes a file for me in the background. Nothing needs to be done."

 

Could you just use the read function of open? Don't really know why you would want to open a file and then immediately close it.

 

(defun C:foo (/ F line txt)
  (setq F (open Y:\\Folder\\Data\\Test.txt "r"))  ;opens file to read
  (while (setq line (read-line F))
    (if (not (null line))
      (setq txt (append txt (list line))) ;adds each line of txt file to list txt
    )  ;end if
  )    ;end while
  (close F)
  (foreach ln txt
    (prompt (strcat ln "\n"))
  )
  (princ)
)

 

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

2 hours ago, mhupp said:

 

Could you just use the read function of open? Don't really know why you would want to open a file and then immediately close it.

 


(defun C:foo (/ F line txt)
  (setq F (open Y:\\Folder\\Data\\Test.txt "r"))  ;opens file to read
  (while (setq line (read-line F))
    (if (not (null line))
      (setq txt (append txt (list line))) ;adds each line of txt file to list txt
    )  ;end if
  )    ;end while
  (close F)
  (foreach ln txt
    (prompt (strcat ln "\n"))
  )
  (princ)
)

 

 

 

Exactly what i need, thanks.

Link to comment
Share on other sites

Like lee why ?

 

If you want a lets look use a lisp open a file and read each line then can release file name.

 

(setq Path "yourfile")
(setq fo (open path "R"))
(while (setq str (read-line fo))
(princ str)
(princ "\n")
)
(close fo)

 

Mhupp forgot to hit submit button yesterday same idea.

Edited by BIGAL
  • Agree 1
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...