directory

Q&A's, tips, howto's
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

directory

Post by newdep »

Hi Lutz,

Small question on a small problem..?
(on windows and linux)


(directory "/") returns a list of all files in "/" * this works fine *

(directory? "/") returns true * also fine *


(filter directory? (directory "/") * not so fine, returns () *


So where is the problem ? Is it with 'Filter ? because the above is 100%
according the syntax expectation..

BUT! this works ->

(change-dir "/")
(filter directory? (directory "/"))

* Where "/" is the current directory, if you leave "/" out it also works
but it does not work without change-dir *


Where is the misintepretation?

regards, Norman
-- (define? (Cornflakes))

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

Post by Lutz »

Everything is fine :), in:

(filter directory? (directory "/") )

the term:

(directory "/")

returns a list of all files in "/" but without the directory prefix. The 'directory?' function then checks i.e. "WINDOWS", which is a directory but not in the current directory. For that reason it works when you do a 'change-dir' to "/" first or prepend a "/" to "WINDOWS" to get "/WINDOWS"

(directory? "WINDOWS") => nil
(directory? "/WINDOWS") => true

Try this, and it works without 'change-dir'

(filter directory? (map (fn (f) (append "/" f)) (directory "/")))

Lutz

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Oke yes thats what im doing currently too..though it makes life not easier
when its upto the next problem ->

Try to make a MAP of all the directory Names on your local harddisk and
store them in a nested LIST as a tree ;-)

Thats partly the problem im facing regarding the topic on 'Depth' :-)

Regards, Norman
-- (define? (Cornflakes))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

I need to take care of several counters regarding the depth
of the directory structure...

(1) Current directory in the tree
(2) amount of directorys in the current directory
(3) the depth inside the main directory tree

Im trying to make 1 simple lambda that can cover this nested problem ;-)

(I think thats a nice Brain teaser ;-)

Regards, Norman.
-- (define? (Cornflakes))

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

Post by Lutz »

Try this one:

Code: Select all

(define (show-tree dir counter)
 (dolist (nde (directory dir))
   (if (and (directory? (append dir "/" nde)) (!= nde ".") (!= nde ".."))
        (show-tree (append dir "/" nde) (+ counter 1))
        (println counter ": "(append dir "/" nde)))))

> (show-tree "c:/Borland" 0)
0: c:/Borland/.
0: c:/Borland/..
1: c:/Borland/BCC55/.
1: c:/Borland/BCC55/..
2: c:/Borland/BCC55/Bin/.
2: c:/Borland/BCC55/Bin/..
2: c:/Borland/BCC55/Bin/bcc32.cfg
2: c:/Borland/BCC55/Bin/bcc32.exe
Works on both Linux and Win32

Lutz

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

!!! APPLAUSE !!! Guru in progress ;-)

Thanks !

Norman.
-- (define? (Cornflakes))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Btw... Thats one hell off an CPU eater ;-) My windows machine goes crazy
when I run that one from the newlisp console...

Norman.
-- (define? (Cornflakes))

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

Post by Lutz »

This is due to the huge amount of text output filling up the console display buffer in newLISP-tk which is of many-megabyte size.

When running show-tree in a shell window as a command-line utility memory requirements are costant about 1Mbyte on my machine and it does about 5,000 files per second.

Lutz

Locked