! Folder utils

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

! Folder utils

Post by Dutchman » Wed Nov 20, 2019 12:30 pm

! Folder utils {/Extensions/Files/! Folder utils}
Contains functions to operate on folders

Code: Select all

' {/Extensions/Files/! Folder utils}
[ functions to operate on folders ]
'
[–––––––––––––––––––––––– Functions ––––––––––––––––––––––––
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$
–––
End of function description –––––––––––]
'
[–––––––––––––––––––––– Implementation ––––––––––––––––––––––]
DEF Content(File$,Dir$,Indent$)  ' by Ton Nillesen, November 2019
' 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
# Content test.bb is a test and demo program
It is still a long way to go

Post Reply