Tree content

For finished programs
Post Reply
User avatar
Dutchman
Posts: 148
Joined: Tue Aug 06, 2019 4:47 pm
Location: Netherlands

Tree content

Post by Dutchman » Fri Sep 06, 2019 1:59 pm

The program "Tree" contains two user definedfunctions
• Function "Content(…)" writes the contents of a folder, including subDirs, to file
• Function "Info(File$)" is called by the function "Content" just after the name of 'File$' is written to file.
The parameter 'File$' contains the full path name of the file and the function can be adapted by the user in order to get additional info.
The function can be used, for example, to find a file and then print the full path name to the console.

The function "Content" operates in a recursive way.
Files found in a folder are written directly to file.
The names of the subdirs present are stored in a string (SubDirs$) for further processing in a recursive way.

Code: Select all

'Tree, by "Dutchman" Ton Nillesen
'originally coded for SmartBasic, September 2017
'
'Makes File$ with contents of folder Dir$
'Folders are printed with leading "📁"
'Additional info can be obtained by 
'adapting user defined function "Info(…)"
'
'==== User settings
Dir$="/Examples"
File$="Tree.data"
Indent$=" … " ' adds to each folder-level, e.g. " … " or "   "
'
'--- check settings
OPTION BASE 1
IF NOT DIR EXISTS Dir$ THEN 
  PRINT "Folder """+Dir$+""" not found"
  STOP
ENDIF
'
'--- Run
IF FILE EXISTS(File$) THEN FILE DELETE  File$
CALL Content(File$,Dir$,Indent$)
'
'--- Evaluate
CLS
PRINT "Scanned folder """+Dir$+"""."
PRINT "Found";Files;"files in";Dirs;"folders."
Fsize=(FILE SIZE File$)/1024
PRINT "Content written to """+File$+""". "+STR TEXT Fsize "(0.00 kb)"
'
'--- Give info
PRINT: PRINT "Largest file size is "\
	+ STR TEXT Info.MaxSize/1024 "0,?? kb"\
	+ " (new """+File$+""" excluded)"
END

'============ User defined functions ==========
DEF Info(File$)
'To be adapted by the user
'This function will be called by function 'Content(…)'
'File$ will contain the name of the file which
'just has been written to the tree.
'--- Example of usage
  MaxSize=MAX(MaxSize,FILE SIZE File$)
END DEF

DEF Content(File$,Dir$,Indent$) 
' Write contents of folder Dir$ including subDirs to File$
' Folder-names are preceded by "📁"
' Indent$ is added at each level
' Dirs and Files-count are written to .Dirs and .Files
' original version codede for SmartBasic in 2017
'----- by "Dutchman" Ton Nillesen --------
IF Leader$="" THEN Init=1 ELSE Init=0 ' for reset on exit
IF STR RIGHT(Dir$,1)<>"/" THEN Dir$+="/"
FILE WRITE File$ Leader$+"📁"+Dir$+CR$
.Dirs+=1
Leader$+=Indent$
'--- collect files and dirs
DIM F1$() F2$()
F1$=DIR FILES Dir$ : n1=SIZE OF F1$
F2$=DIR DIRS Dir$ : n2=SIZE OF F2$
'--- write files
IF n1>0 THEN
  FOR i=1 TO n1
    F$=F1$(i)
    GOSUB GetFsize
    FILE WRITE File$ leader$+F$+fsize$+CR$
    CALL Info(Dir$+F$) ' determine user-info
    .Files+=1
   NEXT
ENDIF
SubDirs$="" ' clear SubDirs for this folder
'---- process dirs
IF n2<1 THEN Done ' no SubDirs in current folder
' store subdirs in local variable SubDir$
FOR i=1 TO n2 ' store in SubDirs-string
  SubDirs$+=F2$(i)+","
NEXT
DO IF (STR LEN SubDirs$)>1
  p=STR POS(SubDirs$,",")
  F$=Dir$+STR LEFT(SubDirs$,p-1)+"/"
  SubDirs$=STR RIGHT(SubDirs$,STR LEN(SubDirs$)-p)
  CALL Content(File$,F$,Indent$)
REDO
Done:
IF Init THEN Leader$="" ' clear for additional call
RETURN ' from function
GetFsize: ' local subroutine
  '--- determine size.
  'Note: size of target File$ is not complete yet
  fsize=FILE SIZE (Dir$+F$)
  IF fsize>=1E6
    fsize$="  ("+STR TEXT (fsize/1048576 "0.000 Mb)")
  ELSE IF fsize<1E5
    fsize$=" ("+STR TEXT (fsize)+" bytes)"
  ELSE: fsize$=" ("+STR TEXT (fsize/1024 "0.00 kb)")
  ENDIF
RETURN ' from subroutine
END DEF
Output in file and on console.JPG
Output in file and on console.JPG (81.18 KiB) Viewed 111 times
It is still a long way to go

User avatar
kibernetik
Site Admin
Posts: 129
Joined: Tue Aug 06, 2019 3:03 pm

Re: Tree content

Post by kibernetik » Fri Sep 06, 2019 6:41 pm

Very interesting program.

User avatar
Dutchman
Posts: 148
Joined: Tue Aug 06, 2019 4:47 pm
Location: Netherlands

Re: Tree content

Post by Dutchman » Sun Nov 10, 2019 6:49 pm

I have extended the "content" function so that the generated tree is now sorted.
The program:

Code: Select all

'Tree, by "Dutchman" Ton Nillesen
'originally coded for SmartBasic, September 2017
'
'Makes File$ with contents of folder Dir$
'Folders are printed with leading "📁"
'Additional info can be obtained by 
'adapting user defined function "Info(…)"
'
'==== User settings
Dir$="/Examples"
File$="Tree.data"
Indent$=" … " ' adds to each folder-level, e.g. " … " or "   "
'
'--- check settings
OPTION BASE 0
IF NOT DIR EXISTS Dir$ THEN 
  PRINT "Folder """+Dir$+""" not found"
  STOP
ENDIF
'
'--- Run
IF FILE EXISTS(File$) THEN FILE DELETE  File$
CALL Content(File$,Dir$,Indent$)
'
'--- Evaluate
CLS
PRINT "Scanned folder """+Dir$+"""."
PRINT "Found";Files;"files in";Dirs;"folders."
Fsize=(FILE SIZE File$)/1024
PRINT "Content written to """+File$+""". "+STR TEXT Fsize "(0.00 kb)"
'
'--- Give info
PRINT: PRINT "Largest file size is "\
	+ STR TEXT ContentInfo.MaxSize/1024 "0.?? kb"\
	+ " (new """+File$+""" excluded)"
END

'============ User defined functions ==========
DEF ContentInfo(Dir$,File$)
'To be adapted by the user
'This function will be called by function 'Content(…)'
'File$ will contain the name of the file which
'just has been written to the tree.
'--- Example of usage
  MaxSize=MAX(MaxSize,FILE SIZE Dir$+File$)
END DEF

DEF Content(File$,Dir$,Indent$) 
' Write contents of folder Dir$ including subDirs to File$
' Folder-names are preceded by "📁"
' Indent$ is added at each level
' Dirs and Files-count are written to .Dirs and .Files
' For each file found, the user function ContentInfo(Dir$,File$) will be called
' If this function is not present then this will cause a "Syntax error"
' ContentInfo() can be used to collect info etc. about Dir$ and/or File$
' original version coded for SmartBasic in 2017
'----- by "Dutchman" Ton Nillesen --------
IF Leader$="" THEN Init=1 ELSE Init=0 ' for reset on exit
IF STR RIGHT(Dir$,1)<>"/" THEN Dir$+="/"
FILE WRITE File$ Leader$+"📁"+Dir$+CR$
.Dirs+=1
Leader$+=Indent$
'--- collect files and dirs
DIM F1$() F2$()
F1$=DIR FILES Dir$ : n1=SIZE OF F1$
F2$=DIR DIRS Dir$ : n2=SIZE OF F2$
'--- sort
IF n1>0 THEN F1$=SORTUP F1$
IF n2>0 THEN F2$=SORTUP F2$
'--- write files
IF n1>0 THEN
  FOR i=0 TO n1-1
    F$=F1$(i)
    GOSUB GetFsize
    FILE WRITE File$ leader$+F$+fsize$+CR$
    CALL ContentInfo(Dir$,F$) ' determine user-info
    .Files+=1
   NEXT
ENDIF
SubDirs$="" ' clear SubDirs for this folder
'---- process dirs
IF n2<1 THEN Done ' no SubDirs in current folder
' store subdirs in local variable SubDir$
FOR i=0 TO n2-1 ' store in SubDirs-string
  SubDirs$+=F2$(i)+","
NEXT
DO IF (STR LEN SubDirs$)>1
  p=STR POS(SubDirs$,",")
  F$=Dir$+STR LEFT(SubDirs$,p-1)+"/"
  SubDirs$=STR RIGHT(SubDirs$,STR LEN(SubDirs$)-p)
  CALL Content(File$,F$,Indent$)
REDO
Done:
IF Init THEN Leader$="" ' clear for additional call
RETURN ' from function
GetFsize: ' local subroutine
  '--- determine size.
  'Note: size of target File$ is not complete yet
  fsize=FILE SIZE (Dir$+F$)
  IF fsize>=1E6
    fsize$="  ("+STR TEXT (fsize/1048576 "0.000 Mb)")
  ELSE IF fsize<1E5
    fsize$=" ("+STR TEXT (fsize)+" bytes)"
  ELSE: fsize$=" ("+STR TEXT (fsize/1024 "0.00 kb)")
  ENDIF
RETURN ' from subroutine
END DEF
. and the resulting tree of "/Examples":

Code: Select all

📁/Examples/
 … .DS_Store (8196 bytes)
 … hello world.txt (20 bytes)
 … 📁/Examples/Basics/
 …  … comments.txt (91 bytes)
 …  … function definition.txt (174 bytes)
 …  … hello world.txt (20 bytes)
 …  … recursion.txt (144 bytes)
 …  … string length.txt (590 bytes)
 … 📁/Examples/Complex numbers/
 …  … fractal.txt (616 bytes)
 …  … simple.txt (122 bytes)
 … 📁/Examples/Console/
 …  … even or odd.txt (79 bytes)
 …  … math.txt (125 bytes)
 …  … sine.txt (66 bytes)
 …  … user input.txt (254 bytes)
 … 📁/Examples/Games/
 …  … life.txt (3061 bytes)
 … 📁/Examples/Graphics/
 …  … color moss.txt (398 bytes)
 …  … color pixels.txt (158 bytes)
 …  … color points.txt (131 bytes)
 …  … crazy font.txt (293 bytes)
 …  … dragon curve.txt (582 bytes)
 …  … hsv2rgb.txt (92 bytes)
 …  … kaleidos.txt (453 bytes)
 …  … random circles.txt (121 bytes)
 …  … random dots.txt (93 bytes)
 …  … sierpinski.txt (141 bytes)
 …  … text rotate.txt (658 bytes)
 … 📁/Examples/Interface/
 …  … button hunting.txt (103 bytes)
 …  … buttons.txt (433 bytes)
 …  … inedit.txt (391 bytes)
 …  … scribbler.txt (905 bytes)
 … 📁/Examples/Miscellaneous/
 …  … FFT + IFFT.txt (358 bytes)
 …  … Lorenz attractor.txt (1342 bytes)
 …  … barnsley fern.txt (730 bytes)
 …  … frequencies.txt (718 bytes)
 …  … rose.txt (503 bytes)
 …  … spring.txt (1244 bytes)
 …  … words counter.txt (547 bytes)
 …  … 📁/Examples/Miscellaneous/res/
 …  …  … hamlet.txt (187.24 kb)

It is still a long way to go

Post Reply