Splitting file name
Frequently it is necessary to split a full file name into path, name and extension parts. This subroutine will help you in this. You pass a full path to it and it splits the path into its components. Here FileName is the full file name to parse, Path, Name, and Ext are file path, name and extension respectively.
SUB SplitFileName(FileName$,BYREF Path$,BYREF Name$,BYREF Ext$) i&=0 p&=1 WHILE p<>0 p&=INSTR(FileName,"\",i+1) IF p<>0 THEN i=p WEND Path$=LEFT(FileName,i) s&=i p&=1 WHILE p<>0 p&=INSTR(FileName,".",i+1) IF p<>0 THEN i=p WEND IF i=s THEN i=LEN(FileName)+1 Name$=MID(FileName,s+1,i-s-1) Ext$=MID(FileName,i) END SUB
For example, if FileName is "C:\My Documents\report.doc", then SplitFileName will split it as following:
This subroutine can be used in different situations. For example, you need to open a file from one folder and save it to another. Then you need to split the file name and then merge it back with the new folder name:
SplitFileName InputFile$,Path$,Name$,Ext$ OutputFile$=OutputFolder$+Name$+Ext$
Another application is to change the file extension. If you open a file name and then need to export it to GIF, you will probably want to change its extension to "gif". Here's how to do this:
SplitFileName InputFile$,Path$,Name$,Ext$ OutputFile$=Path$+Name$+".gif"
Copyright © 2000 by Alex Vakulenko. All rights reserved.