Jump to content

Recommended Posts

Posted

I'm not sure but I assume this is simple, I want to create a new .txt file from a lisp routine. Can this be done? I have searched the internet and these forums and can't find anything at all on how to create a file. Everything just tells you how to write to an existing file

Thanks in advance.

Posted

In its simplest form:

(setq des (open "C:\\MyTextFile.txt" "w"))
(write-line "This is a test." des)
(close des)

 

And a slightly more elaborate example:

(defun c:writetextfile ( / des txt ) ;; Define function, declare local variables
   (if (setq txt (getfiled "Create Text File" "" "txt" 1)) ;; Prompt user for filename/filepath
       (if (setq des (open txt "w")) ;; Attempt to create text file with given filename/filepath
           (progn ;; Evaluate the enclose expressions as the 'then' expression for the IF statement
               (write-line "This is a test." des) ;; Write line of text to text file
               (close des) ;; Close file descriptor
           ) ;; end PROGN
           (princ "\nUnable to create text file.") ;; Else the text file could not be created
       ) ;; end IF
       (princ "\n*Cancel*") ;; Else the user pressed Cancel
   ) ;; end IF
   (princ) ;; Suppress the return of the last evaluated expression
) ;; end DEFUN

Posted

Ok, thanks for that. It appears I am doing it correctly but it doesn't work

 

(setq StrRx (open "C:\\WCcharset.txt" "w"))
(write-line p+ StrRx)
(close StrRx)

 

THis is my code. My code works if I manually create the file already, but as soon as I delete the file from my hard drive, it doesn't work and i get an error.

Any thoughts?

I thought it might be to do with permissions but I made sure my security settings on c drive allowed full access.

 

Really stumped

Posted

Ok, it was a permissions thing. No mater how much full control i gave myself of c drive I still didn't have enough permissions to create a file, even manually. Got to love Windows.

Posted
Ok, it was a permissions thing. No mater how much full control i gave myself of c drive I still didn't have enough permissions to create a file, even manually. Got to love Windows.

 

Yes, as shown in my second example above, you'll need to include error trapping to ensure that a valid file descriptor is returned before attempting to write to it & close it, e.g.:

 

(if (setq des (open "C:\\test.txt" "w"))
   (progn
       (write-line "testing" des)
       (close des)
   )
   (princ "\nUnable to create/modify test.txt.")
)

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