Page 1 of 3

Tree.sb to list folder contents

Posted: Mon Sep 25, 2017 8:58 pm
by Dutchman
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 "#"

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

Re: Tree.sb to list folder contents

Posted: Mon Sep 25, 2017 9:45 pm
by rbytes
You did a great job on this program! Can you make it delete stuff I don’t use anymore? :lol:

Re: Tree.sb to list folder contents

Posted: Tue Sep 26, 2017 9:59 am
by Dutchman
The indentation on folder-level was fixed as a number of spaces.
I have changed that to the function-parameter 'Indent$'.
That opens the possibility to use a character, e.g. "#" which simplifies the reading of the folder-level.

Re: Tree.sb to list folder contents

Posted: Wed Sep 27, 2017 1:18 am
by Dav
This a nice snippet of code! Works great.

- Dav

Re: Tree.sb to list folder contents

Posted: Wed Sep 27, 2017 8:12 am
by Dutchman
I added the variable Content.Number.
If Content.number<>0 then the names will be preceded by level-number&"|"

Re: Tree.sb to list folder contents

Posted: Wed Sep 27, 2017 8:38 pm
by GeorgeMcGinn
I am getting a runtime error here:
'--- 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
The line I am getting the error is "DIR Dir$ LIST FILES F1$,n1"

George.

Re: Tree.sb to list folder contents

Posted: Thu Sep 28, 2017 7:31 am
by Dutchman
Does the folder exist?

Re: Tree.sb to list folder contents

Posted: Fri Sep 29, 2017 11:43 pm
by GeorgeMcGinn
Yes it does as it is failing where it is reading from a file of directories it creates.

I look at the output it produced, but I'm betting it has to o with a filetype, not a missing file. That does not make sense.


Dutchman wrote:
Thu Sep 28, 2017 7:31 am
Does the folder exist?

Re: Tree.sb to list folder contents

Posted: Sat Sep 30, 2017 6:51 am
by Dutchman
I don't understand you. I can not reproduce the problem unless you give me more info.

Re: Tree.sb to list folder contents

Posted: Sat Oct 07, 2017 2:21 pm
by GeorgeMcGinn
I get the file with a list of all the directories, sub directories, and file names, but it fails half way in. I did this on my root directory and I have a lot of programs.

I can't put the list here or even give it to you without having to remove a bunch of them as I have written many research programs where the program names will give away what I have been working on and I have signed a non disclosure letter.

Also, I have apps that I am working on that I do not wish to let everyone know about. (Two will be eventually for users here, but I am not making announcements until I am close to delivering.

All I can tell you is that since I have close to a thousand files in the IDE, i am suspecting a size issue, either with the IDE abnormally ending your program or your code isn't handling very large directory tree structures, and since I am in the middle of writing some code on the Hercules Z390 Mainframe emulator, I don't have the time to debug, but I will give you the screen images that I am getting.
Dutchman wrote:
Sat Sep 30, 2017 6:51 am
I don't understand you. I can not reproduce the problem unless you give me more info.