Page 2 of 2

Re: Any samples to retrieve a nested array of folders and files?

Posted: Thu Oct 06, 2022 1:41 pm
by serge_girard
HGAutomator,

Looking forward to your solution!

Serge

Re: Any samples to retrieve a nested array of folders and files?

Posted: Fri Oct 07, 2022 11:35 pm
by HGAutomator
I couldn't resist, and worked on this rather than prepare for the interview.

Unfortunately, my solution didn't work.

However, there was this gem, from the old days (1991). This does the trick.

Code: Select all

*
* ListDir.Prg:  Example of processing a complex multidimensional
*               array with a variable structure.
*
*               Craig Yellick, Alto Microcomputer Inc.
*               7107 Ohms Lane, Minneapolis, MN 55439
*               (612) 835-1080   CompuServe 76247,541
*
*               ListDir.Exe will load the file and subdirectory
*               structure of an entire drive volume and then
*               list the structure to the screen.  Optional
*               parameter allows you to specify the starting
*               subdirectory.  Default is the root directory.
*
*  Examples:    C:\> listdir list entire directory structure
*               C:\> listdir \clip5\    list c:\CLIP5\
*
*               (No error checking is performed on the parameter!)
*
*  Notes:       This program is not so notable for what it DOES
*               but HOW it does it.  There are two functions.
*               LoadDir() loads the specified directory structure
*               and returns a single array that contains the ENTIRE
*               directory structure of any arbitrary drive volume.
*               ListDir() accepts such an array and displays the
*               contents formatted clearly on the screen.  Both
*               functions make use of recursion to cleanly and
*               efficiently perform their tasks.  The code looks
*               amazingly simple -- the mark of elegance!
*
* Compiling:    Compile with the /N option because the source code
*               filename, LISTDIR.PRG, is also the name of a function.
*
* Linking:      Use of a pre-linked library (a "PLL" file) containing
*               at least CLIPPER.LIB and EXTEND.LIB will result in
*               a LISTDIR.EXE of under 10 kb.
*
* Enhancements: This code is BEGGING for you to add file size,
*               date, time and file attributes!
*
*-----------------------------------------------------------------------*
*
*  File Header:  The following are required for compiling the
*                functions found in this source code file.
*

***  IFNIL replaces a long-winded run of three lines of code
*    when testing if a parameter was omitted from a function call
*    and then supplying a default value.
*
*    "IF PARAM = NIL" is the 5.0 way to test for unused parameters,
*    much better than Summer '87's cumbersome IF TYPE("PARAM") = "U"
*    or checking PCOUNT() and keeping track of the parameter order.
*
#command IFNIL <var> := <value>  ;
      => if <var> = nil ; <var> := <value> ; endif

***  This Clipper header file contains #DEFINE commands useful
*    when dealing with the output of the DIRECTORY() function.
*
#include "DIRECTRY.CH"
STATIC All_a := {}
* end file header
*-----------------------------------------------------------------------*

function Main(path)
*
*  Entry point from DOS.
*

ifnil path := "c:\myfolder\"

@ 0,0 clear
? "Loading directory structure"
ListDir(LoadDir(path),5)

return nil

* end func Main
*-----------------------------------------------------------------------
*
function ListDir(dir_, level)
*
*  Display outer level of a subdirectory and file list
*  in the arrray passed as a parameter.  Optional level
*  number indicates how deep in to consider the "outer" level.
*
*  NOTE: ListDir() calls itself recursively.
*

local i
// ifnil level := 0
ifnil level := 5


for i= 1 to len(dir_)
  ? space(level *3)
  if valtype(dir_[i]) = "A"
    ?? dir_[i, 1]
    ?? replicate("-", 15 -len(dir_[i, 1]))
    ListDir(dir_[i, 2], level +1)
  else
    ?? dir_[i]
  endif
next i
All_a := dir_
return nil

* end func ListDir
*-----------------------------------------------------------------------
*
function LoadDir(path)
*
*  Load DOS files and subdirectories into single multidemensional array.
*  Optional parameter specifies starting subdirectory.
*  Default is root directory.
*  Returns array containing the structure.
*
*  NOTE:  LoadDir() calls itself recursively.
*

local i, name, d_, r_ := {}

ifnil path := "\"
d_ := directory(path +"*.*", "D")

