Thursday, April 19, 2018

SQR: FindFiles

Via peoplecode its very easy to read a directory and fetch filenames - leveraging delivered function "FindFiles". Now try doing the same in SQR. Not trivial at all. 

Here is what I had to do. I am running PT 8.55.x but its pretty much the same in any recent PT versions.
I essentially have to run a dir command at the Windows level get the filenames based on the wildcard and write the filenames to a file. Then read the file for the filenames. Once done, I can delete the temp file which has the list of filenames. if the dir cmd does not return anything for my wildcard search then the temp file will be empty.
The other important thing here is the WAIT parameter for the call system command. If that is not provided then the reading of the temp file does not work, as it takes the system some time to write the contents to the temp file. 

let $comspec = getenv('COMSPEC')
let $TempFile = 'C:\temp\temp_file.txt'
let $FindFiles = $comspec || ' /c dir /b \\my_network_path\share\some_directory\*.pdf > ' ||  $TempFile
call system using $FindFiles #call-status WAIT

open $TempFile as 10 for-reading record=50 #filestat
while 1 
  read 10 into $file-name:50
if $file-name != '' or #end-file
break
end-if
end-while

close 10

! delete text file
let $delCmd = $comspec || ' /c del /F /Q ' || $TempFile 
call system using $delCmd  #call-status WAIT

Way longer than a simple FindFiles command via peoplecode, but this does the trick.

3 comments:

  1. Hi Deepak,

    I try to use your code to copy files from one network directory to another but it doesn't work.
    let $TempFile = 'W:\test\direct_journal_completed'
    let $FindFiles = getenv('COMSPEC')||' /c dir /b '||'W:\test\direct_journal\CATHY_PAUL_test.txt > ' || $TempFile
    call system using $FindFiles #call-status WAIT

    Can you please help me?
    Paul
    pnersesov@chesco.org

    ReplyDelete
    Replies
    1. whatever you have written is not going copy any file, its doing a dir listing and sending the output of that to a file.

      Delete
  2. Thanks Deepak..
    You gave me a very unique thing to understand about WAIT command.. I was not aware abt this that it really takes time to finish our first task..and very important for next set of execution

    ReplyDelete