Showing posts with label find files. Show all posts
Showing posts with label find files. Show all posts

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.