Extract name and directory and extension from a full filename

The script below uses the different string WinTask functions to extract the needed substrings from a full filename.

You can find the corresponding script file under Script_Examples, sub-directory of scripts.

 

'function which uses a complete filename (path and extension)

'and returns the following results

'file_dir$ : file directory

'filename$ : filename without the path

'filename_without_ext$ : filename without the extension

'file_ext$ : extension only

 

function extract_rep_file(full_filename$,file_dir$,filename$,filename_without_ext$,file_ext$)

local buffer$, exit, pos

 

'extraction of filename without path

exit=0

 

buffer$=full_filename$

repeat

pos=instr(buffer$,"\")

if pos=0 then

exit=1

else

lg=len(buffer$)

buffer$=right$(buffer$,lg-pos)

endif

until exit=1

filename$=buffer$

 

'extraction of path

pos=instr(full_filename$,buffer$)

file_dir$=left$(full_filename$,pos-2)

 

'extraction of extension and filename without the extension

pos=instr(filename$,".")

filename_without_ext$=left$(filename$,pos-1)

file_ext$=right$(filename$,len(filename$)-pos)

endfunction

 

'Call example (in a x64 version of Windows, WinTask is installed under program files (x32))

full_filename$="c:\program files\wintask\help\wintask.chm"

extract_rep_file(full_filename$,file_dir$,filename$,filename_without_ext$,file_ext$)

 

msgbox(file_dir$)

msgbox(filename$)

msgbox(filename_without_ext$)

msgbox(file_ext$)