extract.filename 

File:  jmfuns.rda

 

extract.filename extracts the file name from a string that indicates its path + file name.  It can take a vector as input; in this case, it returns the vector of file names without the paths.  By default, all filenames are returned in the same case as the input; by default the extension is included with the filename.  If the input is a path that does not end in a filename, then extract.filename returns NA. 

 

function (char.vec, lower = FALSE, No.Names = TRUE, extension = TRUE)

 

 

ARGUMENTS:

 

Set lower = TRUE to convert all outputted characters to lower case.

 

Set No.Names = FALSE to return a vector of outputs that is named by the inputted path + file name vector.

 

Set extension = FALSE to return the filename without an extension.

 

Note that extract.filename uses a heuristic to decide whether a path that ends in a string that contains a period "." denotes a folder or a file name.  For example,

   extract.filename( "e:/cb/cs.str" )

   [1] NA

This call to extract.filename returns NA because the directory (folder) "e:/cb/cs.str" exists on my computer. 

   extract.filename( "e:/cb/cs.xyz" )

   [1] "cs.xyz"

This call to extract.filename returns "cs.xyz" because the directory "e:/cb/cs.xyz" does not exist on my computer.  Because  "cs.xyz" does not exist as a directory, extract.filename assumes that it is a file that is not a directory.  But note also that:

   extract.filename( "e:/cb/cs.xyz/" )

   [1] NA

This call to extract.filename returns NA because the final "/" indicates that cs.xyz is a folder, not a file.  If some folders contain periods in their names, it is better to follow all such folders with a final "/" to avoid confusion with the possibility that they may name files that are not directories. 

 

POSSIBLE REVISION:

Consider using the basename function as part of this function.  The basename function is not quite the equivalent of the extract.filename function. 

 

The following produce identical outputs:

    basename( "e:/r/fns/ls.jm.docm" )

    [1] "ls.jm.docm"

    extract.filename( "e:/r/fns/ls.jm.docm" )

    [1] "ls.jm.docm"

 

The following do not produce identical outputs:

    basename( "e:/r/fns" )

    [1] "fns"

    extract.filename( "e:/r/fns" )

    [1] NA

 

Also,

    basename( "e:/r/fns/" )

    [1] "fns"

    extract.filename( "e:/r/fns/" )

    [1] NA

 

 

#-------------- The 'doc' bookmark highlights all of the documentation but not the function skeleton at the top of the file.  Use rdocToString macro to convert documentation to an R string variable  --------------------------------------------------------

Format for this html document:  Page Size = Tabloid:  11" x 17".  Note that Tabloid page size is available if Adobe PDF is set as the default printer, but not if the default printer is a physical printer that prints 8.5" x 11" standard paper.  Table widths = 9.5"