User Tools

Site Tools


manipulating_20filenames_20and_20pathnames

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:

      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. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
manipulating_20filenames_20and_20pathnames.txt · Last modified: 2024/01/05 00:22 by 127.0.0.1