Call System Command Example (SQR)

Example on calling a system command in SQR.

As a bonus, this system command does more than just run your average system command.  This example does a directory listing for the path in $pathname, for anything ending with .log.  It puts the content of the directory listing into a file called directory_listing.txt.  We then open this directory_listing.txt and process each file found.  This is very useful if you have an inbound folder for one or more files, and you don’t know what filenames will be found.

Calling the System Command

!Format the Command we are going to run.
#ifdef NT !Windows
    let $COMSPEC = GETENV('COMSPEC')
    show '$COMSPEC = ' $COMSPEC
    let $cmd = $COMSPEC || ' /c ' || 'dir ' || $pathname || '*.log > ' || $pathname || 'directory_listing.txt /b'
#else !Unix/Linux
    let $cmd = 'ls ' || $pathname || '*.log > ' || $pathname || 'directory_listing.txt'
#end-if

!Show and Run the Command
show '$cmd = ' $cmd
call system using $cmd #status wait
show '#status = ' #status

Open and Read the File

!Open the Directory Listing txt file.
let $full_filename = $pathname || 'directory_listing.filelisting'
open $full_filename as 1 for-reading record=250 status=#fileStatus
show '#fileStatus = ' #fileStatus

while 1
    Read 1 into $Filename_To_Open:250
    if #end-file = 1
        break
    end-if
    show 'Filename is ' $Filename_To_Open
    do Open-File
    do Process-File
    !Rename on the filename, perhaps making the extension be .archive 
    !  or moving the file to an archive or worked directory.
    do Rename-File
    do Close-File
end-while

 

↑ Back to top