Re: Any samples to retrieve a nested array of folders and files?
Posted: Thu Oct 06, 2022 1:41 pm
HGAutomator,
Looking forward to your solution!
Serge
Looking forward to your solution!
Serge
Exclusive forum for HMG, a Free / Open Source xBase WIN32/64 Bits / GUI Development System
http://mail.hmgforum.com/
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
*______________________________________________________________
Try this: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![]()
https://hmgforum.com/viewtopic.php?f=5& ... ing#p61850Code: 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
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