8.4.2 basename command (extracts a file name from a path)
Syntax
basename character-string[suffix]
Description
This command extracts a file name character string from a character string that conforms to the path naming conventions and then outputs it to the standard output.
If a suffix (a string of any characters) is specified, the command deletes the character string that matches the specified suffix from the end of the extracted character string.
The rules for extracting a file name character string are as follows:
Of the elements separated by directory separators in the specified character string, the command extracts the rightmost element.
In UNIX, a forward slash (/) is treated as a directory separator. In Windows, the / and the backslash (\) are both treated as directory separators.
In Windows, colons (:) following a drive letter are treated as element separators.
Multiple consecutive directory separators are treated as a single directory separator.
If the specified character string ends with a directory separator, the command extracts the file name character string without the last directory separator.
If the specified character string contains no directory separator, the command extracts the specified character string as is.
If the specified character string consists of only directory separators, the command extracts the directory separators.
Arguments
- character-string
Specifies the character string from which a file name is to be extracted.
- suffix
Specifies the suffix to be deleted from the end of the extracted file name. If the he following conditions are satisfied, the command outputs the extracted file name character string as is:
The suffix does not match the end of the file name character string.
The specified suffix is the same as the extracted file name character string.
Return code
Return code |
Meaning |
---|---|
0 |
Normal termination |
1 |
Termination with an error |
Notes
This command has no options. Any option that is specified in the argument is treated as part of the character string or suffix that is to be subject to the extraction of a file name character string.
Examples
Extract file name character strings from path names.
Example 1:
C:\TEMP>%ADSH_OSCMD_DIR%\basename E:\dir001\file01.txt file01.txt
Example 2:
C:\TEMP>%ADSH_OSCMD_DIR%\basename /dir001 dir001
Example 3:
C:\TEMP>%ADSH_OSCMD_DIR%\basename .\file01.txt file01.txt
Example 4:
C:\TEMP>%ADSH_OSCMD_DIR%\basename E:\dir001\dir002\ dir002
Example 5:
C:\TEMP>%ADSH_OSCMD_DIR%\basename E:\ E:
Example 6:
C:\TEMP>%ADSH_OSCMD_DIR%\basename \\server01\ server01
Example 7:
C:\TEMP>%ADSH_OSCMD_DIR%\basename \\ \
Example 8:
C:\TEMP>%ADSH_OSCMD_DIR%\basename "C:\Documents and Settings\User01\My Documents" My Documents
Example 9:
C:\TEMP>%ADSH_OSCMD_DIR%\basename C:file01.txt file01.txt
Extract a file name from a path name without the extension:
C:\TEMP>%ADSH_OSCMD_DIR%\basename E:\dir001\file01.txt .txt file01