Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scanning for multiple files in a directory

Status
Not open for further replies.

SiouxCityElvis

Programmer
Jun 6, 2003
228
US
Does anyone know if it is possible to use RMCOBOL-85 on Unix to scan a directory for 1 to many sequential files, read them all and put the original files into a different directory?
Basically, I know the directory to look for files, but I will need to read all files that will be put in there without knowing the names of the files before hand.

Thanks.
-David
 
David,

1. Are you trying to OPEN and READ files?

2. or, Are you trying to move files from one directory to another?

3. or both?

Dimandja
 
Both. And I will not know the names of the files.
I figure something like this(if it is even possible) is going to somehow require kicking off a Unix bash script.

The nice thing is that the directory will hold ONLY files that I need. No other files will be in that directory - only the ones that I will need, so there will be no need to distinguish what type of file it is.

Thanks.
-David
 
Hi David,

Try something like this:
Code:
:
Select Dir-Fil Assign To Input-Output Dir-Path
       Organization Line Sequential
       Access            Sequential
       File Status       Dir-Flst.

Select Txt-Fil Assign To Input-Output Txt-Path
       Organization Line Sequential
       Access            Sequential
       File Status       Txt-Flst.
:
Fd  Dir-Fil.
1   Dir-Rec              Pic x(...).

Fd  Txt-Fil.
1   Txt-Rec              Pic x(...).
:
Working-Storage Section.
:
1                        Pic x.
 88 End-Of-Dir               Value "Y" False "N".
1   Dir-Cmd.
    2                    Pic x(3)   Value "ls ".
    2   Your-Directory   Pic x(...).
    2                    Pic x(3)   Value " > ".
    2   Dir-Path         Pic x(...) Value "ls.out".
    2                    Pic x(12)  Value " 2>/dev/null".
1   Txt-Path             Pic x(..).
:
Procedure Division.
:
Accept/Move a Directory name to Your-Directory.
:
    Call "SYSTEM" Using Dir-Cmd
    Open Input          Dir-Fil
    Set  End-Of-Dir  To False
    Perform   Read-Dir
    Perform   Until End-Of-Dir
      Perform Process-Dir-Entry
      Perform Read-Dir
    End-Perform
    Close  Dir-Fil
    Delete File Dir-Fil
    .
Read-Dir.
    Read  Dir-Fil    At End
      Set End-Of-Dir To True
    End-Read
    .
Process-Dir-Entry.
    Move    Dir-Rec To Txt-Path
    Open    Input   Txt-Fil
    Perform Process-Txt-Fil
    Close   Txt-Fil
    .

Hope it Helps
 
theotyflos,

What do the dots represent/mean in your email above?

examples are in the Pic x definitions below. Am I supposed to just put the dots there or a number?

Fd Dir-Fil.
1 Dir-Rec Pic x(...).

1 Txt-Rec Pic x(...).
2 Dir-Path Pic x(...) Value "ls.out".
 
It's just a "placeholder". You should put an actual picture there that is applicable for you.

e.g.
Code:
  2 Dir-Rec Pic x(128).
 
Okay thanks.

Also,

Process-Dir-Entry.
Move Dir-Rec To Txt-Path
Open Input Txt-Fil
Perform Process-Txt-Fil
Close Txt-Fil

Do you have any insight as to what the Process-Txt-Fil block will do?

I'm still picking my brain on how all this is going to/supposed to work as I enter the code.
My guess is that this is just code for moving the files to a different directory and deleting the files out of the original directory.
I somehow need to know the names of each file found in the directory and to do some processing on each file.

Thanks
-David
 
Process-Txt-Fil will do ... whatever you want to:
Apparently Read it, do domething with the record and ... who knows?

What are you trying to do exactly? Do you want to process each text file or just move it to another directory?

If you just want to move it then you don't have to open it and process it. You need to call another command like similar to the Dir-Cmd, which moves the text file to the "other" directory.

Theophilos.
 
David,

Most importantly, how much COBOL do you know? Do you need a fully written and debugged program?

Otherwise, I have a feeling this could turn out to be a long thread.

Dimandja
 
David,

If your version of RM/COBOL is 7.50 or higher you could try using input pipes. I also used the RENAME internal subprogram to move the files to a different directory. Hope this helps.

-Robert Heady
Liant Software Corp.

