From: Janet
Subject: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <3fd0edfb$1@news.starhub.net.sg>
Does anyone know how to output the content eg. filenames of a directory into
a text file using lisp? I would appreciate if some examples are provided. I
actually did some work as below.

(defun file ()
(let ((all-pkg-files (directory (format nil "c:/my documents/*.doc")))
log-file
)
(setf log-file (open "c:/a.txt"
:direction :output
:if-exists :new-version
:if-does-not-exist :create))

The content of the file "a.txt" should look something like below.
a.doc
b.doc
123.doc

Thanks in advance.

From: Pascal Bourguignon
Subject: Re: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <87ekvia5lh.fsf@thalassa.informatimago.com>
"Janet" <·····@starhub.net.sg> writes:

> Does anyone know how to output the content eg. filenames of a directory into
> a text file using lisp? I would appreciate if some examples are provided. I
> actually did some work as below.
> 
> (defun file ()
> (let ((all-pkg-files (directory (format nil "c:/my documents/*.doc")))
> log-file
????
> )
> (setf log-file (open "c:/a.txt"
> :direction :output
> :if-exists :new-version
> :if-does-not-exist :create))
????
 
> The content of the file "a.txt" should look something like below.
> a.doc
> b.doc
> 123.doc
> 
> Thanks in advance.

Oh! I see, you meant:

(defun file ()
  (let ((all-pkg-files (directory (format nil "c:/my documents/*.doc")))
        log-file)
    (setf log-file (open "c:/a.txt"
                         :direction :output
                         :if-exists :new-version
                         :if-does-not-exist :create))))

You  should use emacs  (it runs  on MS-DOS)  it indents  properly lisp
sources automatically.

(defun file ()
  (let ((all-pkg-files (directory (format nil "c:/my documents/*.doc")))
        (log-file (open "c:/a.txt"
                         :direction :output
                         :if-exists :new-version
                         :if-does-not-exist :create)))
    (format log-file "~{~A~%~}" all-pkg-files)
    (close log-file)))


This could be rewritten with the nice macro with-open-file:

(defun file (pattern outpath)
  (with-open-file (log-stream outpath
                          :direction :output
                          :if-exists :new-version
                          :if-does-not-exist :create)
    (format log-stream "~{~A~%~}" (directory pattern))))

(ext:shell "touch /tmp/{a,b,c}.doc")
(file "/tmp/*.doc" "/tmp/dir.txt")
(ext:shell "cat /tmp/dir.txt")

/tmp/c.doc
/tmp/b.doc
/tmp/a.doc

0

http://www.lisp.org/HyperSpec/Body/sec_the_streams_dictionary.html
http://www.lisp.org/HyperSpec/Body/sec_22-3.html


-- 
__Pascal_Bourguignon__                              .  *   * . * .* .
http://www.informatimago.com/                        .   *   .   .*
                                                    * .  . /\  ).  . *
Living free in Alaska or in Siberia, a               . .  / .\   . * .
grizzli's life expectancy is 35 years,              .*.  / *  \  . .
but no more than 8 years in captivity.                . /*   o \     .
http://www.theadvocates.org/                        *   '''||'''   .
SCO Spam-magnet: ··········@sco.com                 ******************
From: Nikodemus Siivola
Subject: Re: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <bqsi1e$m4m$1@nyytiset.pp.htv.fi>
Janet <·····@starhub.net.sg> wrote:

> (defun file ()
> (let ((all-pkg-files (directory (format nil "c:/my documents/*.doc")))
> log-file
> )
>
> (setf log-file (open "c:/a.txt"
> :direction :output
> :if-exists :new-version
> :if-does-not-exist :create))

 * That (directory (format ...)) bit is just plain wierd and useless.
   The format does absolutely nothing there.

 * Your let-form binds the list returned by directory to all-pkg-files,
   but log-file is unbound within the scope of the function.

 * log-file also has no global binding when you setf it. Unless you want
   to have legions of hell appear out of your nose, don't do that. 
   (see http://www.cliki.net/Nasal%20Demons)

 * Indent your code. Or even better: let your editor, which surely is
   both paren and lisp aware, do it.

 * Never, ever, use hanging parens. It's a hanging offence.

So:

;; You probably actually mean :supersede, not :new-version.
;; Also, eventually you need to close the file you've opened.
;; with-open-file is very convenient for that. Oh well.
;;
;; Anyways, after this there is a stream you can write to
;; bound to the global variable *log-file*.
(defvar *log-file* (open "c:/a.txt" 
                         :direction :output
                         :if-exists :new-version))

;; I've kept your function name, but it's a bad name.
;; It's bad because it really tells nothing about what you
;; expect the function to do. And since the function is
;; broken I'm forced to guess.
;;
;; I'm guessing that you mean calling this function to
;; write the directory contents to the stream, one
;; per line.
(defun file ()
  (dolist (name (directory "c:/my documents/*.doc"))
    (write-line (namestring name) *log-file*)))

Hope that helps. But the best advice is: find a good book and work
through it in order.

Cheers,

 -- Nikodemus
From: Janet
Subject: Re: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <3fd26dee@news.starhub.net.sg>
Thanks for the help guys! Really appreciate. I have got another question.

I have got a file say "files.txt" with the content as shown below.
c:\my documents\my job stories.doc
c:\my documents\girlwedding.doc
c:\my documents\daddycard.doc
c:\my documents\dadbirthday tableformat.doc

I created some lisp code (see below) to read a line from the file. When I
execute b, the return is "c:\\my documents\\my job stories.doc". Is there
anyway to return just "c:\my documents\my job stories.doc" without the
double forward slashes?

(setf a (open "c:/files.txt"
        :direction :input
        :element-type 'character))

(setq b (read-line a))

Thanks in advance.

"Janet" <·····@starhub.net.sg> wrote in message
···············@news.starhub.net.sg...
> Does anyone know how to output the content eg. filenames of a directory
into
> a text file using lisp? I would appreciate if some examples are provided.
I
> actually did some work as below.
>
> (defun file ()
> (let ((all-pkg-files (directory (format nil "c:/my documents/*.doc")))
> log-file
> )
> (setf log-file (open "c:/a.txt"
> :direction :output
> :if-exists :new-version
> :if-does-not-exist :create))
>
> The content of the file "a.txt" should look something like below.
> a.doc
> b.doc
> 123.doc
>
> Thanks in advance.
>
>
From: Rahul Jain
Subject: Re: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <87d6b1xdxo.fsf@nyct.net>
"Janet" <·····@starhub.net.sg> writes:

> I created some lisp code (see below) to read a line from the file. When I
> execute b, the return is "c:\\my documents\\my job stories.doc". Is there
> anyway to return just "c:\my documents\my job stories.doc" without the
> double forward slashes?

There aren't even single forward slashes anywhere there. I think you
mean the backslashes. The REPL prints out the string as an object, not
directly. The string "c:\my documents\my job stories.doc" contains no
directory separators. Try doing
(write-line "c:\my documents\my job stories.doc")
and you'll see that it prints out:
c:my documentsmy job stories.doc

You need those backslashes to indicate that you want a literal backslash
in the string.

> (setq b (read-line a))

The result of this form is the value that was assigned to b. This is
what is printed out by the REPL, as an object, and the printer then
escapes the backslashes in the string so that the representation is
correct.

HTH,
--
Rahul Jain
From: Peter Seibel
Subject: Re: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <m3wu99qatd.fsf@javamonkey.com>
"Janet" <·····@starhub.net.sg> writes:

> Thanks for the help guys! Really appreciate. I have got another question.
> 
> I have got a file say "files.txt" with the content as shown below.
> c:\my documents\my job stories.doc
> c:\my documents\girlwedding.doc
> c:\my documents\daddycard.doc
> c:\my documents\dadbirthday tableformat.doc
> 
> I created some lisp code (see below) to read a line from the file. When I
> execute b, the return is "c:\\my documents\\my job stories.doc". Is there
> anyway to return just "c:\my documents\my job stories.doc" without the
> double forward slashes?

Try this: (format t "~a" b) or (write b :escape nil)

In other words, the string doesn't really have double slashes in it;
it's just Lisp printing the string in b in a readable form that makes
it look that way. Just go ahead and use the line you read however you
were planning to.

-Peter

> 
> (setf a (open "c:/files.txt"
>         :direction :input
>         :element-type 'character))
> 
> (setq b (read-line a))
> 
> Thanks in advance.
> 
> "Janet" <·····@starhub.net.sg> wrote in message
> ···············@news.starhub.net.sg...
> > Does anyone know how to output the content eg. filenames of a directory
> into
> > a text file using lisp? I would appreciate if some examples are provided.
> I
> > actually did some work as below.
> >
> > (defun file ()
> > (let ((all-pkg-files (directory (format nil "c:/my documents/*.doc")))
> > log-file
> > )
> > (setf log-file (open "c:/a.txt"
> > :direction :output
> > :if-exists :new-version
> > :if-does-not-exist :create))
> >
> > The content of the file "a.txt" should look something like below.
> > a.doc
> > b.doc
> > 123.doc
> >
> > Thanks in advance.
> >
> >
> 
> 

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: Janet
Subject: Re: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <3fd29334@news.starhub.net.sg>
Hi guys, I tried the below lisp code. But the variable c will return "c:\\my
documents\\my job stories.doc" for example 1 & "NIL" for example 2. What I
really want is to make variable c return "c:\my documents\my job
stories.doc" without the double slashes. Any suggestions?

Example 1:

(setf a (open "c:/files.txt"
        :direction :input
        :element-type 'character))

(setq b (read-line a))

(setq c (write b :escape nil))

Example 2:

(setf a (open "c:/files.txt"
        :direction :input
        :element-type 'character))

(setq b (read-line a))

(setq c (format t "~a" b))



"Peter Seibel" <·····@javamonkey.com> wrote in message
···················@javamonkey.com...
> "Janet" <·····@starhub.net.sg> writes:
>
> > Thanks for the help guys! Really appreciate. I have got another
question.
> >
> > I have got a file say "files.txt" with the content as shown below.
> > c:\my documents\my job stories.doc
> > c:\my documents\girlwedding.doc
> > c:\my documents\daddycard.doc
> > c:\my documents\dadbirthday tableformat.doc
> >
> > I created some lisp code (see below) to read a line from the file. When
I
> > execute b, the return is "c:\\my documents\\my job stories.doc". Is
there
> > anyway to return just "c:\my documents\my job stories.doc" without the
> > double forward slashes?
>
> Try this: (format t "~a" b) or (write b :escape nil)
>
> In other words, the string doesn't really have double slashes in it;
> it's just Lisp printing the string in b in a readable form that makes
> it look that way. Just go ahead and use the line you read however you
> were planning to.
>
> -Peter
>
> >
> > (setf a (open "c:/files.txt"
> >         :direction :input
> >         :element-type 'character))
> >
> > (setq b (read-line a))
> >
> > Thanks in advance.
> >
> > "Janet" <·····@starhub.net.sg> wrote in message
> > ···············@news.starhub.net.sg...
> > > Does anyone know how to output the content eg. filenames of a
directory
> > into
> > > a text file using lisp? I would appreciate if some examples are
provided.
> > I
> > > actually did some work as below.
> > >
> > > (defun file ()
> > > (let ((all-pkg-files (directory (format nil "c:/my documents/*.doc")))
> > > log-file
> > > )
> > > (setf log-file (open "c:/a.txt"
> > > :direction :output
> > > :if-exists :new-version
> > > :if-does-not-exist :create))
> > >
> > > The content of the file "a.txt" should look something like below.
> > > a.doc
> > > b.doc
> > > 123.doc
> > >
> > > Thanks in advance.
> > >
> > >
> >
> >
>
> --
> Peter Seibel                                      ·····@javamonkey.com
>
>          Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: Rahul Jain
Subject: Re: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <87zne5vsh1.fsf@nyct.net>
"Janet" <·····@starhub.net.sg> writes:

> Hi guys, I tried the below lisp code. But the variable c will return "c:\\my
> documents\\my job stories.doc" for example 1 & "NIL" for example 2. What I
> really want is to make variable c return "c:\my documents\my job
> stories.doc" without the double slashes. Any suggestions?

The string "\m" contains a single character, #\m.
The string "\\m" contains two characters, #\\ and #\m.

FYI, (format t ...) prints some text to *standard-output* and then
returns NIL, not "NIL".

--
Rahul Jain
From: Thomas A. Russ
Subject: Re: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <ymismjvtd4j.fsf@sevak.isi.edu>
"Janet" <·····@starhub.net.sg> writes:

> 
> Hi guys, I tried the below lisp code. But the variable c will return "c:\\my
> documents\\my job stories.doc" for example 1 & "NIL" for example 2. What I
> really want is to make variable c return "c:\my documents\my job
> stories.doc" without the double slashes. Any suggestions?

As Peter Seibel wrote in his answer to you, the string doesn't have the
double slashes in it.  It is just that the PRINTED version of the string
that is shown on your screen is written that way.  It has to be in order
to make the string readable to Lisp (since \ is the escape character for
string literals).

You can verify this in several ways.

For example, compare the following

    b
    (format t "~A" b)
    (format t "~S" b)

The first formatted string shows just the contents of the string.  The
second one shows it in such a way as to be readable to lisp.  That
happens to be the same as when you just type it.

Some other tests to convince yourself that you have the right answer:

   (coerce b 'list)
   (length b)




> 
> Example 1:
> 
> (setf a (open "c:/files.txt"
>         :direction :input
>         :element-type 'character))
> 
> (setq b (read-line a))
> 
> (setq c (write b :escape nil))
> 
> Example 2:
> 
> (setf a (open "c:/files.txt"
>         :direction :input
>         :element-type 'character))
> 
> (setq b (read-line a))
> 
> (setq c (format t "~a" b))

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Janet
Subject: Re: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <3fd50fba@news.starhub.net.sg>
Hi guys. Thanks for all the help. I have another question.

How to I change the backslahes to forward slashes?
For eample, I want to change "c:\my documents\my job stories.doc" to "c:/my
documents/my job stories.doc".

Thanks in advance.

"Janet" <·····@starhub.net.sg> wrote in message
·············@news.starhub.net.sg...
> Hi guys, I tried the below lisp code. But the variable c will return
"c:\\my
> documents\\my job stories.doc" for example 1 & "NIL" for example 2. What I
> really want is to make variable c return "c:\my documents\my job
> stories.doc" without the double slashes. Any suggestions?
>
> Example 1:
>
> (setf a (open "c:/files.txt"
>         :direction :input
>         :element-type 'character))
>
> (setq b (read-line a))
>
> (setq c (write b :escape nil))
>
> Example 2:
>
> (setf a (open "c:/files.txt"
>         :direction :input
>         :element-type 'character))
>
> (setq b (read-line a))
>
> (setq c (format t "~a" b))
>
>
>
> "Peter Seibel" <·····@javamonkey.com> wrote in message
> ···················@javamonkey.com...
> > "Janet" <·····@starhub.net.sg> writes:
> >
> > > Thanks for the help guys! Really appreciate. I have got another
> question.
> > >
> > > I have got a file say "files.txt" with the content as shown below.
> > > c:\my documents\my job stories.doc
> > > c:\my documents\girlwedding.doc
> > > c:\my documents\daddycard.doc
> > > c:\my documents\dadbirthday tableformat.doc
> > >
> > > I created some lisp code (see below) to read a line from the file.
When
> I
> > > execute b, the return is "c:\\my documents\\my job stories.doc". Is
> there
> > > anyway to return just "c:\my documents\my job stories.doc" without the
> > > double forward slashes?
> >
> > Try this: (format t "~a" b) or (write b :escape nil)
> >
> > In other words, the string doesn't really have double slashes in it;
> > it's just Lisp printing the string in b in a readable form that makes
> > it look that way. Just go ahead and use the line you read however you
> > were planning to.
> >
> > -Peter
> >
> > >
> > > (setf a (open "c:/files.txt"
> > >         :direction :input
> > >         :element-type 'character))
> > >
> > > (setq b (read-line a))
> > >
> > > Thanks in advance.
> > >
> > > "Janet" <·····@starhub.net.sg> wrote in message
> > > ···············@news.starhub.net.sg...
> > > > Does anyone know how to output the content eg. filenames of a
> directory
> > > into
> > > > a text file using lisp? I would appreciate if some examples are
> provided.
> > > I
> > > > actually did some work as below.
> > > >
> > > > (defun file ()
> > > > (let ((all-pkg-files (directory (format nil "c:/my
documents/*.doc")))
> > > > log-file
> > > > )
> > > > (setf log-file (open "c:/a.txt"
> > > > :direction :output
> > > > :if-exists :new-version
> > > > :if-does-not-exist :create))
> > > >
> > > > The content of the file "a.txt" should look something like below.
> > > > a.doc
> > > > b.doc
> > > > 123.doc
> > > >
> > > > Thanks in advance.
> > > >
> > > >
> > >
> > >
> >
> > --
> > Peter Seibel                                      ·····@javamonkey.com
> >
> >          Lisp is the red pill. -- John Fraser, comp.lang.lisp
>
>
From: Howard Ding <······@hading.dnsalias.com>
Subject: Re: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <m365gqm3u7.fsf@frisell.localdomain>
"Janet" <·····@starhub.net.sg> writes:

> Hi guys. Thanks for all the help. I have another question.
>
> How to I change the backslahes to forward slashes?
> For eample, I want to change "c:\my documents\my job stories.doc" to "c:/my
> documents/my job stories.doc".
>
> Thanks in advance.
>

http://www.lispworks.com/reference/HyperSpec/Body/f_sbs_s.htm

Take note particularly of the first example which corresponds closely
to your question.


-- 
Howard Ding
<······@hading.dnsalias.com>
From: Peter Seibel
Subject: Re: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <m37k16m3rr.fsf@javamonkey.com>
"Janet" <·····@starhub.net.sg> writes:

> Hi guys. Thanks for all the help. I have another question.
> 
> How to I change the backslahes to forward slashes?
> For eample, I want to change "c:\my documents\my job stories.doc" to "c:/my
> documents/my job stories.doc".

Look up SUBSTITUTE in you favorite Lisp reference.

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: Håkon Alstadheim
Subject: Re: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <m0oeuk7ug4.fsf@alstadhome.dyndns.org>
"Janet" <·····@starhub.net.sg> writes:

> I created some lisp code (see below) to read a line from the file. When I
> execute b, the return is "c:\\my documents\\my job stories.doc".

The return is _printed_ as  "c:\\my documents\\my job stories.doc",
because to be able to read a \ it has to be escaped with a \. Read up
on how lisp prints objects, and what "readable" means. Then try

(format t "~A" "c:\\dir")
(format t "~S" "c:\\dir")

Notice that one prints a quoted string, the other just prints the
string object itself.

> Is there anyway to return just "c:\my documents\my job stories.doc"
> without the double forward slashes?

It _is_ returning it with just \. It is _printing_ with \\.

-- 
H�kon Alstadheim, hjemmepappa.
From: Thomas A. Russ
Subject: Re: How to output the content of a directory into a text file using lisp?
Date: 
Message-ID: <ymiu14btdc6.fsf@sevak.isi.edu>
"Janet" <·····@starhub.net.sg> writes:

> 
> Does anyone know how to output the content eg. filenames of a directory into
> a text file using lisp? I would appreciate if some examples are provided. I
> actually did some work as below.
> 
> (defun file ()
> (let ((all-pkg-files (directory (format nil "c:/my documents/*.doc")))
> log-file
> )
> (setf log-file (open "c:/a.txt"
> :direction :output
> :if-exists :new-version
> :if-does-not-exist :create))

But it is still missing a few parts.  You need to find a lisp construct
that will iterate (or recurse, if you insist) over the entries in the
directory.

Then you will need to remember to close the file you open with OPEN.
You can do that with CLOSE, but for your purposes a better solution
would be to use the WITH-OPEN-FILE macro instead.

> 
> The content of the file "a.txt" should look something like below.
> a.doc
> b.doc
> 123.doc

The results of DIRECTORY will be a list of pathnames.  If you want just
the file names (to make the resulting output prettier), you may want to
investigate the use of NAMESTRING, TRUENAME or perhaps constructing your
own output using PATHNAME-NAME, PATHNAME-TYPE and for completeness
PATHNAME-VERSION.

You can get some insight into the parts of the pathname objects by
either reading the documentation or by getting one of them and calling
the DESCRIBE function on it.

> 
> Thanks in advance.
> 

Also, I would note that you don't need to use the FORMAT statement just
to get a literal string.  You could just use the string, but I will
assume that what you posted was just a simplified version of the real
code.  In any case, you can use FORMAT with the results of the pathname
accessor functions I mentioned above to construct the names for output.
You would then just pass the stream you get from opening the file as the
first argument to the function.

For a simple (to write, at least) version consider:


(defun file-list (destination pattern)
  (with-open-file (output destination :direction :output
                                      :if-exists :new-version
                                      :if-does-not-exist :create)
    (mapcar #'(lambda (pathname) (print pathname output))
            (directory pattern))))


-- 
Thomas A. Russ,  USC/Information Sciences Institute