! Main for OpenFile, SpiltPathqq, FindFileqq

Program FileOps
use dfwin
use dflib

!implicit none

! Define types for structure "ofn"
!character*(*),parameter :: filter_spec ='*.*'C
character(max_path) :: file_spec = ""C
character(max_path) lpstrInitialDir ,lpstrFilter, lpstrTitle, &
					lpstrDefExt, lpstrFile
integer nFilterIndex
character(max_path) CurrentDirectory; integer istat
integer SelectFile, status
integer lenCurr

integer l1

! SplitPathqq:
character(3) drive
character(256) dir, name
character(4) ext
integer is

! FullPathqq:
character($maxpath) full_path

! Result file:
character($maxpath) Result_File, Result_Name*100

! Console Title:
character(max_path) Input_File

character(max_path) window_title
character(max_path) appl_title, appl_name*120

character(100) folder, text*200



lpstrInitialDir =""C	!NULL
lpstrFilter = &
  "All files (*.*)"C//"*.*"C // &
  "Data Files (*.dat)"C//"*.dat"C// &
  "Text Files (*.txt)"C//"*.txt"C//&
  "Fortran Files"C//"*.f90;*.f"C//&
  ""C

lpstrTitle ='Select File'C
nFilterIndex =1

!lpstrDefExt ='. dat'C
!lpstrFile ="*.dat"C	!""C

!lpstrDefExt ='.f90'C
!lpstrFile ="*.f90"C

lpstrDefExt ='.*'C
lpstrFile ="*.*"C


write(*, '(a, \)') 'Introdu nume fisier de rezultate (fara extensie): '
read(*, '(a)') Result_Name

Result_File =trim(Result_Name)//'.rez'

open(1, file =Result_File, status ='unknown', form ='formatted')


istat =GetCurrentDirectory(max_path, CurrentDirectory)
if(istat >0) then
lpstrInitialDir =CurrentDirectory
endif

status =SelectFile(lpstrInitialDir ,lpstrFilter, lpstrTitle, nFilterIndex, &
					lpstrDefExt, lpstrFile)

IF(status >0) THEN

l1 =lstrlen(lpstrFile)
write(1, '(a, a)') 'Selected File: ', lpstrFile(:l1)

! get current directory
istat =GetCurrentDirectory(max_path, CurrentDirectory)
lenCurr =index(CurrentDirectory, char(0))
print '(/, a, a)', 'Current Directory is: ', CurrentDirectory(:lenCurr-1)

is =SPLITPATHQQ(lpstrFile, drive, dir, name, ext)

write(1, '(/, a, a)') 'drive: ', drive
write(1, '(a, a)') 'dir: ', dir(:is)
write(1, '(a, a)') 'name: ', trim(name)
write(1, '(a, a)') 'ext: ', ext


! SET CONSOLE TITLE:
ic =GetConsoleTitle(appl_title, 120)

is =SPLITPATHQQ (appl_title, drive, dir, appl_name, ext)
la =len_trim(appl_name)

Input_File =lpstrFile

is =SPLITPATHQQ (Input_file, drive, dir, name, ext)
ld =len_trim(dir)
ln =len_trim(name); le =len_trim(ext);

! Folder:
ldir =len_trim(dir)

! dir =c:\...\Sur_folder\Folder
! 1st scan:
is =scan(dir(:ldir), '\', back =.true.)

is2 =scan(dir(:is-1), '\', back =.true.)
folder =dir(is2+1:is-1)

lf =len_trim(folder)


!*
!write(11,*) dir, is, is2
!write(11,*) folder(:lf)
!*

! Folder\File:
text =folder(:lf)//'\'//name(:ln)//ext(:le)

! File:
!text =name(:ln)//ext(:le)

window_title =appl_name(:la)//':   '//trim(text)//''C

lret =SetConsoleTitle(window_title)


ELSE
print '(/, a)', ' ** Error / Canceled'
END IF


is =FullPathqq(name, full_path)

write(1, '(/, a, a)') 'Path from FULAPTHQQ: ', trim(full_path)

write(*, '(/, a, a)') 'Fisier de Rezultate: ', trim(Result_File)

is =FindFileqq('Openfile.f90', 'f90', full_path)

write(1, '(/, a, I3)') 'File Length: ', is
write(1, '(a, a)') 'Path from FINDFILEQQ: ', trim(full_path)



END Program FileOps