for i = 1 to len(d_)
  name := d_[i, F_NAME]
  if d_[i, F_ATTR] = "D"
    if .not. (name $ "..")
      ?? "."
      aadd(r_, {name, LoadDir(path +name +"\")})
    endif
  else
    aadd(r_, name)
  endif
next i

return r_

* end func LoadDir
*-----------------------------------------------------------------------*
* eof ListDir.Prg
*______________________________________________________________


Re: Any samples to retrieve a nested array of folders and files?

Posted: Sun Oct 09, 2022 10:06 am
by nekbmm
danielmaximiliano wrote: Thu Oct 06, 2022 1:34 am Hola a todos :
hace muchisimo en el grupo trabajamos con objectos de windows y creo la solucion esta en usar "Scripting.FileSystemObject"
no ando muy memorioso pero dejo un ejemplo nose si funcional ya que no tengo HMG instalado en mi nueva computadora :?

Code: Select all

#include <hmg.ch>

Function Main
Public oFSO, oFile, oFolder
oFSO    := createobject("scripting.Filesystemobject")
oFolder := oFSO.getfolder("c:\HMG.3.5")
for each oFile in oFolder.oFiles
   ?"Name Of File : ", oFile.name
   ?"Size         : ", oFile.size
   ?"Date created : ", oFile.DateCreated
   ?"Last modified: ", oFile.DateLastModified

Return
https://hmgforum.com/viewtopic.php?f=5& ... ing#p61850
Try this:

Code: Select all

#include <hmg.ch>

Function Main
Public oFSO, oFile, oFolder, cFile, cFold

set date german 
set century on


cFOLDER := GetFolder()
If Empty(cfolder)
   Return
Endif

oFSO    := createobject("scripting.Filesystemobject")
oFolder := oFSO:getfolder(cFolder)

   SET CONSOLE OFF
   SET DEVICE TO PRINTER
   SET PRINTER TO "Lista.txt"
   SET PRINT ON
   ??"Number of files:", oFolder:Files:Count    
   ?"---------------------------------------------"
for each cFile in oFolder:Files
   ?"Name Of File : ", cFile:name
   ?"Size         : ", cFile:size
   ?"Date created : ", cFile:DateCreated
   ?"Last modified: ", cFile:DateLastModified
   ?"---------------------------------------------"
next
?
?
   ?"number of subfolders:",oFolder:SubFolders:Count
   ?"---------------------------------------------"
for each cFold in oFolder:SubFolders
   ?"Name         : ", cFold:name
   ?"---------------------------------------------"
next
   set print off
   SET DEVICE TO SCREEN
   set console on
   set printer to

hb_run("Notepad.exe lista.txt")

Return

Re: Any samples to retrieve a nested array of folders and files?

Posted: Sun Oct 09, 2022 6:09 pm
by HGAutomator
Thanks nekbmm,

But this isn't what I'm trying to achieve. This is only returning a list in a text file. It's not returning a multi-dimensional array.

Also, the list itself is returning only the first level of folders in the main folder. It's not returning any deeper levels of folders, and is only returning the files in the main folder. Below is the output of the function.

At any rate, the problem is already resolved. The routine by Craig Yellick from 1991, returns a nested multi-dimensional array that mirrors the directory structure.

Sorry if I haven't been able to articulate the requirements clearly. But Yellick's version fulfills the requirements exactly.

It made me a little nostalgic, searching the Clipper folders for clues. Since we didn't have the internet back then, many many useful routines & libraries are lost to history.


Number of files: 1
---------------------------------------------
Name Of File : notes.txt
Size : 190
Date created : 09.10.2022 14:02:19.000
Last modified: 24.01.2022 10:11:50.000
---------------------------------------------


number of subfolders: 3
---------------------------------------------
Name : Office
---------------------------------------------
Name : Other_Apps
---------------------------------------------
Name : Utilities
---------------------------------------------

Re: Any samples to retrieve a nested array of folders and files?

Posted: Sun Oct 09, 2022 6:36 pm
by serge_girard
Nekbmm,

This function maybe relatively old (1991..) but it works perfect. All you have to do is to find an elegant way to AADD to an array. Can't be very hard but can be tricky when there are many levels... How would you retrieve it? What will you do with it?

Serge