[Date Prev][Date Next][Thread Prev][Thread Next]
[Author Index] [Date Index] [Thread Index]
[SQR-USERS Info] [SQRUG Home Page]

Re: [sqr-users] Opening files dynamically



to assume is not necessarily a good idea.... but here goes
I assume that the number of possible files to process at any given
runtime is variable and
that you desire to process each file, one at a time, until every file
matchining *_summary.txt
is processed

If so, then below should be of some help:
!...............................................................................
begin-procedure  P500-Main
   #debugt show '%' $sqr-program '-debugt, P500-Main'
   !*   This is the main loop. We derive a list of input file(s)
   !*   to process and then execute (call) an sqr to process
   !*   each input file found in the derived list file
   evaluate $hc_interface_type
      when = 'D'
         !* it is a dblink process, so no files
         let #noftpscriptfound = 1   !* to prevent process from being
deleted
         let $Suicide_flag = 'N'     !* to prevent process from being
deleted
         do {sqrinclude}
         break
      when = 'I'
         do P9000-Get-Files-to-Process
         let #In_file_ctr = 0
         let $ToDOInput =  $In_file1_dir || $ToDOfilename
         open $ToDOInput as {todo_fileunitnbr}
             for-reading record={todo_reclen}:vary
status=#ToDO_filestat
         if #ToDO_filestat != 0
            display ' '
            show $pgm_name '-F-ERROR, cannot open files to process list
- ' $ToDOInput
            show '    file status=' #ToDI_filestat
            let #ErrorCount = #ErrorCount + 1
            display ' '
         else
            do P501-Main
         end-if
      when = 'E'
         do P600-Export
      when-other
         show $pgm_name '-F-FATAL, Interface type invalid for
{sqrinclude}'
         let #ErrorCount = #ErrorCount + 1
      end-evaluate
end-procedure


!...............................................................................
begin-procedure  P501-Main
   #debugt show '%' $sqr-program '-debugt, P501-Main'
   read {todo_fileunitnbr} into $ToDO_rcrd:{todo_reclen}
   let #end-TODOfile = #end-file
   while #end-ToDOfile = 0
      let #In_file_ctr = #In_file_ctr + 1
      let $ToDO_rcrd = rtrim($ToDO_rcrd, ' ')
      let #len = length($ToDO_rcrd)
      if $ToDOfilename = $ToDO_rcrd
         show $pgm_name '-I-MSG, skipping file list file: ' $ToDO_rcrd
      else
         Evaluate $opsys-platform
            When = 'DOS'
            When = 'UNIX'
               !the unix ls -1 or ls doesn't return a bare output
because the ls parm includes
               !the full path to the files which we must use as where
the targets are won't
               !be the default directory. So we gotta strip the file to
