PDA

View Full Version : How to output the content of a directory into a text file using lisp?


Leekhinc
11-26-2003, 04:25 PM
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. However, I got errors when executing the program in OSDM.

(setq a (directory d:/doc/*.pkg))
(with-open-file (a "d:/doc/pkg.dat" :direction :output :if-exists :supersede) )

Thanks in advance.

dorothea
11-26-2003, 09:40 PM
Hi,

Try it with the following function:


(defun read-and-write-directory ()
(let ((all-pkg-files (directory (format nil "*.pkg")))
log-file
)
(setf log-file (open "d:/temp/listing_all_package_files.txt"
:direction :output
:if-exists :new-version
:if-does-not-exist :create))

(dolist (pkg-file all-pkg-files)
(format log-file "~A~%" (namestring pkg-file))
)
(close log-file)
)
)


If you want to have other file types you have to change the format.

Hope this helps,
Dorothea

Leekhinc
12-09-2003, 06:46 PM
Thanks for your help dorothea.

I have another question. I have got a file say filelist.txt with the content as below.

D:/doc/frontplate.pkg
D:/doc/housing.pkg

I created another lisp program to read a line from the file.

(setf a (open "d:/doc/filelist.txt"
:direction :input
:element-type 'character))

(setq b (read-line a))

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

(LOAD_PACKAGE c)

As you can see, the program will load the variable c too. However, the variable c will return "D:\doc\frontplate.pkg".
How can we change the backslashes to forward slashes so that the program can load the pkg file correctly?

Thanks in advance.

Leekhinc
12-09-2003, 09:26 PM
I found the solution. The variable b actually return the correct format which is "D:/doc/frontplate.pkg".

Thank you.

clausb
12-09-2003, 10:47 PM
See the Common LISP HyperSpec (http://www.lispworks.com/reference/HyperSpec/Front/Contents.htm), chapters 19 and 20 to learn how to properly deal with file names and paths. Also, the Developer's Kit documentation which we ship along with OSDM has a section on OS/file system interfacing.

Hope this helps,

Claus

John Scheffel
12-10-2003, 01:58 PM
You might also want to look at these two functions.

sd-convert-filename-to-platform
sd-convert-filename-from-platform

Which can be used to convert the slashes depending on the OS being used. See the file

Install_dir\help\Common\documentation\integration_kit\reference\filing_and_os.html