basename

The basename statement is used to extract the filename portion of a path + filename string.


Syntax

basename_varName_

basename_stringasvarName_

Details

Given a string describing the full path of a file, such as /extracted/test/mydata.csv, the basename statement is used to identify the filename (including the file extension, if any) portion of that string only. If there are no path delimiters in the string, then the original string is returned.

The basename statement supports both UNIX-style (forward slash) and Windows-style (backslash) delimiters.

When invoked as basename varName, the varName parameter must be the name of the variable containing the string to analyze. The value of the variable will be updated with the result so care should be taken to copy the original value to a new variable beforehand if the full path may be required later in the script.

As a convenience in cases where the full path needs to be retained, the result of the operation can be placed into a separate variable by using the form basename string as varName where string is the value containing the full path + filename and varName is the name of the variable to set as the result.

When invoked using basename string as varName, if a variable called varName does not exist, then it will be created, otherwise its value will be updated.

Example 1

The following script …

var path = "extracted/test/testdata.csv"
          
          # Copy the path as we'll need it later
          var file = ${path}
          
          # Note: use the NAME of the variable not the value
          basename file
          
          # The variable called 'file' now contains the result
          print The basename of the path '${path}' is '${file}'
          
          var path = "testdata.csv"
          var file = ${path}
          basename file
          
          print The basename of the path '${path}' is '${file}'

… will produce the following output:

The basename of the path 'extracted/test/testdata.csv' is 'testdata.csv'
          The basename of the path 'testdata.csv' is 'testdata.csv'

Example 2

The following script …

var path = "extracted/test/testdata.csv"
          basename ${path} as file
          print The basename of the path '${path}' is '${file}'

… will produce the following output:

The basename of the path 'extracted/test/testdata.csv' is 'testdata.csv'

How did we do?