Menu

How Can I Dynamically Assign File Name Within COBOL app?

2021-09-14
2021-12-10
  • Steve Millman

    Steve Millman - 2021-09-14

    Hi guys,
    I have a program that has 5 keys. When i run using a certain key.. would be nice to name the output file to reflect that key. instead of the generic name in the Assign clause. Is there a way for me to reassign the file name from within the program? I sure could not find it in the manuals.
    Thank you,
    Steve

     
  • Simon Sobisch

    Simon Sobisch - 2021-09-14

    You can adjust the filename at runtime (read during OPEN) by using either a variable name in the ASSING clause (this is the common option) instead of a literal, or by using a literal and set DD_assignname in the environment, for example by SET ENVIRONMENT - but the ASSIGN TO variable-name is much more clear, especially when adjusting within the program.

     
    • Steve Millman

      Steve Millman - 2021-09-14

      Nice. That is what I will do.. Thank you!!

       
  • László Erdős

    László Erdős - 2021-09-14
     
    • Steve Millman

      Steve Millman - 2021-09-14

      That is perfect! Thank you, Laszlo!!

       
  • Vincent (Bryan) Coen

    IF, I am reading this correctly you have a ISAM file with four alternative keys defined in addition to the primary one so that you can select which one to use at any one point ?

    If so, then there is no nead to change the file name as all five key indexes are linked to the one file i.e., using BDB this creates the primary file plus one key file for each of the alternate keys.
    For example :

    Customers.dat
    Customers.dat.1
    Customers.dat.2

    thru to :
    Customers.dat.4

    The first holds the data and the primary keys and the other four hold each alternate key set.

    So doing a READ with one of the KEYS selected does the job.

    Small example:

     select  Stock-File      assign               File-11
                             access               dynamic
                             organization         indexed
                             status               Fs-Reply
                             record key           Stock-Key
                             alternate record key Stock-Abrev-Key
                             alternate record key Stock-Desc with duplicates.
    

    In WS

    *>
     fd  Stock-File.
    *>
     01  Stock-Record.
         03  Stock-Key                pic x(13).
         03  Stock-Abrev-Key          pic x(7).
         03  Stock-Suppliers-Group.
             05  Stock-Supplier-P1    pic x(7).                          *> Primary   Supplier
             05  Stock-Supplier-P2    pic x(7).                          *> Secondary Supplier
             05  Stock-Supplier-P3    pic x(7).                          *> Back Up   Supplier
         03  filler redefines Stock-Suppliers-Group.
             05  Stock-Suppliers      pic x(7)     occurs 3.  *> 41
         03  Stock-Desc               pic x(32).              *> 73
         03  Stock-Construct-Item     pic x(13).              *> 86
    

    ...... rest cut ....

    In the Proc. Div a routine in a specific F.H. (File Handler) that handles the processing for this file is called as needed with the following code within :

     aa050-Process-Read-Indexed.
    *>
    *> copy the three possible keys to main file area
    *>
         move     204 to WS-No-Paragraph.
         perform  aa045-Eval-keys.
         move     zero to Cobol-File-Status.
         if       File-Key-No = 1
                  read     Stock-File key Stock-Key       invalid key
                           move 21 to we-error fs-reply
                  end-read
                  if       fs-Reply = zero
                           move     Stock-Record to WS-Stock-Record
                           move     WS-Stock-Key to WS-File-Key
                  else
                           move     spaces     to WS-Stock-Record
                  end-if
                  go       to aa999-main-exit
         end-if.
         if       File-Key-No = 2
                  read     Stock-File key Stock-Abrev-Key invalid key
                           move 21 to we-error fs-reply
                  end-read
                  if       fs-Reply = zero
                           move     Stock-Record to WS-Stock-Record
                           move     WS-Stock-Abrev-Key to WS-File-Key
                  else
                           move     spaces to WS-Stock-Record
                  end-if
                  go       to aa999-main-exit
         end-if.
         if       File-Key-No = 3        *> can also use start, read next
                  read     Stock-File key Stock-Desc  invalid key
                           move 21 to we-error fs-reply
                  end-read
                  if       fs-Reply = zero
                           move     Stock-Record to WS-Stock-Record
                           move     WS-Stock-Desc to WS-File-Key
                  else
                           move     spaces to WS-Stock-Record
                  end-if
                  go       to aa999-main-exit
         end-if.
         move     998 to WE-Error   *> file seeks key type out of range
         move     99 to fs-reply
         go       to aa999-main-exit.
    
     
    • Anonymous

      Anonymous - 2021-09-14

      Hi Vince (or is it Bryan?),
      No.. I am good with the files themselves.. Just want the flexibility of
      assigning output a unique name depending on the key I use.
      I built a parmfile that I use for the run.. and in it I choose the Key and
      provide some starting values for the lookups, for each key.
      What I would like, ideally, is the means to say.. if (for example) I am
      reading the Transaction file by date.. my output file (currently Acct.rpt)
      might be called Acct_Date.rpt,
      and lookup by Name might be Acct_Name.rpt and so on.
      I have never seen a means of dynamically assigning a file name from within
      a program but hoped one of smart contributors to this page have done it.
      Thank you,
      Steve

      Mod edit to remove reply-to

       

      Last edit: Simon Sobisch 2021-09-14
    • Steve Millman

      Steve Millman - 2021-09-14

      Hi Vince (or is it Bryan?),
      Thought I had replied; but dont see it here. No, what I was asking about Simon and Laszlo answered. A way to dynamically assign a name to files (in this case.. based on the keys I am using.

      I have an external parmfile and in it I specify keys for the run and starting values. I can use it to pass a file name too, if I didnt want to set it within the program.

      Everything is fitting together well. Still working on the online maintenance program and working out the bugs.

      Thanks again for your help.

      Steve

       
      • Simon Sobisch

        Simon Sobisch - 2021-09-14

        Your post got to moderation as you were note signed in / answered from an address that does not match your SF user profile.
        I've found 5 spare minutes to work through the moderation que and it now is in.

         
  • Steve Millman

    Steve Millman - 2021-09-15

    Solved, thanks to input from Simon and Laszlo. Much appreciated.

     
  • Michael F Gleason

    to anonymous if you are still there.
    Here is a small program that reads 3 of 6 possible input files and writes one of two possible output files. A command line switch/argument dictate which files to read and write. Your parm file could supply the file names. The key lies in the assign clause and the associated working storage identifier it points to.
    The important parts are attached.cob. The compile list is also attached. The program has a lot of copy books, so it won't compile, but they are not relevant to your question of how to dynamically assign a file name.
    Michael

     
  • Steve Millman

    Steve Millman - 2021-12-10

    Thank you Mike. . I did get it solved some time ago; but appreciate you taking the time to respond. Hopefully it will help others who are trying to set their file names externally, eliminating the need to recompile everytime the file name might change.

     

Anonymous
Anonymous

Add attachments
Cancel