Tree.sb to list folder contents
Posted: Mon Sep 25, 2017 8:58 pm
In order to make a backup/install-program, I needed a list of the contents of a folder.
I made the following code with a function which calls itself recursively.
I made a run in my root folder, with astonishing result: 1307 files in 177 folders.
With the resulting file you can also easily inspect where you can clean up.
In the list, folder-names start with "#"
I made the following code with a function which calls itself recursively.
I made a run in my root folder, with astonishing result: 1307 files in 177 folders.
With the resulting file you can also easily inspect where you can clean up.
In the list, folder-names start with "#"
Code: Select all
'Tree.sb, by Dutchman, September 2017
'Makes File$ with contents of folder Dir$
'Folders are printed with leading "#"
'
Dir$=""
File$="contents.tree"
Indent$=" " ' adds to each folder-level, e.g. "#" or " "
OPTION BASE 1
Content.numbers=0
IF LEFT$(REVERSE$(Dir$),1)<>"/" THEN Dir$&="/"
IF NOT FILE_EXISTS(Dir$) THEN
PRINT "Folder """&Dir$&""" not found"
STOP
ENDIF
IF FILE_EXISTS(File$) THEN FILE File$ DELETE
CALL Content(File$,Dir$,Indent$)
TEXT CLEAR
PRINT "Scanned folder """&Dir$&"""."
PRINT "Found "&Files&" files in "&Dirs&" folders."
PRINT "Content written to """&File$&"""."
END
DEF Content(File$,Dir$,Indent$)
' Write contents of folder Dir$ to File$
' Folder-names are preceded by "#"
' Indent$ is added at each level
' If Content.number<>0 then names
' will be preceded by level-number&"|"
' Dirs and Files-count are written to
' .Dirs and .Files
'---------- by Dutchman, 2017 ----------
IF numbers THEN Level$=Level&"|" ELSE Level$=""
FILE File$ PRINT Tab$&Level$;"#";Dir$
.Dirs+=1
Tab$&=Indent$
level+=1
IF numbers THEN Level$=Level&"|" ELSE Level$=""
Leader$=Tab$&level$
'--- list files and dirs
Content$="" ' clear for this folder
DIR Dir$ LIST FILES F1$,n1
DIR Dir$ LIST DIRS F2$,n2
FOR i=1 TO n1
Content$&=F1$(i)&","
NEXT i
FOR i=1 TO n2
Content$&="#"&F2$(i)&","
NEXT i
'--- write contents
WHILE LEN(Content$)>1
p=INSTR(Content$,",")
F$=LEFT$(Content$,p-1)
Content$=RIGHT$(Content$,LEN(Content$)-p)
IF INSTR(F$,"#")<1 THEN ' is file
size=FILE_SIZE(Dir$&F$)
size$=" ("&STR$(size/1E6,"#.##")&"Mb)"
FILE File$ PRINT Leader$&F$&size$
.Files+=1
ELSE ' is folder
PRINT CHR$(9632);
f$=Dir$&RIGHT$(f$,LEN(F$)-1)&"/"
CALL Content(File$,f$,Indent$)
ENDIF
END WHILE
RETURN
END DEF