1

I am working with GnuCOBOL(Using Windows) and I need to write a compiler with it.

What i am asking is - given directory path, can i modify the files inside of it using COBOL? It is important to say that you can't know the files names. You know only the path of the directory which contains them.

I1265
  • 51
  • 6
  • We have a C$LIST-DIRECTORY stock library function on the to do pile, but for now you'll want to look into opendir, readdir for POSIX and FindFirstFile FindNextFile on Windows. Or you can look at cobweb-pipes, use `pipe-open` with `dir` or `ls` and read the strings into working store. Bing on google for `gnucobol cobweb pipes` for that code. – Brian Tiffin Mar 09 '16 at 01:19
  • Which OS are you using? Do you mean "pass" or "parse"? What do you mean by "change them"? What happens when it doesn't work? What code have you attempted it with? – Bill Woodger Mar 09 '16 at 02:03

1 Answers1

2

Here is some code for POSIX systems

       identification division.
       program-id. SAMPLE.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

data   data division.
       working-storage section.
       01 dir                  usage pointer.
       01 dent                 usage pointer.
       01 dirent                                based.
          05 filler            pic x(19).   *> HERE BE DRAGONS
          05 entname           pic x(256).
          05 filler            pic x(237).
       01 sayname              pic x(256).

      *> ************************************************
code   procedure division.
       call "opendir" using
           by content z"."
           returning dir
           on exception
               display "error: no opendir found" upon syserr end-display
bail           stop run returning 1
       end-call
       if dir not equal null then
           call "readdir" using
               by value dir
               returning dent
           end-call

           perform until dent equal null
               *> set address of the based dirent and pull out the name
               set address of dirent to dent
               initialize sayname
               string entname delimited by x"00" into sayname end-string
               display trim(sayname TRAILING) end-display

               call "readdir" using
                   by value dir
                   returning dent
               end-call
           end-perform

           call "closedir" using by value dir end-call
       else
           call "perror" using by content z"" returning omitted end-call
bail       stop run returning 1
       end-if

done   goback.
       end program SAMPLE.

Originally posted to SourceForge, licensed under the GPL. Due to the assumption on sizing of dirent you'd want to duff the code over a little bit before unleashing it on the unwary.

Brian Tiffin
  • 3,978
  • 1
  • 24
  • 34