newLISP Fan Club

Forum => newLISP in the real world => Topic started by: tom on January 04, 2005, 08:10:36 PM

Title: an empty problem...
Post by: tom on January 04, 2005, 08:10:36 PM
Hey guys,



I'd like to  write a file into another file, only if it's not empty.



this snippet is ugly, and out of context, but should give you an idea...



(if (not (empty? (read-file (append notespath note)))
 (write-file (append notespath stamped-name) (read-file (append notespath note)))))


As usual, it's probably easy, I'm just missing it...
Title:
Post by: HPW on January 04, 2005, 10:49:38 PM
What about:



(if (!=(read-file (append notespath note))"")
     (write-file (append notespath stamped-name) (read-file (append notespath note)))))


This assume that the file exists! Otherways you have to check this before.
Title:
Post by: eddier on January 05, 2005, 05:50:02 AM
I'm not sure what the stamped-name means but here is my shot at it.

(let (file-content (read-file (append notespath note)))
  (if (empty? file-content)
    (write-file (append notespath stamped-name))
    file-content))


Eddie
Title:
Post by: Sammo on January 05, 2005, 05:53:23 AM
Perhaps the 'file-info' function can help:(set 'fromName (append notespath note))
(set 'toName (append notespath stamped-name))

(set 'info (file-info fromName))
(if (and info (> (nth 0 info) 0))
    (write-file toName (read-file fromName)))
Title:
Post by: eddier on January 05, 2005, 06:00:02 AM
Oops, in my code, I assumed he wanted to return the file contents if is wasn't empty. My bad.



Eddie
Title:
Post by: Lutz on January 05, 2005, 06:09:09 AM
Using 'file-info' with 'copy-file', it also assumes that the file exists, but so did most other solutions (except for Sam's)



(if (> (first (file-info (append notespath note))) 0)
    (copy-file (append notespath note) (append notespath stamped-name)))


Lutz
Title:
Post by: tom on January 07, 2005, 08:47:59 AM
hey guys,



I'm writing a simple note-jotting script, which is bound to a key.  I

hit the key, and up pops an editor window showing notes.txt.  When I jot

a note, save and close the editor, notes.txt is moved to a

timestamped file name, unless, for whatever reason, I close notes.txt

without writing anything to it.



I would post the code (and may yet), but it's too ugly right now.

Everything works, except for an error with the timestamping.  I'd

like a yyyymmddhhss format.  What I have chokes at certain times of

the day, giving me yyyymmdd-hss.



is there a short and clean way to stamp?  three quarters of my script

wrestles with (now) to achieve the desired result.



by the way, I used Hans-Peter's solution for the "empty" part.



Thanks!
Title:
Post by: Lutz on January 07, 2005, 09:19:40 AM
Try this:



(eval (append '(format "%04d%02d%02d%02d%02d%02d") (slice (now) 0 6)))



=> "20050107171526"



in the 'now' function you could specify an offset to adjust for a different timezone, without offset it will give you UTC, which is probably what you want.



Lutz
Title:
Post by: tom on January 07, 2005, 12:40:08 PM
Quote from: "Lutz"
in the 'now' function you could specify an offset to adjust for a different timezone, without offset it will give you UTC, which is probably what you want.

Lutz


may I have an example for, say, Eastern Standard Time?  Is that UTC - 5?
Title:
Post by: Sammo on January 07, 2005, 12:52:14 PM
(nth 9 (now)) returns minutes west of Greenwich, so (now (- (nth 9 (now)))) returns values corrected for your time zone.
Title:
Post by: tom on January 08, 2005, 03:38:00 AM
Quote from: "Sammo"(nth 9 (now)) returns minutes west of

Greenwich, so (now (- (nth 9 (now)))) returns values

corrected for your time zone.


Thank you.  I saw (nth 9) returned by (now) but I

wasn't sure what to do with it...



What I did to begin with was to squash all the items

returned by (now) together using (append), subtracting

5 from the value at (nth 3 (now)).  I also manually

added a 0 to day and month values if they were a single

digit.



I don't know C, and so the solution using (format) is a

mystery to me (even more mysterious than newlisp still

is).
Title:
Post by: tom on January 08, 2005, 03:43:36 AM
Oh.  Here's my silly little script as it is now...



#!/usr/bin/newlisp

;; File:   quicknotes.lsp
;; $Id$

(set 'stamp
 (eval
  (append '(format "%04d%02d%02d%02d%02d%02d")
  (slice (now (- (nth 9 (now)))) 0 6)
))
)

(set 'stamped-file (append stamp ".txt"))
(set 'notes-path "/path/to/notes/")
(set 'note "notes.txt")
(set 'file-in (append notes-path note))
(set 'file-out (append notes-path stamped-file))

(! (append "leafpad " file-in))

(if (> (first (file-info file-in)) 0)
    (rename-file file-in file-out))

(exit)
Title:
Post by: HPW on January 08, 2005, 05:39:43 AM
set can use multiple arguments:


#!/usr/bin/newlisp

;; File:   quicknotes.lsp
;; $Id$

(set 'stamp
     (eval
      (append '(format "%04d%02d%02d%02d%02d%02d")
            (slice (now (- (nth 9 (now)))) 0 6)
       ))
     'stamped-file (append stamp ".txt")
     'notes-path "/path/to/notes/"
     'note "notes.txt"
     'file-in (append notes-path note)
     'file-out (append notes-path stamped-file)
)
(! (append "leafpad " file-in))

(if (> (first (file-info file-in)) 0)
    (rename-file file-in file-out))

(exit)