Here is a small program that I wrote that will recursively list all folders, subfolders and files within those folders using native Visual COBOL.
Use and modify as required:
identification division. program-id. testscandir. environment division. configuration section. data division. working-storage section. 01 f-entry. 03 f-attribute pic x(4) comp-5. 03 f-date-stamp. 05 f-year pic x(4) comp-5. 05 f-month pic x(2) comp-5. 05 f-day pic x(2) comp-5. 05 f-hour pic x(2) comp-5. 05 f-minute pic x(2) comp-5. 05 f-second pic x(2) comp-5. 05 f-millisec pic x(2) comp-5. 05 f-dst pic x comp-5. 05 f-size pic x(8) comp-5. 05 f-name. 07 f-max-len pic x(2) comp-5. 07 f-entry-name pic x(1024). 01 scan-pattern. 05 pattern-len pic x(2) comp-5 value 0. 05 pattern-text pic x(256) value spaces. 01 scan-attrib pic x(4) comp-5 value 1. 01 scan-flags pic x(4) comp-5 value 2. 01 scan-status pic x(2) comp-5 value 0. 01 scan-entry. 05 entry-attrib pic x(4) comp-5. 05 entry-date-stamp. 10 e-year pic x(2) comp-5. 10 e-month pic x(2) comp-5. 10 e-dow pic x(2) comp-5. 10 e-hour pic x(2) comp-5. 10 e-minute pic x(2) comp-5. 10 e-seconds pic x(2) comp-5. 10 e-mill pic x(2) comp-5. 10 e-dst pic x comp-5. 10 e-filesize pic x(8) comp-5. 10 e-filename. 15 e-name-len pic x(2) comp-5. 15 e-name-text pic x(1024). 01 next-pattern pic x(1024) value spaces. 01 sub1 pic 9(9) value zeroes. 01 any-key pic x. local-storage section. 01 scan-handle pointer. 01 prev-pattern pic x(1024) value spaces. procedure division. display "enter starting dir name; e.g. C:\TEMP" accept next-pattern move 0 to pattern-len move 3 to scan-attrib move 2 to scan-flags call "do-scan" display "complete" accept any-key goback. entry "do-scan". move next-pattern to pattern-text perform varying sub1 from length of pattern-text by -1 until sub1 = 1 if pattern-text(sub1:1) not = " " move "\*.*" & x"00" to pattern-text(sub1 + 1:) exit perform end-if end-perform initialize f-entry move length of f-entry-name to f-max-len display "folder = " pattern-text(1:70) accept any-key call "CBL_DIR_SCAN_START" using by reference scan-handle, by reference scan-pattern, by value scan-attrib, by value scan-flags, returning scan-status end-call perform until exit move spaces to f-entry-name call "CBL_DIR_SCAN_READ" using by reference scan-handle f-entry returning scan-status end-call if scan-status = 0 perform 100-remove-quotes *> Is a folder? if f-attribute b-and 2 = 2 if f-entry-name not = "." and not = ".." move next-pattern to prev-pattern perform varying sub1 from length of f-entry-name by -1 until sub1 = 1 if next-pattern(sub1:1) not = " " move x"00" to next-pattern(sub1 + 1:) exit perform end-if end-perform string next-pattern delimited by x"00" "\" f-entry-name delimited by size into next-pattern end-string *> recursive call for next folder level call "do-scan" display "return" move prev-pattern to next-pattern else display "folder = " f-entry-name(1:70) accept any-key end-if else display "file = " f-entry-name(1:70) accept any-key end-if else call "CBL_DIR_SCAN_END" using scan-handle goback end-if end-perform. 100-remove-quotes. perform varying sub1 from 1 by 1 until sub1 = length of f-entry-name - 1 if f-entry-name(sub1:1) = '"' move f-entry-name(sub1 + 1:) to f-entry-name(sub1:) end-if end-perform. end program testscandir.