Page 1 of 1

creating a list from directory and file-info

Posted: Thu Apr 05, 2007 5:10 pm
by SHX
Hello,

I am trying to create a list with file\folder names fro the directory commandd and the file-info information for each member of the list.

I have the output in 2 lists and I am trying to use a map with a push to combine them. However the push returns just the item that was pushed.

If someone can help with an example of how create a list of files and there info and also how can values from one list be pushed into a list of lists.

Thanks,

I hope I was understandable.

Steven

Posted: Thu Apr 05, 2007 5:27 pm
by cormullion
I'm not sure you need two lists - you can get file-info for every item in a list produced by directory by mapping a function over the list:

Code: Select all

(set 'dirlist (directory "/"))

(map (fn (f) (println f "\t" (file-info f))) dirlist)

;->
..Developer	(476 16893 0 0 80 1175695159 1175694126 1175694126)
etc	        (3162 41453 0 0 80 1127373182 1127373182 1137361024)
Library	(1870 17405 0 0 80 1175791160 1174234983 1174234983)
mach	(603684 41453 0 0 80 1174913732 1174913732 1174913732)
..
If you have two lists that match, you can combine them neatly:

Code: Select all

(set 'dirlist (directory "/"))
(set 'infolist (map (fn (f) (file-info f)) dirlist))
(transpose (list dirlist infolist))

or

(map list dirlist infolist)

Posted: Thu Apr 05, 2007 6:19 pm
by Lutz
... and to combine two lists you would do the following:

Code: Select all

> (set 'list-1 '(a b c d))
(a b c d)
> (set 'list-2 '(1 2 3 4))
(1 2 3 4)
> (map list list-1 list-2)
((a 1) (b 2) (c 3) (d 4))
> 
Lutz

Posted: Fri Apr 06, 2007 7:24 pm
by SHX
Lutz and cormullion, thanks alot for your help. I am new at this but starting to get the hang of it and really appreciate the power it has. Thanks Lutz for such a great product.

Steven

Posted: Sun Apr 29, 2007 1:08 pm
by SHX
Hi,

When I do

Code: Select all

(map list list-1 list-2)

I get the following.
("Demo.lsp" (1408 33206 2 0 0 1177559281 1174002288 1174002288))
("Drag.lsp" (1152 33206 2 0 0 1177559281 1174002288 1174002288))
How can I make it that it each file name and it's info be in one list like the following.
(("Demo.lsp" 1408 33206 2 0 0 1177559281 1174002288 1174002288)
("Drag.lsp" 1152 33206 2 0 0 1177559281 1174002288 1174002288))
thanks

Steven

Posted: Sun Apr 29, 2007 2:13 pm
by cormullion
I think you want to use "cons" rather than "list" here.

Code: Select all

(set 'file-name-list (directory "/")) 
(set 'file-info-list (map file-info file-name-list))
(map cons file-name-list file-info-list)

( ("Applications" 3808 16893 0 0 80 1177793256 1177531823 1177531823) 
 ("Applications (Mac OS 9)" 578 16895 0 501 80 1177793345 1175184313 
  1175184313) 
...