User Tools

Site Tools


manipulating_20filenames_20and_20pathnames

This is an old revision of the document!


A PCRE internal error occured. This might be caused by a faulty plugin

=====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 [[http://en.wikipedia.org/wiki/Path_(computing)#Uniform_Naming_Convention|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:\\ FNf_root("C:\Documents and Settings\jgh\Admin") -> "C:" FNf_root("H:\Apps") -> "H:" FNf_root("\\datastore\Tools\Admin") -> "\\datastore" FNf_root("X:") -> "X:" You could then do, for example:\\ PROCcopyfiles(FNf_root(indir$)+"\Data","D:\Backup","*.*") \\ ===== Extracting leafname ===== **FNf_leaf()** will scan through a fully-qualified pathname to find just the leafname - the final part of the path.\\ DEF FNf_leaf(A$) LOCAL A%:REPEAT:A%=INSTR(A$,"\"):IFA%:A$=MID$(A$,A%+1) UNTILA%=0:=A$ For example, **FNf_leaf("H:\Apps\Admin\Startup.exe")** returns **"Startup.exe"**.\\ \\ ===== Removing leafname ===== **FNf_path()** will remove the leafname, keeping just the pathname. For example:\\ DEF FNf_path(A$):IFINSTR(A$,"\")=0:=A$ LOCAL A%:REPEATB%=A%:A%=INSTR(A$,"\",A%+1):UNTILA%=0:=LEFT$(A$,B%) This lets you use code such as:\\ IF out$="" THEN out$=FNf_path(in$)+"output.txt" \\ ===== Removing filename extension ===== **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"**).\\ 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 ="" You can then use this to do, for example:\\ PROCBMP_toGIF(bmpfile$,FNf_noext(bmpfile$)+".gif") Note that filenames similar to **".htaccess"** are seen as being all extension and no name.\\ \\ ===== Finding filename extension ===== **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.\\ 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$ You can then use this to do, for example:\\ runapp$=FNMime_Type(FNf_ext(file$)) \\ ===== 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.\\ DEF FNf_fullpath(path$,file$) IF INSTR(file$,":")<>0 OR INSTR(file$,"\\")<>0:=file$ IF RIGHT$(path$,1)<>"\":path$=path$+"\" =path$+file$ It functions as in the following examples.\\ FNf_fullpath("C:\EARS","DATA") -> "C:\EARS\DATA" FNf_fullpath("C:\EARS","A:\INCOMING") -> "A:\INCOMING" FNf_fullpath("C:\EARS","\\system\backup") -> "\\system\backup"

This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
manipulating_20filenames_20and_20pathnames.1522502368.txt.gz · Last modified: 2018/03/31 14:19 by 127.0.0.1