Manipulating+filenames+and+pathnames

//by Jonathan Harston//

Here are some code fragments and routines to manipulate various filenames and pathnames.

Root directory

 * FNf_root** extracts the root directory from the supplied path. You can't just look for a colon specifying a drive, as the program may have been run with a Uniform Naming Convention path (see [|Wikipedia]). Also, the path may be surrounded by quotes, which have to be taken care of.

FNf_root will return as per the following examples: code format="bb4w" FNf_root("C:\Documents and Settings\jgh\Admin")  -> "C:" FNf_root("H:\Apps")                              -> "H:" FNf_root("\\datastore\Tools\Admin")              -> "\\datastore" FNf_root("X:")                                   -> "X:" code You could then do, for example: code format="bb4w" PROCcopyfiles(FNf_root(indir$)+"\Data","D:\Backup","*.*") code

Extracting leafname
code format="bb4w" DEF FNf_leaf(A$) LOCAL A%:REPEAT:A%=INSTR(A$,"\"):IFA%:A$=MID$(A$,A%+1) UNTILA%=0:=A$ code For example, **FNf_leaf("H:\Apps\Admin\Startup.exe")** returns **"Startup.exe"**.
 * FNf_leaf** will scan through a fully-qualified pathname to find just the leafname - the final part of the path.

Removing leafname
code format="bb4w" DEF FNf_path(A$):IFINSTR(A$,"\")=0:=A$ LOCAL A%:REPEATB%=A%:A%=INSTR(A$,"\",A%+1):UNTILA%=0:=LEFT$(A$,B%) code This lets you use code such as: code format="bb4w" IF out$="" THEN out$=FNf_path(in$)+"output.txt" code
 * FNf_path** will remove the leafname, keeping just the pathname. For example:

Removing filename extension
code format="bb4w" DEF FNf_ext(A$):IFINSTR(A$,".")=0:="" LOCAL A%:A%=LENA$+1:REPEATA%=A%-1:UNTIL INSTR(".:\",MID$(A$,A%,1)) IF MID$(A$,A%,1)=".":=MID$(A$,A%) ELSE ="" code You can then use this to do, for example: code format="bb4w" PROCBMP_toGIF(bmpfile$,FNf_noext(bmpfile$)+".gif") code Note that filenames similar to **".htaccess"** are seen as being all extension and no name.
 * FNf_noext** will remove an extension from a pathname. Note that you cannot just remove the last four characters, as the extension is not guaranteed to be four characters (eg, ".c"), nor can you simply search for a ".", as the path may have multiple "."s in it (eg **"C:\index.dat\20090721\thumb.db"** or even **"Version1.00\data.file.txt"**).

Finding filename extension
code format="bb4w" DEF FNf_noext(A$):IFINSTR(A$,".")=0:=A$ LOCAL A%:A%=LENA$+1:REPEATA%=A%-1:UNTIL INSTR(".:\",MID$(A$,A%,1)) IFMID$(A$,A%,1)=".":=LEFT$(A$,A%-1) ELSE =A$ code You can then use this to do, for example: code format="bb4w" runapp$=FNMime_Type(FNf_ext(file$)) code
 * FNf_ext** will scan through a fully-qualified pathname to find the extension. As with **FNf_noext**, you cannot just use the last four characters.

Ensuring full absolute pathnames
It is good practice for programs to access files via fully-specified paths, yet it can also be useful for programs to be able to access certain files relative to some path, such as the directory the program is running in. **FNf_fullpath** will take a path and a filename and return the full path of a relative filename. code format="bb4w" DEF FNf_fullpath(path$,file$) IF INSTR(file$,":")<>0 OR INSTR(file$,"\\")<>0:=file$ IF RIGHT$(path$,1)<>"\":path$=path$+"\" =path$+file$ code It functions as in the following examples. code format="bb4w" FNf_fullpath("C:\EARS","DATA")           -> "C:\EARS\DATA" FNf_fullpath("C:\EARS","A:\INCOMING")    -> "A:\INCOMING" FNf_fullpath("C:\EARS","\\system\backup") -> "\\system\backup" code