Problem
A customer asks if there is a way or if a cbl routine exists to search for filenames that contains a certain character (space in this case).
In Visual Basic it could be a simple three line program such as:
Dim result = My.Computer.FileSystem.FindInFiles("c:\\", " ", False, FileIO.SearchOption.SearchAllSubDirectories)
For Each name As String In result
ListBox1.Items.Add(name)
Next
What would be the equivalent code in Visual COBOL for both managed code and native code projects?
Resolution
For managed code COBOL the VB program above could be written as the following: (The assembly Microsoft.VisualBasic must also be added as a reference to the COBOL project)
$set ilusing"Microsoft.VisualBasic.FileIO"
$set ilusing"System.Collections.ObjectModel"
program-id. Program1 as "testfind.Program1".
data division.
working-storage section.
01 result type ReadOnlyCollection[string].
procedure division.
set result to type FileSystem::FindInFiles("c:\\", " ", false, type
Microsoft.VisualBasic.FileIO.SearchOption::SearchAllSubDirectories)
perform varying filename as string thru result
display filename
end-perform
goback.
and the native code equivalent (or close to it) should work with the CBL_DIR_SCAN_START, CBL_DIR_SCAN_READ routines like:
id division.
program-id. testscandir.
data division.
working-storage section.
01 handle usage pointer.
01 pattern.
05 pattern-length pic x(2) comp-5.
05 pattern-content pic x(100) value spaces.
05 search-attribute pic x(4) comp-5.
01 flags pic x(4) comp-5 value 2.
01 search-status pic x(4) comp-5 value zeroes.
01 scan-entry.
05 scan-attribute pic x(4) comp-5 value zeroes.
05 scan-date-stamp.
10 scan-year pic x(4) comp-5 value zeroes.
10 scan-month pic x(2) comp-5 value zeroes.
10 scan-day pic x(2) comp-5 value zeroes.
10 scan-hour pic x(2) comp-5 value zeroes.
10 scan-minute pic x(2) comp-5 value zeroes.
10 scan-second pic x(2) comp-5 value zeroes.
10 scan-millisec pic x(2) comp-5 value zeroes.
10 scan-dst pic x comp-5 value zeroes.
05 scan-size pic x(8) comp-5 value zeroes.
05 scan-name.
10 scan-max-len pic x(2) comp-5 value zeroes.
10 scan-entry-name pic x(100) value spaces.
01 test-bit pic x(4) comp-5 value 0.
01 sub1 pic 9(3) value 0.
procedure division.
move "C:\\* *" to pattern-content
perform varying sub1 from length of pattern-content by -1
until pattern-content = 0
if pattern-content(sub1:1) not = " "
move x"00" to pattern-content(sub1 1:1)
exit perform
end-if
end-perform
move 0 to pattern-length
move 0 to search-attribute
move length of scan-entry-name to scan-max-len
call "CBL_DIR_SCAN_START"
using by reference handle
pattern
by value search-attribute
flags
returning search-status
end-call
display search-status
if search-status not = 0
display "error on start"
stop run
end-if
perform until exit
call "CBL_DIR_SCAN_READ"
using by reference handle
scan-entry
returning search-status
end-call
if search-status not = 0
exit perform
else
display scan-entry-name
end-if
end-perform
call "CBL_DIR_SCAN_END"
using by reference handle
returning search-status
end-call
stop run.