|
Post by basica on Jan 23, 2015 18:01:42 GMT 1
I thought I'd gather the files in a specific directory into a listbox.
INCLUDE "/usr/share/BaCon/hug.bac"
mainwin = WINDOW(" ", 626, 521)
list_box = LIST(604, 330) ATTACH(mainwin, list_box, 12, 128)
OPEN "/tmp" FOR DIRECTORY AS mydir GETFILE myfile$ FROM mydir REPEAT GETFILE myfile$ FROM mydir total_lines$ = total_lines$ & myfile$ & NL$
UNTIL ISFALSE(LEN(myfile$)) CLOSE DIRECTORY mydir
'SPLIT total_lines$ BY NL$ TO element$ SIZE dim 'FOR i = 1 TO dim 'TEXT(list_box, element$[i]) 'NEXT PRINT total_lines$ DISPLAY
The "PRINT total_lines$" works, but if the commented code above for SPLIT, etc. is used to put the files into the listbox, it doesn't work.
Have I missed something, in thinking this should work?
Regards, basica
|
|
|
Post by Pjot on Jan 23, 2015 20:22:21 GMT 1
Hi basica,
When an array is created with SPLIT then the dimension ranges from 0 to dim-1. So the code should look like this:
SPLIT total_lines$ BY NL$ TO element$ SIZE dim FOR i = 0 TO dim-1 TEXT(list_box, element$[i]) NEXT
If you want to start the array ranges with 1 then use 'OPTION BASE 1' in the beginning of your program.
HTH Peter
|
|
|
Post by basica on Jan 23, 2015 22:46:23 GMT 1
Peter,
Thank you for your reply.
OPTION BASE 1 was in effect (just wasn't in that code snippet)
I'm still puzzled -
INCLUDE "/usr/share/BaCon/hug.bac" OPTION BASE 1 OPEN "/tmp" FOR DIRECTORY AS mydir GETFILE myfile$ FROM mydir REPEAT GETFILE myfile$ FROM mydir total_lines$ = total_lines$ & myfile$ & NL$
UNTIL ISFALSE(LEN(myfile$)) CLOSE DIRECTORY mydir
mainwin = WINDOW(" ", 626, 521)
list_box = LIST(604, 330) ATTACH(mainwin, list_box, 12, 128)
SPLIT total_lines$ BY NL$ TO element$ SIZE dim
FOR i = 1 TO dim ' TEXT(list_box, element$[i]) : ' does not fill the listbox TEXT(list_box, "somefile.txt") : ' works and fills the listbox (dim number of times) PRINT element$[i] : ' prints out the filenames NEXT
DISPLAY
It seems that "element$" is not moving to the listbox, yet it prints ok
Regards, basica
|
|
|
Post by vovchik on Jan 24, 2015 0:49:11 GMT 1
Dear basica,
Yep, I am experienceing the same phenomenon. I think something weird has crept into LIST.
With kind regards, vovchik
|
|
|
Post by basica on Jan 24, 2015 2:10:49 GMT 1
vovchik, Thanks for giving this a try. I was just going to do a work-around using - total_lines$ = EXEC$("ls /tmp") and SPLIT total_lines$ BY " " TO element$ SIZE dim which works fine. Then still trying to make the original method work, I commented out the "total_lines$ = EXEC$("ls /tmp")" line and compiled. Wow, it worked. So I had stumbled on the solution because of not changing the SPLIT line back to NL$. My guess is that the two dots as the first file confuses something. Add a NL$ but split by " " (now why didn't I think of that Regards, basica
|
|
|
Post by Pjot on Jan 24, 2015 7:24:47 GMT 1
Hi basica, You have to know that passing an empty string to TEXT will empty the list box. So, your original code works with a slight modification: INCLUDE "hug.bac"
OPTION BASE 1
OPEN "/tmp" FOR DIRECTORY AS mydir
GETFILE myfile$ FROM mydir
REPEAT GETFILE myfile$ FROM mydir total_lines$ = total_lines$ & myfile$ & NL$ UNTIL ISFALSE(LEN(myfile$))
CLOSE DIRECTORY mydir
mainwin = WINDOW(" ", 626, 521)
list_box = LIST(604, 330) ATTACH(mainwin, list_box, 12, 128)
SPLIT total_lines$ BY NL$ TO element$ SIZE dim
FOR i = 1 TO dim IF LEN(element$[i]) THEN TEXT(list_box, element$[i]) NEXT
DISPLAY
The GETFILE statement will return an empty string if there are no files left. That means, that your 'total_lines' contains empty strings separated by NL$ at the end of the string. Therefore, the SPLIT will return members in element$ which are empty. Therefore, the TEXT method will cleanup the list. So, another way of solving this issue is making sure that SPLIT does not return empty results. For this, you can use OPTION COLLAPSE: INCLUDE "hug.bac"
OPTION BASE 1 OPTION COLLAPSE TRUE
OPEN "/tmp" FOR DIRECTORY AS mydir
GETFILE myfile$ FROM mydir
REPEAT GETFILE myfile$ FROM mydir total_lines$ = total_lines$ & myfile$ & NL$ UNTIL ISFALSE(LEN(myfile$))
CLOSE DIRECTORY mydir
mainwin = WINDOW(" ", 626, 521)
list_box = LIST(604, 330) ATTACH(mainwin, list_box, 12, 128)
SPLIT total_lines$ BY NL$ TO element$ SIZE dim
FOR i = 1 TO dim TEXT(list_box, element$[i]) NEXT
DISPLAY
HTH Peter
|
|
|
Post by vovchik on Jan 24, 2015 11:14:05 GMT 1
Dear Peter, Thanks. With kind regards, vovchik
|
|
|
Post by basica on Jan 24, 2015 16:58:33 GMT 1
Peter,
You amaze me with your grasp of all these Bacon aspects...... which explains very exactly what happened.
My thanks, basica (OPTION COLLAPSE TRUE seems just right)
|
|
|
Post by greco558 on Jan 27, 2015 17:29:33 GMT 1
Hi Basica, I changed your program to fill list box right in REPEAT UNTIL loop. Also added entry field so you can type the name of the directory you would like to list thats in your home directory. BR greco558 Attachments:dirlist.bac (700 B)
|
|
|
Post by basica on Jan 28, 2015 1:32:02 GMT 1
greco558,
Good idea. No point in aggregating and splitting when it can be done in the loop.
Thanks, basica
|
|
|
Post by greco558 on Jun 30, 2015 13:44:59 GMT 1
Hi basica, I re-visited your listbox program for listing contents of a directory. I made some quick changes so when you start it on command line it lists the current directory like you had but added the ability to click on a sub directory in the listbox and it will switch to that directory and list contents. There is also a Back button to take you down one directory in tree and a Home button to take you to your home directory.. Just playing around with program added home, up, exit and find buttons. Added a crude file find which will print files found to terminal in background when you start program on command line >./dirgui Did not get to test it completely I am sure there are a few bugs... BR greco558 Attachments:dirgui.bac (4.8 KB)
|
|