function get_file, target, qdir, qprompt, default
;
;Default is to assume "target" is a file (QDIR = 1 says it is a directory);
;Default is not to ask for input
;
if (n_elements(qprompt) eq 0) then qprompt = 0
if (n_elements(qfile) eq 0) then qfile = 0
if (n_elements(default) eq 0) then default = 'last_img:'
;
if (qprompt) then begin
    print,'$(a)','$* Enter the image file name [def=last_img]: '
    infil = '' 
    read, infil 
end else begin
    infil = target
end

;  If <cr>: use last_img
;  If request ends with a ":", assume it's a logical
;  otherwise, if it doesn't contain a ".", append a "*.*"

Test_name = infil
if infil eq '' then test_name = 'last_img' else $
   if strpos(infil,':') eq strlen(infil)-1 then test_name = infil else $
   if strpos(infil,'.') eq -1 then test_name = infil + '*.*'
   
on_ioerror, None			; Set error handling -- jump to None:
files = findfile( test_name )
on_ioerror, Null			; Reset error handling

if n_elements( files ) eq 1 then ii = 0 else begin
  ii = wmenu(['Select a file (middle button to abort)', files], init=1, title=0)
  ii = ii-1
  if !err eq 2 then return,''
endelse
if ii gt n_elements( Files ) then begin
   print,' ** Invalid file request'
   return,''
endif
infil = Files(ii)

return,infil
None:
  on_ioerror, Null			; Reset error handling
  print,' **  File not found ** '
  return,''		; No file returned

end