its bare name.
               let #str_beg = #len
               while substr($ToDO_rcrd, #str_beg, 1) <> '/' or #str_beg
<= 1
                  let #str_beg = #str_beg - 1
               end-while
               let #str_beg = #str_beg + 1
               let #str_len = #len - #str_beg + 1
               let $ToDO_rcrd = substr($ToDO_rcrd, #str_beg, #str_len)
            When = 'WINDOWS'
         End-Evaluate
         !*show $pgm_name '-I-MSG, Processing file: ' $ToDO_rcrd  ' 
(filename len=' #len edit 999 ')'
         show $pgm_name '-I-MSG, Processing file: ' $ToDO_rcrd
         let $In_filename = $In_file1_dir || $ToDO_rcrd
         let $In_file1 = $ToDO_rcrd
         let #File_exist = exists($In_filename)
         if #file_exist = 0
            do P9000-FILE-PROT-CLEANUP($In_filename)
            do P510-process-one-file
            evaluate #Loaded_flag
               when = 0   !file processed, so move/rename to subdir
                  let $MV_target = $MV_path || $prcs_process_instance
|| '-' || $ToDO_rcrd
                  show '%' $pgm_name '-I-MSG, Moving ' $In_filename '
to ' $MV_target
                  display ' '
                  let $OPSYS_command = $OPSYS_mvcmd || $In_filename ||
' ' ||$MV_target
                  Call System Using $OPSYS_command #rtn_status wait
                  if #rtn_status
                     show $pgm_name '-F-FATAL-ERROR, (P501-Main) move
of data file failed'
                     let #ErrorCount = #ErrorCount + 1
                     show '    system command:'
                     show '   ' $OPSYS_command
                  else
                     do Commit-Transaction  !* 4/5/2004,egc
                  end-if
                  !* sqr rename would work IF rename in same dir, but
t'aint what we want
                  !* let #Rtn_Cd = rename($In_filename, $MV_target)
                  !* if #Rtn_cd <> 0
                  !*    show $pgm_name '-F-ERROR, rename/move failed.
ABORTING.'
                  !*    rollback
                  !*    STOP
                  !* end-if
               When-Other !errors encountered, so leave input file
alone, no data loaded
                  show $pgm_name '-W-Warning, ' $ToDO_rcrd ' not
moved/renamed due to errors processing it!!'
                  let #ErrorCount = #ErrorCount + 1
            end-evaluate
         else
            show '**********' $pgm_name '-W-MSG, File not found or
could not be opened **********'
            show '    file name: ' $In_filename
            display ' '
         end-if
      end-if
      read {todo_fileunitnbr} into $ToDO_rcrd:{todo_reclen}
      let #end-TODOfile = #end-file
   end-while
   close {todo_fileunitnbr}
   let #file_exist = delete($ToDOInput)   !cleanup after ourselves;
delete the dir list file
end-procedure


!...............................................................................
begin-procedure P510-process-one-file
   #debugt show '%' $sqr-program '-debugt, P510-process-one-file'
   #ifdef #debugd    !*just display input file data
      open $In_filename as {localin_fileunitnbr}
           for-reading record={localin_reclen}:vary
status=#Insource_filestat
      if #Insource_filestat != 0
         show $pgm_name '-F-ERROR, could not open ' $In_sourcefile
      else
         read {localin_fileunitnbr} into
$InSource_rcrd:{localin_reclen}
         let #end-InSourcefile = #end-file
         while #end-InSourcefile = 0
            let $InSource_rcrd = rtrim($InSource_rcrd, ' ')
            show $InSource_rcrd
            read {localin_fileunitnbr} into
$InSource_rcrd:{localin_reclen}
            let #end-InSourcefile = #end-file
         end-while
         close {localin_fileunitnbr}
         show $pgm_name '-I-MSG, >>>>> all done reading ' $In_filename
         input $crap maxlen=8 'press enter to continue'
      end-if
   #else
      !*show $pgm_name '-I-MSG, stub for the call to: ' $In_sqrtocall
      !*note: forcing the "call" to be a local var proc is more
nuisance than it is
      !*      worth since we really need to share most of the var's.
      !*  So the rule now is, we define var $unit2_filename and that
MUST
      !*  be the file name used by the "called"/included sqc's
      !*do {sqrinclude} ($In_filename, #Loaded_flag)
      let $In_file2 = $In_filename
      #debugt show 'P510-just before do {sqrinclude}'
      do {sqrinclude}
   #end-if
end-procedure


and proc in hc9000.sqc custom copy lib:
!...............................................................................
begin-procedure P9000-Get-Files-to-Process
   !* performs operating system commands to derive a list
   !* of files and other file maintenance actions that are needed
   !* for financial interface processing (hcfsldgl, hcfsldap)
   #debug9 show '.....%hc9000.sqc-i-msg, P9000-Get-Files-to-Process'
   Evaluate $opsys-platform
     When = 'DOS'    !*this is not compatible with windows op system
cmds
        let $OPSYS_command = 'dir /-p/b/a-d ' || $In_file1_dir ||
$In_file1 || ' >'
        let $OPSYS_command = $OPSYS_command || $in_file1_dir ||
$ToDOfilename
        let $OPSYS_command = $OPSYS_cmdstart || $OPSYS_command
        let $MV_dir = $In_file1_dir || 'a_done'
        let $MV_path = $MV_dir || '\'
     When = 'UNIX'
        !* note: the redir >! fails if redir target not exist, so can't
be used
        let $OPSYS_command = 'ls '|| $In_file1_dir || $In_file1 || ' >
'
        let $OPSYS_command = $OPSYS_command || $in_file1_dir ||
$ToDOfilename
        let $MV_dir = $In_file1_dir || 'a_done'
        let $MV_path = $MV_dir || '/'
     When = 'WINDOWS'      !*sqr screwsup & doesn't report if nt or
95/98 in $sqr-platform
        !*dir cmd notes: /-p ensures we turn off pause after each
screen
        !*               /b display in bare format; no heading, file
size, or summary
        !*               /a-d don't display directory names; they
aren't files
        let $OPSYS_command = 'dir /-p/b/a-d ' || $In_file1_dir ||
$In_file1 || ' >'
        let $OPSYS_command = $OPSYS_command || $in_file1_dir ||
$ToDOfilename
        let $OPSYS_command = $OPSYS_cmdstart || $OPSYS_command
        let $MV_dir = $In_file1_dir || 'a_done'
        let $MV_path = $MV_dir || '\'
        !* now find or create the subdir to move processed file(s) to
     When-Other
        show $pgm_name '-F-ERROR, unsupported operating system'
        display '  RUN TERMINATED'
        rollback
        STOP
   End-Evaluate
   !* 6/2003,egc, add check for valid fully qualified file name
   let $p9000_fname = $in_file1_dir || $In_file1
   let #p9000_fnamlen = length($p9000_fname)
   let $p9000_fnamelastch = substr($p9000_fname,#p9000_fnamlen,1)
   if $p9000_fnamelastch = '/'
   or $p9000_fnamelastch = '\'
   or #p9000_fnamlen < 1
      do Rollback-Transaction
      if $prcs_process_instance <> ''
         let #prcs_run_status = #prcs_run_status_error
         let $prcs_message_parm1 = 'Improper run control parm(file)'
         let #prcs_continuejob = 0     !set to 1 if want next step in a
job to run regardless
         do Update-Prcs-Run-Status
      end-if
      show ' '
      show '   %hc9000-F-FATAL, Error: Fully qualified filename user
parm improperly entered'
      show '    Program Terminated'
      stop quiet
   end-if
   show $pgm_name '-I-MSG, Op sys command line executed'
   show '     ' $OPSYS_command
   display ' '
   Call System Using $OPSYS_command #rtn_status wait
   if #rtn_status
      show '.....%hc9000.sqc-i-msg, P9000-Get-Files-to-Process-E-ERROR,
op system cmd failed'
      show '                        #rtn_status=' #rtn_status
      show '    cmd: ' $OPSYS_command
      if $opsys-platform = 'UNIX'
         do P9000-GetFilestoProcess-UnixErr
      end-if
   end-if
   Evaluate $opsys-platform
     When = 'DOS'    !this is not compatible with windows op system
cmds
        let #Rtn_cd = exists($MV_dir)
        let $OPSYS_mkdir = $P9000_mkdir || $MV_dir
     When = 'UNIX'
        let #Rtn_cd = exists($MV_dir)
        let $OPSYS_mkdir = $P9000_mkdir || $MV_dir
     When = 'WINDOWS'      !sqr screwsup & doesn't report if nt or
95/98 in $sqr-platform
        let #Rtn_cd = exists($MV_dir)
        let $OPSYS_mkdir = $P9000_mkdir || $MV_dir
   End-Evaluate
   if #Rtn_cd <> 0 and isnull($Skip_mkdir)
      show $pgm_name '-I-MSG, creating subdirectory ' $MV_dir ' to
store input file(s) processed'
      Call System Using $OPSYS_mkdir #rtn_status wait
      if #rtn_status <> 0
         show $pgm_name '-F-ERROR, subdir create failed. Input file(s)
cannot be processed'
         show 'cmd= ' $opsys_mkdir
         show 'rtn_status= ' #rtn_status
         display '  RUN TERMINATED'
         rollback
         STOP
      end-if
   end-if
end-procedure


>>> "Vaidyanathan, Ramakrishnan (GE, Corporate,  consultant)"
<ramakrishnan.vaidyanathan@ge.com> 7/26/2006 1:55:03 PM >>>
Hi,
   In sqr, is it possible to open the files dynamically at run time?
For eg. I have 10 files ending with "summary.txt" ,say
<filename1>_summary.txt, <filename2>_summary.txt and so on..I need to
open all these summary.txt files for processing. If possible please send
me some sample code..
Thanks in Advance
Ram

_______________________________________________
sqr-users mailing list
sqr-users@sqrug.org 
http://www.sqrug.org/mailman/listinfo/sqr-users

_______________________________________________
sqr-users mailing list
sqr-users@sqrug.org
http://www.sqrug.org/mailman/listinfo/sqr-users