Re: File Listing

new topic     » goto parent     » topic index » view thread      » older message » newer message

Hello CK Lester,

> > I need a program that will output a file list,
> > including subfolders...
> > kinda like this:
> > Forms <dir>
> >    Texas <dir>
> >        doc1.doc
> >        doc2.doc
> >    New York <dir>
> >        doc3.doc
> >        doc4.doc
> >    California <dir>
> >        doc2.doc
> >        doc4.doc

I tried my hand at this but I see Robert has beaten me to
the punch. However, I couldn't get Robs solution to run
so I perservered  with my own and now its finally finished.
I thought of using walk_dir but decided to hard code
everything to be as customizable as possible.

This demo prints the Euphoria directory structure to a
file called Test.txt. You could just as easliy use these
routines to print to the screen or any file.

Here is the source:

include file.e

type string (object s) -- an ASCII ignorent string type
  if not sequence (s) then
    return 0
  end if
  for i = 1 to length (s) do
    if sequence (s[i]) then
      return 0
    end if
  end for
  return 1
end type

function get_file_list (sequence path) -- get the dir structure of a dir
  sequence ret
  object temp
  ret = {}
  temp = dir (path)
  if atom (temp) then
    puts (1, "Error: bad path.")
    abort (1)
  end if
  for i = 1 to length (temp) do
    if find ('d', temp [i] [D_ATTRIBUTES]) then -- it's a directory
      if not equal ( temp [i] [D_NAME] , "..") and
         not equal ( temp [i] [D_NAME] , ".") then
        ret = append (ret,
         {temp[i][D_NAME],get_file_list(path&'\\'&temp[i][D_NAME])}
         )
      end if
    else
      ret &= {temp [i] [D_NAME]}
    end if
  end for
  return ret --{{path, ret}}
end function

constant tab = 4 -- how many spaces each indention is

procedure print_list (integer fn, sequence data, integer level)
  for i = 1 to length (data) do
    puts (fn, repeat (' ', level * tab))
    if string (data [i]) then
      puts (fn, data [i] & '\n')
    elsif sequence (data [i]) then -- its a directory
      puts (fn, "\n" & repeat (' ', level * tab))
      puts (fn, data [i][1] & " <dir>\n")
      print_list (fn, data [i][2], level+1)
    end if
  end for
end procedure

procedure print_file_list (integer fn, sequence path)
  puts (fn, path & " <dir>\n\n")
  print_list (fn, get_file_list (path), 1)
end procedure

constant f = open ("test.txt", "w")

print_file_list (f, "c:\\Euphoria")

I hope this is useful,
.     __       _____  __    __    __    __    _____
.    /\ \     /\    \|\ \  /\ \  /\ \  /\ \  /\    \
.   /  \_\   /  \____\ \_\/  \_\/  \_\/  \_\/  \____\
.  /   / /  /   / ___/ |  |  / |   / /   / /\  /  __ \
. /   / /  /   / /_\ | |  | /  |  / /   / /\_\/  /_ \/
./   / /\ /   / ___/ | |  |/   | / /   / /\ \ \__  \
.\  / /__\\  / /__\  \ |    /| |/ /\  / /  \/\_\/  /
. \/_____/ \/_____/   \|___/\|___/  \/_/    \_____/
keroltarr at hotmail.com  http://geocities.com/keroltarr/
________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu