Tree.sb to list folder contents

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Tree.sb to list folder contents

Post 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
Last edited by Dutchman on Tue Oct 10, 2017 5:35 pm, edited 6 times in total.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Tree.sb to list folder contents

Post by rbytes »

You did a great job on this program! Can you make it delete stuff I don’t use anymore? :lol:
Attachments
2120ED2D-75E4-4FC3-BB1E-1EB74F57DD99.png
2120ED2D-75E4-4FC3-BB1E-1EB74F57DD99.png (75.77 KiB) Viewed 4501 times
The only thing that gets me down is gravity...

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Tree.sb to list folder contents

Post 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.

User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Re: Tree.sb to list folder contents

Post by Dav »

This a nice snippet of code! Works great.

- Dav

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Tree.sb to list folder contents

Post by Dutchman »

I added the variable Content.Number.
If Content.number<>0 then the names will be preceded by level-number&"|"

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Tree.sb to list folder contents

Post 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.
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Tree.sb to list folder contents

Post by Dutchman »

Does the folder exist?

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Tree.sb to list folder contents

Post 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?
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Tree.sb to list folder contents

Post by Dutchman »

I don't understand you. I can not reproduce the problem unless you give me more info.

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Tree.sb to list folder contents

Post 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.
Attachments
IMG_1767.PNG
IMG_1767.PNG (72.8 KiB) Viewed 4414 times
IMG_1766.PNG
IMG_1766.PNG (234.05 KiB) Viewed 4414 times
IMG_1765.PNG
IMG_1765.PNG (225.96 KiB) Viewed 4414 times
IMG_1764.PNG
IMG_1764.PNG (388.57 KiB) Viewed 4414 times
IMG_1763.PNG
IMG_1763.PNG (250.05 KiB) Viewed 4414 times
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

Post Reply