WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Dialog Boxes

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

AskFileNameEx UDF


This function has the same behaviour and parameters than the wil one, but returns a null string if cancel is pressed there is also one extra parameter(the 1st), for the parent dialog handle.
;udf_AskFileNameEx : Returns the filename with its path as selected by a FileOpen dialog box
;               Same as AskFileName() , but returns null string if cancel is pressed
;               instead of jumping to the :cancel label
;hdlg      : handle of parent dialog or 0 if no parent
;title     : same as AskFileName()
;directory : same as AskFileName()
;filetypes : same as AskFileName()
;dfilename : same as AskFileName()
;flag      : same as AskFileName()
;Returns   : A string containing the filename and path or tab delimited list
;            in multiple selection. If cancel is pressed returns a null string.
#definefunction udf_AskFileNameEx(hdlg,title,directory,filetypes,dfilename,flag)
	;invalid flag
	if flag<0 || flag>3 then return ''
	
	ret='' ;return value
	
	comdlg32=strcat(dirwindows(1),'comdlg32.dll')
	MAX_PATH=2048 ;same as wil function
	
	OFN_ALLOWMULTISELECT=512
	OFN_OVERWRITEPROMPT=2
	OFN_EXPLORER=524288
	OFN_FILEMUSTEXIST=4096
	OFN_PATHMUSTEXIST=2048
	
	OPENFILENAME=binaryalloc(76)
	
	bfilename=binaryalloc(MAX_PATH)
	if dfilename<>'' then binarypokestr(bfilename,0,dfilename)
	
	btitle=binaryalloc(strlen(title)+1)
	if title<>'' then binarypokestr(btitle,0,title)
	
	bdirectory=binaryalloc(strlen(directory)+1)
	if directory<>'' then binarypokestr(bdirectory,0,directory) 
	
	;make filter
	bfiletypes=binaryalloc(strlen(filetypes)+2) ;double null terminated
	if filetypes<>'' 
		boffset=0
		for x=1 to itemcount(filetypes,'|') 
			item=itemextract(x,filetypes,'|')
			itemlen=strlen(item)
			BinaryPokeStr(bfiletypes,boffset,item)
			binarypoke(bfiletypes,boffset+itemlen,0)
			boffset=boffset+itemlen+1
		next
	endif
	
	;flags
	oflags=OFN_EXPLORER|OFN_OVERWRITEPROMPT|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST
	if flag==2 then oflags=oflags|OFN_ALLOWMULTISELECT
	if flag==3 then oflags=oflags & ~OFN_OVERWRITEPROMPT

	binarypoke4(OPENFILENAME,0,76)   ;lStructSize
	binarypoke4(OPENFILENAME,4,hdlg) ;hwndOwner
	binarypoke4(OPENFILENAME,8,0)    ;hInstance
	binarypoke4(OPENFILENAME,12,intcontrol(42,bfiletypes,0,0,0)) ;lpstrFilter
;	binarypoke4(OPENFILENAME,16,0) ;lpstrCustomFilter
;	binarypoke4(OPENFILENAME,20,0) ;nMaxCustFilter
;	binarypoke4(OPENFILENAME,24,0) ;nFilterIndex
	binarypoke4(OPENFILENAME,28,intcontrol(42,bfilename,0,0,0)) ;lpstrFile
	binarypoke4(OPENFILENAME,32,MAX_PATH) ;nMaxFile
;	binarypoke4(OPENFILENAME,36,0) ;lpstrFileTitle
;	binarypoke4(OPENFILENAME,40,0) ;nMaxFileTitle
	binarypoke4(OPENFILENAME,44,intcontrol(42,bdirectory,0,0,0)) ;lpstrInitialDir
	binarypoke4(OPENFILENAME,48,intcontrol(42,btitle,0,0,0)) ;lpstrTitle
	binarypoke4(OPENFILENAME,52,oflags) ;Flags
;	binarypoke2(OPENFILENAME,56,0) ;nFileOffset
;	binarypoke2(OPENFILENAME,58,0) ;nFileExtension
;	binarypoke4(OPENFILENAME,60,0) ;lpstrDefExt
;	binarypoke4(OPENFILENAME,64,0) ;lCustData
;	binarypoke4(OPENFILENAME,68,0) ;lpfnHook
;	binarypoke4(OPENFILENAME,72,0) ;lpTemplateName
	
	if flag==1 || flag==2 then dllcall(comdlg32,long:'GetOpenFileNameA',lpbinary:OPENFILENAME)
	if flag==0 || flag==3 then dllcall(comdlg32,long:'GetSaveFileNameA',lpbinary:OPENFILENAME)

	binaryeodset(bfilename,MAX_PATH)

	if flag<>2 ;single selection
		ret=binarypeekstr(bfilename,0,MAX_PATH)
	else ;multiple selection
		dir1=binarypeekstr(bfilename,0,MAX_PATH)
		dirlen=strlen(dir1)
		
		;add backslash
		if strsub(dir1,dirlen,1)<>'\' then dir2=strcat(dir1,'\')
		
		boffset=dirlen+1
		while 1
			item=binarypeekstr(bfilename,boffset,MAX_PATH)
			if item=='' then break
			ret=iteminsert(strcat(dir2,item),-1,ret,@tab)
			boffset=boffset+strlen(item)+1
		endwhile
		
		;no items found, only 1 file was selected in multiple selection
		if boffset==dirlen+1 then ret=dir1
	endif
	
	binaryfree(OPENFILENAME)
	binaryfree(bfilename)
	binaryfree(btitle)
	binaryfree(bdirectory)
	binaryfree(bfiletypes) 
	
	return ret
#endfunction

;TEST
filetypes='Text files|*.txt|All files|*.*'
r=udf_AskFileNameEx(0,'','',filetypes,'',2)
message('ret',r)
exit

Article ID:   W16244
File Created: 2004:03:30:15:43:32
Last Updated: 2004:03:30:15:43:32