# BBC BASIC Programmers' Reference

### Site Tools

manipulating_20filenames_20and_20pathnames

# Differences

This shows you the differences between two versions of the page.

 manipulating_20filenames_20and_20pathnames [2018/03/31 13:19]127.0.0.1 external edit manipulating_20filenames_20and_20pathnames [2018/04/17 17:32] (current)tbest3112 Added syntax highlighting 2018/04/17 17:32 tbest3112 Added syntax highlighting2018/03/31 13:19 external edit 2018/04/17 17:32 tbest3112 Added syntax highlighting2018/03/31 13:19 external edit Line 4: Line 4: ===== Root directory ===== ===== 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()** 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("​C:​\Documents and Settings\jgh\Admin"​) ​ ​*| ​-> "​C:"​ - FNf_root("​\\datastore\Tools\Admin"​) ​              ​-> "​\\datastore"​ + FNf_root("​H:​\Apps"​) ​                             ​*| ​-> "​H:"​ - FNf_root("​X:"​) ​                                   -> "​X:"​ + FNf_root("​\\datastore\Tools\Admin"​) ​             ​*| ​-> "​\\datastore"​ + FNf_root("​X:"​) ​                                  *| -> "​X:"​ + ​ You could then do, for example:​\\ ​ You could then do, for example:​\\ ​ + PROCcopyfiles(FNf_root(indir\$)+"​\Data","​D:​\Backup","​*.*"​) PROCcopyfiles(FNf_root(indir\$)+"​\Data","​D:​\Backup","​*.*"​) + ​ \\ \\ ===== Extracting leafname ===== ===== Extracting leafname ===== **FNf_leaf()** will scan through a fully-qualified pathname to find just the leafname - the final part of the path.\\ ​ **FNf_leaf()** will scan through a fully-qualified pathname to find just the leafname - the final part of the path.\\ ​ + DEF FNf_leaf(A\$) DEF FNf_leaf(A\$) LOCAL A%:​REPEAT:​A%=INSTR(A\$,"​\"​):​IFA%:​A\$=MID\$(A\$,​A%+1) LOCAL A%:​REPEAT:​A%=INSTR(A\$,"​\"​):​IFA%:​A\$=MID\$(A\$,​A%+1) UNTILA%=0:​=A\$ UNTILA%=0:​=A\$ + ​ For example, **FNf_leaf("​H:​\Apps\Admin\Startup.exe"​)** returns **"​Startup.exe"​**.\\ \\ For example, **FNf_leaf("​H:​\Apps\Admin\Startup.exe"​)** returns **"​Startup.exe"​**.\\ \\ ===== Removing leafname ===== ===== Removing leafname ===== **FNf_path()** will remove the leafname, keeping just the pathname. For example:​\\ ​ **FNf_path()** will remove the leafname, keeping just the pathname. For example:​\\ ​ + DEF FNf_path(A\$):​IFINSTR(A\$,"​\"​)=0:​=A\$ DEF FNf_path(A\$):​IFINSTR(A\$,"​\"​)=0:​=A\$ LOCAL A%:​REPEATB%=A%:​A%=INSTR(A\$,"​\",​A%+1):​UNTILA%=0:​=LEFT\$(A\$,​B%) LOCAL A%:​REPEATB%=A%:​A%=INSTR(A\$,"​\",​A%+1):​UNTILA%=0:​=LEFT\$(A\$,​B%) + ​ This lets you use code such as:\\ This lets you use code such as:\\ + IF out\$=""​ THEN out\$=FNf_path(in\$)+"​output.txt"​ IF out\$=""​ THEN out\$=FNf_path(in\$)+"​output.txt"​ + ​ \\ \\ ===== Removing filename extension ===== ===== 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"​**).\\ ​ **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:​=""​ DEF FNf_ext(A\$):​IFINSTR(A\$,"​."​)=0:​=""​ LOCAL A%:​A%=LENA\$+1:​REPEATA%=A%-1:​UNTIL INSTR("​.:​\",​MID\$(A\$,​A%,​1)) LOCAL A%:​A%=LENA\$+1:​REPEATA%=A%-1:​UNTIL INSTR("​.:​\",​MID\$(A\$,​A%,​1)) IF MID\$(A\$,​A%,​1)="​.":​=MID\$(A\$,​A%) ELSE =""​ IF MID\$(A\$,​A%,​1)="​.":​=MID\$(A\$,​A%) ELSE =""​ + ​ You can then use this to do, for example:​\\ ​ You can then use this to do, for example:​\\ ​ + PROCBMP_toGIF(bmpfile\$,​FNf_noext(bmpfile\$)+"​.gif"​) PROCBMP_toGIF(bmpfile\$,​FNf_noext(bmpfile\$)+"​.gif"​) + ​ Note that filenames similar to **"​.htaccess"​** are seen as being all extension and no name.\\ \\ Note that filenames similar to **"​.htaccess"​** are seen as being all extension and no name.\\ \\ ===== Finding filename extension ===== ===== 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.\\ ​ **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\$ DEF FNf_noext(A\$):​IFINSTR(A\$,"​."​)=0:​=A\$ LOCAL A%:​A%=LENA\$+1:​REPEATA%=A%-1:​UNTIL INSTR("​.:​\",​MID\$(A\$,​A%,​1)) LOCAL A%:​A%=LENA\$+1:​REPEATA%=A%-1:​UNTIL INSTR("​.:​\",​MID\$(A\$,​A%,​1)) IFMID\$(A\$,​A%,​1)="​.":​=LEFT\$(A\$,​A%-1) ELSE =A\$ IFMID\$(A\$,​A%,​1)="​.":​=LEFT\$(A\$,​A%-1) ELSE =A\$ + ​ You can then use this to do, for example:​\\ ​ You can then use this to do, for example:​\\ ​ + runapp\$=FNMime_Type(FNf_ext(file\$)) runapp\$=FNMime_Type(FNf_ext(file\$)) + ​ \\ \\ ===== Ensuring full absolute pathnames ===== ===== 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.\\ ​ 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\$) DEF FNf_fullpath(path\$,​file\$) IF INSTR(file\$,":"​)<>​0 OR INSTR(file\$,"​\\"​)<>​0:​=file\$ IF INSTR(file\$,":"​)<>​0 OR INSTR(file\$,"​\\"​)<>​0:​=file\$ IF RIGHT\$(path\$,​1)<>"​\":​path\$=path\$+"​\"​ IF RIGHT\$(path\$,​1)<>"​\":​path\$=path\$+"​\"​ =path\$+file\$ =path\$+file\$ + ​ It functions as in the following examples.\\ ​ 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","​DATA"​) ​           ​*| -> "​C:​\EARS\DATA"​ - FNf_fullpath("​C:​\EARS","​\\system\backup"​) -> "​\\system\backup"​ + FNf_fullpath("​C:​\EARS","​A:​\INCOMING"​) ​    *| -> "​A:​\INCOMING"​ + FNf_fullpath("​C:​\EARS","​\\system\backup"​) ​*| -> "​\\system\backup"​ +