Andrew1979 Posted November 30, 2014 Posted November 30, 2014 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. Quote
Lee Mac Posted November 30, 2014 Posted November 30, 2014 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 Quote
Andrew1979 Posted November 30, 2014 Author Posted November 30, 2014 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 Quote
Andrew1979 Posted November 30, 2014 Author Posted November 30, 2014 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. Quote
Lee Mac Posted November 30, 2014 Posted November 30, 2014 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.") ) Quote
Recommended Posts
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.