Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Recursive-Read question

 [1/7] from: bry::itnisk::com at: 5-Nov-2002 13:49


Hi, I have a question about the Recursive-Read function, which is provided in http://www.reboltech.com/library/html/Recursive Read.html Now of course what it does is not to Recursively Read a directory but rather read a file or Url into a block and then recursively read that block. The problem there is that if I want to get something hierarchical out of it, I can't. As an example of what I mean I have the following: xml-dec: rejoin["<?xml version='1.0' encoding='ISO-8859-1'?><files>"] write %dir.xml xml-dec sep: "/" ReadDir-ToXml: function [ "Based on Andrew Martin's Recursive-Read." Directory [file! url!]] [Files Subdirectory] [ Files: read Directory if block? Files [ foreach File Files [ if directory? File [ path: replace/all Directory/:Subdirectory/:File "none/" "" path: replace/all path Subdirectory "" write/append %dir.xml rejoin["<folder name='" File"' path='" path "' modified='" modified? File "' size='" size? File "'>"] Subdirectory: File foreach File read Directory/:Subdirectory [ name: to-string File either find name sep[ append Files Subdirectory/:File ][ write/append %dir.xml reduce[ "<file name='" File "' path='" Directory/:Subdirectory/:File "' modified='" modified? File "' size='" size? File "'/>"] ] ] write/append %dir.xml "</folder>" ] ] ] write/append %dir.xml rejoin["</files>"] ] So anyway I was hoping to get some help on this, things I'd like help with are 1. since I'm still in the beginner phase with Rebol and just taking scripts like this for reuse, what are some most evident things I could do to include the coding of ReadDir-ToXml now that I muddied it all up? 2. Is there a simple way to make it hierarchical? If not it doesn't matter as I could write an xslt that reprocessed it no problem.

 [2/7] from: bry:itnisk at: 5-Nov-2002 18:04


>The problem there is that if I want to get something hierarchical out
of
>it, I can't. >ReadDir-ToXml: function [
<<quoted lines omitted: 25>>
> write/append %dir.xml rejoin["</files>"] > ]
Okay thinking about the problem I thought it might be easier to do with a while[not tail? Files] like the following: Address: ftp://some-ftp.site Files: read Address while [not tail? Files] [File: First Files address: either directory? File[rejoin[address / File]][ rejoin[address File]] read address Files: Next Files] Figure I'd add in the Xml generation step later, once I know I am recursively parsing the file tree. this, like anything one is unsure of, raises an error. ** Access Error: Invalid port spec: ENIdea.xml ENIdea_files/ T1/ bryanstest.xml bryanstest2.x ml copyright.htm date.jsp date2.jsp mods.xml result.html result.xml result2.html result2.xml velkommen.htm / T2/ / T2/ / T2/ / T2/ ** Where: halt-view ** Near: read address

 [3/7] from: al:bri:xtra at: 6-Nov-2002 7:36


Perhaps you could describe a desired end point given a starting point? That way people would know what you're trying to do. Here's my latest version: Rebol [ Name: 'Recursive-Read Title: "Recursive Read" File: %"Recursive Read.r" Author: "Andrew Martin" eMail: [Al--Bri--xtra--co--nz] Web: http://valley.150m.com Date: 23/September/2002 Version: 1.1.0 Purpose: {Recursively read Directory.} Category: [util 3] ] Recursive-Read: function [ "Recursively read Directory." Directory [file! url!] "The Directory to read." ] [Files Subdirectory] [ if exists? Directory [ Files: read Directory if block? Files [ foreach File Files [ if #"/" = last File [ Subdirectory: File foreach File read Directory/:Subdirectory [ append Files Subdirectory/:File ] ] ] ] Files ] ] Andrew Martin ICQ: 26227169 http://valley.150m.com/

 [4/7] from: chris:ross-gill at: 5-Nov-2002 13:45


Hi Bryan, I have some recursive reading functions on my reb site (Chris RG) in the 'rebol' folder. It's fairly easy to add actions to each file/directory. Or at http://www.ross-gill.com/r/recurse.r - Chris

 [5/7] from: bry:itnisk at: 6-Nov-2002 12:51


Andrew Martin Wrote:
>Perhaps you could describe a desired end point given a starting point?
Sorry I've been sort of sick the last two days, so my posts are not the most clear headed(more congested actually) We have a minimal xml format for describing directory structures, I was trying to see if I could build up this format via Rebol. The format would be <folder> <file/> <file/> <folder> <file/> <file/> </folder> </folder> given a directory structure of Folder File File Folder File File however trying to work with Recursive-Read(haven't tried that new version yet) I get <folder> <file/> <file/> </folder> <folder> <file/> <file/> </folder> now it's not too important of a thing because I can get the path of directory I'm in so I end up with <folder path=ftp://some.site.com/T1/> <file path="ftp://some.site.com/T1/enIdea.xml"/> .... and so forth which means I can easily post-process with xslt and put subfolders underneath proper parent folders, but this would be in the class of an ugly hack, an admission of not knowing Rebol well enough yet. Of course I don't know Rebol well enough yet, and I am not averse to making ugly hacks to get something done, but as this is just my own testing the language I figure it would be better to find the solution rather than ugly-hack it. :) But anyway I'm gonna go look through some of the other Recursive functions for directories, like your new version and the stuff from Christopher Ross-Gill and see if I can come to some understanding of how I should do it. Thanks.

 [6/7] from: chris:ross-gill at: 6-Nov-2002 9:41


Hi Bryan,
> The format would be > <folder>
<<quoted lines omitted: 5>>
> </folder> > </folder>
[spaces added] The function below will create the hierarchy as desired. Usage: write %file-list.xml recurse-xml %folder/ I've used a value local to the function 'xml, and I'm not sure if that's the right approach. The function and xml could also be wrapped in an object - ctx-xmldir: context [ xml: "" set 'recurse-xml func [...][...] ] - Chris -- REBOL [] use [xml][ recurse-xml: func [ { Creates an XML string listing all files and sub-directories of provided folder. } dir [file! url!] "The base-level directory (works for ftp too!)" /sub "For recursive use, do not set." /indent ind [string!] "Indent (used internally)" /local files [block!] ][ dir: dirize dir if not indent [ind: copy ""] if not sub [ xml: copy {<?xml version="1.0" encoding="iso-8859-1"?>} repend xml [newline build-tag reduce ['files 'path dir]] ] files: sort read dir foreach file files [ either dir? join dir file [ repend xml [ newline ind build-tag reduce [ 'folder 'name file 'path dir 'modified modified? join dir file ] ] recurse-xml/sub/indent join dir file append copy ind " " repend xml [newline ind </folder>] ][ repend xml [ newline ind build-tag reduce [ 'file 'name file 'path dir 'modified modified? join dir file 'size size? join dir file to-word "/" ] ] ] ] if not sub [return repend xml [newline </files>]] ] ]

 [7/7] from: bry:itnisk at: 6-Nov-2002 16:05


>The function below will create the hierarchy as desired.
Thanks now I just have to take it home and figure out how it works. :)

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted