an empty problem...

Started by tom, January 04, 2005, 08:10:36 PM

Previous topic - Next topic

tom

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

HPW

#1
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.
Hans-Peter

eddier

#2
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

Sammo

#3
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)))

eddier

#4
Oops, in my code, I assumed he wanted to return the file contents if is wasn't empty. My bad.



Eddie

Lutz

#5
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

tom

#6
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!

Lutz

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

tom

#8
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?

Sammo

#9
(nth 9 (now)) returns minutes west of Greenwich, so (now (- (nth 9 (now)))) returns values corrected for your time zone.

tom

#10
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).

tom

#11
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)

HPW

#12
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)
Hans-Peter