an empty problem...

Q&A's, tips, howto's
Locked
tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

an empty problem...

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

Code: Select all

(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
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

What about:

Code: Select all

(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
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

I'm not sure what the stamped-name means but here is my shot at it.

Code: Select all

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

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

Perhaps the 'file-info' function can help:

Code: Select all

(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
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

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

Eddie

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Using 'file-info' with 'copy-file', it also assumes that the file exists, but so did most other solutions (except for Sam's)

Code: Select all

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

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

Lutz wrote: 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
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

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

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

Sammo wrote:(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
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

Oh. Here's my silly little script as it is now...

Code: Select all

#!/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
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

set can use multiple arguments:

Code: Select all

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

Locked