Skip to main content

I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

Why don't you use Managed Cobol? The .Net Framework function System.IO.Directory.GetFiles gives you what you want.

Freundliche Gürsse

Werner Lanter


I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

Thanks for the info

I do not use .Net stuff

Never have been trained for that

I have a mainframe Batch/CICS/CA-DATACOM/Masterpiece background and I find the MF VC NATIVE COBOL to be almost the same as the mainframe COBOL (in some ways it's better)

I can see this product has a huge number of options and it's very capable but I am not working so I don't need to use those things

But I will have a dabble around and if it's 'easy' to do I can try it


I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

You can use the routines CBL_DIR_SCAN_START, CBL_DIR_SCAN_READ and CBL_DIR_SCAN_END to read through a directory listing of files.

There is an example under CBL_DIR_SCAN_READ in the docs.


I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

Thanks Chris

I found community.microfocus.com/.../10363.aspx


I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

Additional info

I got it going this morning and it works really well

Superfast actually

I started with Jerry's code from community.microfocus.com/.../10363.aspx

And I re-wrote it to suit my style e.g. I use MOVE instead of SET - MOVE is faster I think

And I used Chris's input and I also read KarlRainer's comments and made my own version from those 3 inputs

I delete files, not copy them, at the moment

It will delete 121 small files, one at a time, in way less than one second

I read the pre-determined folder and then decide whether to issue a DELETE or not based on the actual filename's prefix

It doesn't put the deleted files in the Recycle Bin yet (and I do want that feature) so I will add a step to COPY into there just before the DELETE

I didn't code the CBL_DIR_SCAN_END step and it doesn't matter in my case because I'm only doing one scan

Thanks for the help with this


I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

This sample return folder content: documentation.microfocus.com/.../HRCLRHCALL7I.html

Is there a way to get folder and sub-folders content by using CBL_DIR_SCAN_START, CBL_DIR_SCAN_READ or any other way? I need to get a file list in all sub-folders. I am using ED 2.2 Micro Focus dialect and running ES 2.2.


I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

You should be able to do this simply by changing the flag used in the CBL_DIR_SCAN_START from:

compute search-attributes =                  

  find-file                  

  find-directory                  

  find-neither.        

to

compute search-attributes =                  

  find-directory

or you could check the flag on the CBL_DIR_SCAN_READ for the return value to check if it is a folder.


I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

Hi Chris,

i want to list files in subfolders not only subfolders. Here is mu code from sample:

      copy "cblproto.cpy".

      identification division.

      program-id. SCANDIR.

      environment division.

      configuration section.

      data division.

      working-storage section.

      01  .

          05 os-info          cblt-os-info-params.

     * Variables for CBL_DIR_SCAN_BEGIN      

     * Some used by other CBL_DIR_SCAN_ routines.      

     * The handle.

      01 dir-handle     pointer.      

     * The pattern.  I chose a null terminator instead of      

     * specified length.      

      01 dir-name-pattern.          

          10 dir-name-pattern-length     cblt-x2-comp5 value zero.

          10 dir-name-pattern-text       pic x(2048).      

     * The terminator.      

      01 pattern-terminator         pic x value low-values.      

     * Search attributes.      

      01 search-attributes     cblt-x4-comp5 value zero.          

          78 find-file         value 1.          

          78 find-directory    value 2.          

          78 find-neither      value 4.      

     * Flags      

      01 dirflags             cblt-os-flags.          

          78 escape-seq       value 1.          

          78 wildcards        value 2.      

      01 search-status        pic xx   comp-5.      

     * Variables for CBL_DIR_SCAN_READ      

     * The entry.      

      01 entry-data.          

          10 entry-data-attribute cblt-x4-comp5.          

          10 entry-data-date-stamp.              

              20 stamp-year   cblt-x4-comp5.

              20 stamp-month  cblt-x2-comp5.          

              20 stamp-day    cblt-x2-comp5.              

              20 stamp-hour   cblt-x2-comp5.              

              20 stamp-minute cblt-x2-comp5.              

              20 stamp-sec    cblt-x2-comp5.              

              20 stamp-msec   cblt-x2-comp5.              

              20 stamp-dst    cblt-x1-comp5.              

              20 stamp-size   cblt-x8-comp5.          

          10 entry-data-name.              

              20 name-length  cblt-x2-comp5 value 50.              

              20 name-text    pic x(50).      

     * Variables for CBL_DIR_SCAN_END      

     * No additional data items required.      

     * Command line argument data area      

      01 arg-pos              pic 9 value 1.      

      01 arg-text             pic x(2048) value spaces.      

     * Variables to make program Unix/Windows universal      

      01 default-path pic x(70).          

          78 win-path  value 'D:\\Tutorials\\JCL'.

          78 unix-path value '/var'.      

     * Parameter block for CBL_GET_OS_INFO, ignoring all but OS type      

      01 osinfo.          

          10 osinfo-length pic x(2) comp-x value 23.          

          10 os-type pic x comp-x.          

          10 osinfo-junk pic x(25).      

     * OTHER VARIABLES      

      01 attrib-work          pic x    comp-5.      

      01 attrib-text          pic x(10).      

      01 nonblank-len         pic 9(9).      

      01 terminator-position  pic 9(9).      

      01 return-val           cblt-rtncode.                

      procedure division.

     * call 'CBL_GET_OS_INFO' using os-info

     * display cblte-osi-os-type      upon console                    

     ** Find out whether this is Unix or Windows,      

     ** and set default path appropriately.          

      call 'CBL_GET_OS_INFO' using osinfo    

                      returning return-val.          

      if os-type = 128 or os-type = 129      

     ** Unix

      move unix-path to default-path            

          else      

     ** Windows, OS/2, or DOS.              

      move win-path to default-path.      

     ** Check for directory specification on command line.          

      display arg-pos upon argument-number.          

      accept arg-text from argument-value.          

          if arg-text = spaces              

               move default-path to dir-name-pattern-text              

          else              

              move arg-text to dir-name-pattern-text.      

     ** Find the nonblank length and append string terminator.          

      perform varying nonblank-len              

          from function length(dir-name-pattern-text)              

              by -1 until                  

          dir-name-pattern-text(nonblank-len:1) <> space

           or                  

          nonblank-len = 1.

          add 1 to nonblank-len giving terminator-position.          

      move pattern-terminator              

        to dir-name-pattern-text(terminator-position:1).      

     ** Set desired search attributes by name.          

      compute search-attributes =                  

     *    find-file                  

          find-directory                    

     *    find-neither.          

      move zero to dirflags.      

     ** Start the scan -- similar to opening a file.          

      call 'CBL_DIR_SCAN_START' using                            

                         dir-handle                            

                         dir-name-pattern                            

                         search-attributes                            

                         dirflags                                      

                               returning                            

                         search-status.      

     ** Read to end, similar to reading a file.          

      perform walk-dir thru walk-dir-exit until                  

                          search-status <> zero.      

     ** At end or upon error, end the scan, similar to closing a file.

      call 'CBL_DIR_SCAN_END' using

                       dir-handle

                           returning

                       search-status.      

     ** End the program.

      display 'That''s all!' upon console.

      goback.      

      walk-dir.      

     ** Initialize name-text; function does not right-pad to length.

          move spaces to name-text.      

     ** Do the read.          

      call 'CBL_DIR_SCAN_READ' using                            

                        dir-handle                            

                        entry-data                                    

                             returning                            

                        search-status.          

      if search-status <> zero exit paragraph.      

     ** Remove all but the two rightmost bits of result.          

      move function MOD(entry-data-attribute, 3) to attrib-work.      

     ** Format result display.          

      evaluate attrib-work              

          when 1 move 'File' to attrib-text              

          when 2 move 'Directory' to attrib-text              

          when other move '???' to attrib-text.              

      display 'Next entry is: '                      

          attrib-text ' ' name-text(1:name-length) upon console.

       display  name-text upon console.  

      walk-dir-exit.

          EXIT.

      end program SCANDIR.


I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

If you wish to find both folders and files then you should set flags to 3:

   find-file find-directory                    


I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

I am able to list files and folders, i not able to list files and folders recursively. Fo example i have C:\\dir_A\\dir_B\\file_X and i set dir-name-pattern to C:\\dir_A  i want to get dir_B and file_X, but since file_X is in subfolder dir_B, i only get dir_B and file_X is invisible.  

In Powershell it would be Get-ChildItem -Path C:\\dir_A –Recurse


I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

That you cannot do using these calls. You would have to do one folder level at a time and then for each subfolder you would have to do another call to cbl_dir_scan_start and read thru to end, etc. etc in order to list its set of files.


I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

OK, thanks for confirmation. Would it be possible to invoke Powershell and get files list back to my COBOL program?


I am looking for but cannot find a function that will return the contents of a folder (i.e. the filenames that are in the folder) back to my NATIVE COBOL program

I have looked through the File Handling Library Routines and none of those seem appropriate

The MS-DOS DIR command does this - is there a VC equivalent to the old DIR command?

I have MF VC P/Edit 2.2 with V/Studio 2012 on Windows 8.1

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.