Filename API Functions
This document lists most useful (and most often needed) functions. Be aware, that there are many more to be found in either the class references or via the builtin class browser.
See also "File Operations" in Expecco_API.
Reference: Filename
Inhaltsverzeichnis
Construction[Bearbeiten]
- Filename homeDirectory => Filename
- return a filename representing the user's home folder
- Filename currentDirectory => Filename
- return a filename representing the current directory
- Filename tmpDirectory => Filename
- return a filename representing the directory for temporary files (typically, that is determined by a shell variable named "TMPDIR" or similar).
- aString asFilename => Filename
- given a string, make a filename instance from it. "/"-characters in aString will be replaced by "\" on Windows.
- aFilename / aString => Filename
- allows for portable construction of a path. "/"-characters in aString will be replaced by "\" on Windows.
- e.g. "Filename homeDirectory / 'myFiles' / 'foo'" or "Filename homeDirectory / 'myFiles/foo'" might generate a filename for "/usr/fritz/myFiles/foo" on Unix and "C:\Users\anton\myFiles\foo" on Windows.
Queries[Bearbeiten]
- aFilename exists
- aFilename.exists() [JS]
- Returns true if the file exists.
- Example:
'data.txt' asFilename exists
=> false
- Example:
- aFilename isReadable
- aFilename isWritable
- aFilename isExecutable
- Returns a boolean.
Notice that "isExecutable" has a special meaning for folders (ie. can be visited - refer to your Unix/Windows operating system manual).
- Returns a boolean.
- Example:
'data.txt' asFilename isReadable
=> false
- Example:
- aFilename fileSize
- Returns the file's size (in bytes). 0 if it does not exist (or is empty).
- Example:
'data.txt' asFilename fileSize
=> 0
- Example:
- aFilename modificationTime
- aFilename creationTime
- aFilename accessTime
- Returns the time of the last modification, initial creation and last access of the file.
- Notice that Unix operating systems do not record a file's creationTime (in contrast to Windows). There, nil will be returned for creationTime.
- Example:
'/etc' asFilename modificationTime
=> 2018-07-27 11:37:20'c:\tmp\test.dat' asFilename modificationTime
=> 2010-01-27 22:37:20
- Example:
Queries - Name & Path[Bearbeiten]
- aFilename baseName
- aFilename.baseName() [JS]
- Returns the filename's base name. This is the name without directory path - i.e. the file's name within its containing directory.
- Examples:
'/etc/foo.txt' asFilename baseName
- => 'foo.txt'
- Examples:
'c:\foo\bar\bla.txt.dat' asFilename baseName
- => 'bla.txt.dat'
'foo' asFilename baseName
- => 'foo'
- aFilename directory
- aFilename directoryName
- aFilename.directory() [JS]
- aFilename.directoryName() [JS]
- Returns the filename's containing directory either as a new filename instance (directory) or as string (directoryName). This is the directory part of the name.
- Examples:
'/etc/foo.txt' asFilename directoryName
- => '/etc'
- Examples:
'c:\foo\bar\bla.txt.dat' asFilename directoryName
- => 'c:\foo\bar'
'foo' asFilename directoryName
=> '.'
'c:\foo\bar\bla.txt.dat' asFilename directory directory
- => Filename('c:\foo')
'/foo/bar/bla.txt.dat' asFilename directory directory
- => Filename('/foo')
- aFilename suffix
- aFilename.suffix() [JS]
- Returns the filename's suffix, also called its filename extension. This is the part of the name after the last "." (period), except for the special case where the filename starts with a period.
- Examples:
'foo.txt' asFilename suffix
- => 'txt'
- Examples:
'foo.txt.dat' asFilename suffix
- => 'dat'
'foo' asFilename suffix
- => ' ' (<- an empty string)
'.cvsignore' asFilename suffix
- => ' ' (<- an empty string)
- aFilename hasSuffix: suffixString
- aFilename.hasSuffix(suffixString) [JS]
- aFilename hasSuffix: suffixString caseSensitive: aBoolean
- aFilename.hasSuffix_caseSensitive(suffixString, aBoolean) [JS]
- Returns true if the filename's suffix is the same as suffixString. The first variant (without casesensitive argument) cares for the operating system's behavior (i.e. insensitive on Windows, but case sensitive on Unix); the second variant compares as defined by the boolean argument.
- Examples:
'foo.txt' asFilename hasSuffix:'txt'
- => true
- Examples:
'foo.txt.DAT' asFilename hasSuffix:'dat'
- => true on Windows, false on Unix
'foo.txt.DAT' asFilename hasSuffix:'dat' caseSensitive:false
- => false
- aFilename withoutSuffix
- aFilename.withoutSuffix() [JS]
- Returns the filename without filename extension as filename instance.
- That is: it returns the name before the last "." (period), except for the special case, where the name starts with a period.
- Examples:
'/etc/foo.txt' asFilename withoutSuffix
- => '/etc/foo'
- Examples:
'c:\foo\bar\bla.txt.dat' asFilename withoutSuffix
- => Filename('c:\foo\bar\bla.txt')
'foo.exe' asFilename withoutSuffix
- => Filename('foo')
'.cvsignore' asFilename withoutSuffix
- => Filename('.cvsignore')
- aFilename withSuffix: newSuffix
- aFilename.withSuffix( newSuffix ) [JS]
- This returns a new filename instance with a different filename extension after the last "." (period).
- Examples:
'/etc/foo.txt' asFilename withSuffix:dat
- => Filename('/etc/foo.dat')
- Examples:
'c:\foo\bar\bla.txt.dat' asFilename withSuffix:'bbb'
- => Filename('c:\foo\bar\bla.txt.bbb')
'foo.exe' asFilename withSuffix:'dat'
- => Filename('foo.dat')
'foo' asFilename withSuffix:'dat'
- => Filename('foo.dat')
- aFilename separator
- Returns the directory separator, as a character.
- Example:
'/foo' asFilename separator
- => $\ (on Windows)
- => $/ (on Unix)
Queries - Contents[Bearbeiten]
- aFilename mimeTypeFromName
- Tries to guess the mime type from the file's name. Either returns a MimeType instance or nil if it cannot be guessed.
- Example:
'test.png' asFilename mimeTypeFromName
=> 'image/png'
- aFilename mimeTypeOfContents
- Tries to guess the mime type from the file's contents. Either returns a MimeType instance or nil if it cannot be guessed.
Directory Contents[Bearbeiten]
- aFilename files
- aFilename.files() [JS]
- Returns a collection of all files in a directory (folder). The returned collection will not include names of subdirectories.
- Example:
'/etc' asFilename files
- => #( ... )
- Example:
- aFilename filesMatching: multiPattern
- aFilename filesMatching: multiPattern caseSensitive: boolean
- aFilename.filesMatching(multiPattern) [JS]
- Returns a collection of all files in a directory (folder) whose name matches given GLOB multi pattern. The returned collection will not include names of subdirectories.
- The multi pattern may consist of multiple GLOB patterns, separated by semicolon.
- Examples:
'/etc' asFilename filesMatching: 'a*'
- => #( ... )
- Examples:
'/etc' asFilename filesMatching: 'a*;b*;x*'
- => #( ... )
'/tmp' asFilename filesMatching: '*.dat;*.txt'
- => #( ... )
- aFilename directories
- aFilename.directories() [JS]
- Returns a collection of all sub directories in a directory (non-recursive). The returned collection will not include names of regular files.
- Examples:
'/etc' asFilename directories
- => #( ... )
- Examples:
'c:\' asFilename directories
- => #( ... 'c:\Program Files' ...)
Streaming[Bearbeiten]
- aFilename readStream
- aFilename writeStream
- aFilename appendingWriteStream
- Return a stream for reading or writing to the file.
- See Stream API Functions on how to use streams.
- Example:
|s|
s := 'test.dat' asFilename readStream.
firstLine := s nextLine.
nextChar := s next.
nextNumber := Number readFrom:s
s close
- Example: