Absent Member.
Absent Member.

Listing a Folder's contents

Jump to solution

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

1 Solution

Accepted Solutions
13 Replies
Vice Admiral
Vice Admiral

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

Absent Member.
Absent Member.

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

Micro Focus Expert
Micro Focus Expert

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.

Absent Member.
Absent Member.

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

Absent Member.
Absent Member.

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.

Micro Focus Expert
Micro Focus Expert

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 +                  



compute search-attributes =                  


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

Absent Member.
Absent Member.

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            


     ** 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              


              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


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

      move zero to dirflags.      

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

      call 'CBL_DIR_SCAN_START' using                            







     ** 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




     ** End the program.

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



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

          move spaces to name-text.      

     ** Do the read.          

      call 'CBL_DIR_SCAN_READ' using                            





      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.  



      end program SCANDIR.

Micro Focus Expert
Micro Focus Expert

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

   find-file + find-directory                    

Absent Member.
Absent Member.

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

Micro Focus Expert
Micro Focus Expert

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.

The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.