Code:
       IDENTIFICATION DIVISION.
       PROGRAM-ID.           ELVIS-TEST.
       AUTHOR.               RAH.
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER. RM-COBOL.
       OBJECT-COMPUTER. RM-COBOL.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT INPUT-FILE ASSIGN TO INPUT, "| ls -1 input/*",
               ORGANIZATION IS LINE SEQUENTIAL,
               FILE STATUS IS IN-STATUS.

           SELECT SEQ-FILE ASSIGN TO INPUT, WS-FILENAME,
               ORGANIZATION IS LINE SEQUENTIAL,
               FILE STATUS IS SEQ-STATUS.

       DATA DIVISION.
       FILE SECTION.
       FD  INPUT-FILE    
           LABEL RECORDS ARE STANDARD.
       01  INPUT-RECORD.
           03  INPUT-DIR                    PIC X(06).
           03  INPUT-FILENAME               PIC X(14).
       FD  SEQ-FILE    
           LABEL RECORDS ARE STANDARD.
       01  SEQ-RECORD                        PIC X(80).
       WORKING-STORAGE SECTION.
       77  DUMMY                             PIC X.
       01  WS-FILENAME                       PIC X(20) VALUE SPACES.
       01  IN-STATUS                         PIC XX.
           88  IN-OK                         VALUE "00".
           88  NO-MORE-FILES                 VALUE "10".
       01  SEQ-STATUS                        PIC XX.
           88  SEQ-OK                        VALUE "00".
           88  NO-MORE-RECORDS               VALUE "10".
       01  WS-NEW-FILE.
           03 TARGET-DIR                     PIC X(07) VALUE "newdir/".
           03 TARGET-FILENAME                PIC X(14).
       PROCEDURE DIVISION.
       BEGIN.
           DISPLAY SPACE ERASE.
           OPEN INPUT INPUT-FILE.
           PERFORM UNTIL NO-MORE-FILES
               READ INPUT-FILE 
                   AT END 
                       CONTINUE
                   NOT AT END 
                       MOVE INPUT-RECORD TO WS-FILENAME
                       OPEN INPUT SEQ-FILE
                       IF SEQ-OK
                           PERFORM UNTIL NO-MORE-RECORDS
                               READ SEQ-FILE
                                   AT END 
                                       CONTINUE
                                   NOT AT END 
                                       DISPLAY WS-FILENAME, SPACE COL 0,
                                               SEQ-RECORD COL 0
                                       ACCEPT DUMMY NO BEEP
                               END-READ 
                           END-PERFORM 
                           CLOSE SEQ-FILE
                       ELSE
                          DISPLAY WS-FILENAME, SPACE, SEQ-STATUS
                       END-IF 
                END-READ 
                MOVE INPUT-FILENAME TO TARGET-FILENAME
                CALL "RENAME" USING WS-FILENAME, WS-NEW-FILE
           END-PERFORM .
           CLOSE INPUT-FILE.
           GOBACK.
       END PROGRAM           ELVIS-TEST.
[\code]
 
I don't need a fully written program.
I know enough RMCOBOL to read and process files that I know the names of.
The challenge here I see is a Linux one.
Basically, the task is this:

-Scan a directory for all files in the directory(queue directory).
-Process each file found
-Save the original files in a different directory(processed files directory) and empty out the queue directory.

The above objectives are to be kicked off from a Cobol program through a menu option. I have my menu set up. The challenge is to get my COBOL program to "scan" a directory, somehow assign all file names to COBOL variables so that I can use each file name for open for input, and then process the data in the file.

I know enough COBOL to do processing, using files, etc. But all the COBOL I've codes has been in situations where I know what the file name(s) are to be used. In this situation, files will be queued in the known directory, but their names will be unknown and there will be 1 to many files to grab and then process.

I figure I'll scan and process 1 file at a time.
-David
 
David,

You correctly said it: "scan and process 1 file at a time".

Since I see that you know exactly what you need to do, then copying code that you may not understand will simply muddy the waters further for you.

1. Write your program to accomplish the task for just one known file.

2. When this works, do it again for 1 unknown file.

3. When that works, do it for multiple unknown files.

4. When you run into a problem, post it here.

In other words, code and test one step at a time.
And a good design will make coding a lot easier.

Dimandja

 
AmarilloElvis

The examples that theotyflos and Robert Heady gave you are good enough for processing the files.
As for moving them from one directory to another either use the "rename" or use a call "SYSTEM" using cmd-string
where cmd-string is a working storage group
01 cmd-string
05 filler pic x(3) value "mv".
05 orig_file pic x(200).
05 dest_file pic x(200).
and you populate then as you need
(example
string orig_directory
"/"
orig_filename delimited by spaces
into orig_file
string dest_directory
"/"
orig_filename delimited by spaces
into dest_file
)


If your working directory is dynamic, then at the end of the first processing file you just issue the "ls" command again and do everything all over.







Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Okay. Thanks to all of you.

Figuring out how to "redirect" my "ls" statement into a flat file was a challenge I just figured out.

So, I think I will go with simply calling a bash shell from my COBOL program.
This is what I did in my shell which I will call from my main COBOL program.
I'll just set up standards for the files to all have a common extension such as .pass shown below.

I've learned that the 2>/dev/null gets rid of any errors should the *.pass files not be found(queue is empty), for example.

As I said, I'm no guru at this shell script stuff, but I know they are very useful. The ls statement below will redirect the listing of files to a flat file in another directory. At this point, the shell script will end and control will return to my COBOL program that called it.
Then, it's just a matter of having my COBOL read a file called FILELIST. I've tested running my script without any .pass files present and FILELIST is still created. So, if on my read in COBOL it finds it empty then I'll end my processing code right there.
Otherwise, I'll read the file for each record in FILELIST which will give me a file name to go read in the queue directory. After processing each file I'll delete the file, since it's already been copied into the other directory via my shell script.

After I take this approach, I may look at the approach suggested by "xyzzy (Vendor)" above, since they are coming from a Liant RMCOBOL-85 perspective(the installation of COBOL that I use is RMCOBOL-85 version 8).

Thanks.
-David

#!/bin/bash
cp *.pass /fp/data/import/processed 2>/dev/null
ls *.pass > /fp/data/FILELIST 2>/dev/null
exit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top