Filename API Functions: Unterschied zwischen den Versionen

Aus expecco Wiki (Version 2.x)
Zur Navigation springen Zur Suche springen
 
(48 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
This document lists most useful (and most often needed) 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.
Be aware, that there are many more to be found in either the class references or via the builtin [[Tools_ClassBrowser/en | class browser]].


See also [[Expecco_API/en#File_Operations | "File Operations"]] in [[Expecco_API]].
See also [[Expecco_API/en#File_Operations | "File Operations"]] in [[Expecco_API]].
Zeile 9: Zeile 9:


=== Construction ===
=== Construction ===
''aString'' '''asFilename'''
:'''Filename''' '''homeDirectory''' => Filename
::return a filename representing the user's home folder


'''Filename''' '''homeDirectory'''
:'''Filename''' '''currentDirectory''' => Filename
::return a filename representing the current directory


'''Filename''' '''currentDirectory'''
:'''Filename''' '''tmpDirectory''' => Filename
::return a filename representing the directory for temporary files (typically, that is determined by a shell variable named "TMPDIR" or similar).


'''Filename''' '''tmpDirectory'''
:''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 ===
=== Queries ===


===== exists =====
:''aFilename'' '''exists'''
:''aFilename''.'''exists'''()   [JS]
Returns true if the file exists.
::Returns true if the file exists.
::Example:
:::<code>'data.txt' asFilename exists</code><br> => false


''aFilename'' '''exists'''
:''aFilename'' '''isReadable'''
:''aFilename'' '''isWritable'''
:''aFilename'' '''isExecutable'''
::Returns a boolean.<br>Notice that "isExecutable" has a special meaning for folders (ie. ''can be visited'' - refer to your Unix/Windows operating system manual).
::Example:
''aFilename''.'''exists'''() [JS]
:::<code>'data.txt' asFilename isReadable</code><br> => false


:''aFilename'' '''fileSize'''
Example:
::Returns the file's size (in bytes). 0 if it does not exist (or is empty).
'data.txt' asFilename exists
=> false
::Example:
:::<code>'data.txt' asFilename fileSize</code><br> => 0


:''aFilename'' '''modificationTime'''
===== isReadable / isWritable / isExecutable =====
:''aFilename'' '''creationTime'''
Returns a boolean.<br>Notice that ïsExecutable" has a special meaning for folders (refer to your Unix/Windows operating system manual).
:''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:
''aFilename'' '''isReadable'''
:::<code>'/etc' asFilename modificationTime</code><br> => 2018-07-27 11:37:20
:::<code>'c:\tmp\test.dat' asFilename modificationTime</code><br> => 2010-01-27 22:37:20
''aFilename''.'''isReadable'''() [JS]


=== Queries - Name & Path ===
Example:
'data.txt' asFilename isReadable
=> false


:''aFilename'' '''baseName'''
===== fileSize =====
:''aFilename''.'''baseName'''() [JS]
Returns the file's size (in bytes). 0 if it does not exist (or is empty).
::Returns the filename's base name. This is the name without directory path - i.e. the file's name within its containing directory.


::Examples:
''aFilename'' '''fileSize'''
:::<code>'/etc/foo.txt' asFilename baseName</code>
:::=> 'foo.txt'
:::<code>'c:\foo\bar\bla.txt.dat' asFilename baseName</code>
''aFilename''.'''fileSize'''() [JS]
:::=> 'bla.txt.dat'


:::<code>'foo' asFilename baseName</code>
Example:
:::=> 'foo'
'data.txt' asFilename fileSize
=> 0


:''aFilename'' '''directory'''
===== modificationTime / creationTime / accessTime =====
:''aFilename'' '''directoryName'''
Returns the time of the last modification, initial creation and last access of the file.
:''aFilename''.'''directory'''() [JS]
Notice that Unix operating systems do not record a file's creationTime (in contrast to Windows). There, nil will be returned for creationTime.
:''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:
''aFilename'' '''modificationTime'''
:::<code>'/etc/foo.txt' asFilename directoryName</code>
:::=> '/etc'
:::<code>'c:\foo\bar\bla.txt.dat' asFilename directoryName</code>
''aFilename''.'''modificationTime'''() [JS]
:::=> 'c:\foo\bar'

Example:
'/etc' asFilename modificationTime
=> 2018-07-27 11:37:20
'c:\tmp\test.dat' asFilename modificationTime
:::<code>'foo' asFilename directoryName</code>
:::<code>=> '.'</code>
=> 2010-01-27 22:37:20
:::<code>'c:\foo\bar\bla.txt.dat' asFilename directory directory</code>
:::=> Filename('c:\foo')</code>
:::<code>'/foo/bar/bla.txt.dat' asFilename directory directory</code>
:::=> Filename('/foo')</code>


:''aFilename'' '''suffix'''
=== Queries - Name & Path ===
:''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:
===== baseName =====
:::<code>'foo.txt' asFilename suffix</code>
Returns the filename's base name. This is the name without directory path - i.e. the file's name within its containing directory.
:::=> 'txt'
:::<code>'foo.txt.dat' asFilename suffix</code>
:::=> 'dat'
:::<code>'foo' asFilename suffix</code>
:::=> ' ' (<- an empty string)


:::<code>'.cvsignore' asFilename suffix</code>
:::=> ' ' (<- an empty string)


''aFilename'' '''baseName'''
:''aFilename'' '''hasSuffix:''' ''suffixString''
:''aFilename''.'''hasSuffix'''(''suffixString'') [JS]
''aFilename''.'''baseName'''() [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:
Example:
'/etc/foo.txt' asFilename baseName
:::<code>'foo.txt' asFilename hasSuffix:'txt'</code>
:::=> true
=> 'foo.txt'
'c:\foo\bar\bla.txt.dat' asFilename baseName
:::<code>'foo.txt.DAT' asFilename hasSuffix:'dat'</code>
:::=> true on Windows, false on Unix
=> 'bla.txt.dat'
'foo' asFilename baseName
:::<code>'foo.txt.DAT' asFilename hasSuffix:'dat' caseSensitive:false</code>
=> 'foo'
:::=> false



===== directory / directoryName =====
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.


:''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:
''aFilename'' '''directory'''
:::<code>'/etc/foo.txt' asFilename withoutSuffix</code>
:::=> '/etc/foo'
:::<code>'c:\foo\bar\bla.txt.dat' asFilename withoutSuffix</code>
''aFilename'' '''directoryName'''
:::=> Filename('c:\foo\bar\bla.txt')
:::<code>'foo.exe' asFilename withoutSuffix</code>
''aFilename''.'''directory'''() [JS]
:::=> Filename('foo')
''aFilename''.'''directoryName'''() [JS]


:::<code>'.cvsignore' asFilename withoutSuffix</code>
Example:
:::=> Filename('.cvsignore')
'/etc/foo.txt' asFilename directoryName

=> '/etc'
:''aFilename'' '''withSuffix:''' ''newSuffix''
:''aFilename''.'''withSuffix'''( ''newSuffix'' ) [JS]
'c:\foo\bar\bla.txt.dat' asFilename directoryName
::This returns a new filename instance with a different ''filename extension'' after the last "." (period).
=> 'c:\foo\bar'

::Examples:
:::<code>'/etc/foo.txt' asFilename withSuffix:dat</code>
:::=> Filename('/etc/foo.dat')</code>
'foo' asFilename directoryName
:::<code>'c:\foo\bar\bla.txt.dat' asFilename withSuffix:'bbb'</code>
:::=> Filename('c:\foo\bar\bla.txt.bbb')
=> '.'
'c:\foo\bar\bla.txt.dat' asFilename directory directory
:::<code>'foo.exe' asFilename withSuffix:'dat'</code>
=> 'c:\foo'
:::=> Filename('foo.dat')
'/foo/bar/bla.txt.dat' asFilename directory directory
:::<code>'foo' asFilename withSuffix:'dat'</code>
=> '/foo'
:::=> Filename('foo.dat')


:''aFilename'' '''separator'''
===== suffix =====
::Returns the directory separator, as a character.
Returns the filename's suffix, also called its ''filename extension''. This is the part of the name after the last "." (period).
::Example:
:::<code>'/foo' asFilename separator</code>
:::=> $\ (on Windows)
:::=> $/ (on Unix)


=== Queries - Contents ===
:''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:
:::<code>'test.png' asFilename mimeTypeFromName</code> => 'image/png'


''aFilename'' '''suffix'''
:''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.
''aFilename''.'''suffix'''() [JS]


=== Directory Contents ===
Example:
'foo.txt' asFilename suffix
=> 'txt'
'foo.txt.dat' asFilename suffix
=> 'dat'
'foo' asFilename suffix
=> '' <- an empty string


:''aFilename'' '''files'''
===== withoutSuffix =====
:
Returns the filename without ''filename extension''.
:''aFilename''.'''files'''() [JS]
<br>That is: it returns the name before the last "." (period).
::Returns a collection of all files in a directory (folder). The returned collection will not include names of subdirectories.


::Example:
:::<code>'/etc' asFilename files</code>
:::=> #( ... )


''aFilename'' '''withoutSuffix'''
''aFilename''.'''withoutSuffix'''() [JS]


:''aFilename'' '''filesMatching:''' ''multiPattern''
Example:
:
'/etc/foo.txt' asFilename withoutSuffix
:''aFilename'' '''filesMatching:''' ''multiPattern'' caseSensitive: ''boolean''
=> '/etc/foo'
:
:''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:
:::<code>'/etc' asFilename filesMatching: 'a*'</code>
:::=> #( ... )
:::<code>'/etc' asFilename filesMatching: 'a*;b*;x*'</code>
'c:\foo\bar\bla.txt.dat' asFilename withoutSuffix
:::=> #( ... )
=> 'c:\foo\bar\bla.txt'
'foo.exe' asFilename withoutSuffix
:::<code>'/tmp' asFilename filesMatching: '*.dat;*.txt'</code>
=> 'foo'
:::=> #( ... )


===== withSuffix: =====
This returns a new filename instance with a different ''filename extension'' after the last "." (period).


:''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:
''aFilename'' '''withSuffix:''' ''newSuffix''
:::<code>'/etc' asFilename directories</code>
:::=> #( ... )
''aFilename''.'''withSuffix'''( ''newSuffix'' ) [JS]


:::<code>'c:\' asFilename directories</code>
Example:
:::=> #( ... 'c:\Program Files' ...)
'/etc/foo.txt' asFilename withSuffix:dat
=> '/etc/foo.dat'
'c:\foo\bar\bla.txt.dat' asFilename withSuffix:'bbb'
=> 'c:\foo\bar\bla.txt.bbb'
'foo.exe' asFilename withSuffix:'dat'
=> 'foo.dat'
'foo' asFilename withSuffix:'dat'
=> 'foo.dat'


=== Directory Contents ===
=== Enumerating Directory Contents ===
:''aFilename'' '''directoriesDo:''' ''aOneArgBlock''
:''aFilename''.'''directoriesDo'''(''aOneArgLambdaOrFunction'') [JS]
::Enumerates the sub-directories into the argument non-recursive (i.e. calls the block/function for each direct sub-directory).


::Examples:
===== files =====
:::<code>'c:\' asFilename directoriesDo:[:eachDir | ... do something with eachDir ...]</code>
Returns a collection of all files in a directory (folder). The returned collection will not include names of subdirectories.
:::<code>"c:\".asFilename.directoriesDo( function(eachDir) { ... do something with eachDir ...} )</code> [JS]


''aFilename'' '''files'''
:''aFilename'' '''allDirectoriesDo:''' ''aOneArgBlock''
:''aFilename''.'''allDirectoriesDo'''(''aOneArgLambdaOrFunction'') [JS]
::Same as above, but recurses into sub-directories (i.e. the whole tree of directories is enumerated)
''aFilename''.'''files'''() [JS]


:''aFilename'' '''filesDo:''' ''aOneArgBlock''
Example:
:''aFilename''.'''filesDo'''(''aOneArgLambdaOrFunction'') [JS]
'/etc' asFilename files
::Enumerates the non-directories into the argument non-recursive (i.e. calls the block/function for each direct non-directory).
=> #( ... )


===== filesMatching:[caseSensitive:] =====
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 pattern may consist of multiple GLOB patterns, separated by semicolon.


''aFilename'' '''filesMatching:''' ''multiPattern''
:''aFilename'' '''filesDo:''' ''aOneArgBlock''
:''aFilename''.'''filesDo'''(''aOneArgLambdaOrFunction'') [JS]
::Enumerates the non-directories into the argument non-recursive (i.e. calls the block/function for each direct non-directory).
''aFilename'' '''filesMatching:''' ''multiPattern'' caseSensitive: ''boolean''
''aFilename''.'''filesMatching'''(''multiPattern'') [JS]


:''aFilename'' '''recursiveDirectoryContentsDo:''' ''aOneArgBlock'' '''filterForVisitingDirectories:''' ''filterOrNil''
Example:
::Recursively enumerates files and directories into ''aOneArgBlock''. Subdirectories are only processed/enumerated if ''filterOrNil'' is either nil or returns true. The processing block is called once for each relative filename.
'/etc' asFilename filesMatching: 'a*'
::Example:
=> #( ... )
::to enumerate all files and folders under a top folder, but not in folders named "*.tmp", you could write:
:::<code>'c:\myDir' asFilename recursiveDirectoryContentsDo:[:eachFile | ... ] filterForVisitingDirectories:[:dir | (dir hasSuffix:'tmp') not]</code>
'/etc' asFilename filesMatching: 'a*;b*;x*'
::to enumerate all files and folders under a top folder, but only in subfolders named "Font", you could write:
=> #( ... )
:::<code>'c:\Windows' asFilename recursiveDirectoryContentsDo:[:eachFile | ... ] filterForVisitingDirectories:[:dir | dir baseName = 'Fonts']</code>
'/tmp' asFilename filesMatching: '*.dat;*.txt'
=> #( ... )


Notice that there are many more enumeration/filter functions provided by the Filename class. Use a class browser or consult the [http://live.exept.de/ClassDoc/classDocOf:,Filename full online documentation].
===== directories =====
Returns a collection of all sub directories in a directory. The returned collection will not include names of regular files.


=== Contents===
''aFilename'' '''directories'''
The following functions read/write the whole contents of a file as one possibly big data object.
<br>You should not use them for very large files, as they might allocate/require large amounts of memory;
<br>use [[Stream_API Functions|stream functions]] instead (and process it linewise, blockwise or characterwise).
''aFilename''.'''directories'''() [JS]
:''aFilename'' '''contents'''
::Returns a collection of text-lines (i.e. an Array-like object where each element represents one line of text)
:''aFilename'' '''contentsAsString'''
::Returns a single (possibly big) string, a collection of characters
:''aFilename'' '''binaryContents'''
::Returns a single (possibly big) byte array, a collection of integers in the range 0..255


:''aFilename'' '''contents:''' ''aStringOrByteArrayOrCollectionOfLines''
Example:
::Creates (i.e. writes) the file.
'/etc' asFilename directories
::Examples:
=> #( ... )
:::<code> 'foo1' asFilename contents:'Hello world'.</code>
:::<code> 'foo2' asFilename contents:#[1 2 3 4 0xFF 0xFE 0xFD].</code>
:::<code> 'foo3' asFilename contents:#('line1' 'line2' 'line3').</code>


=== Streaming ===
=== Streaming ===


:''aFilename'' '''readStream'''
===== readStream / writeStream / appendingWriteStream =====
:''aFilename'' '''writeStream'''
Returns a stream for reading or writing to the file.
:''aFilename'' '''appendingWriteStream'''
<br>See [[Stream_API Functions|Stream API Functions]] on how to use streams.
::Return a stream for reading or writing to the file.
::See [[Stream_API Functions|Stream API Functions]] on how to use streams.

::Example:
:::<code>|s|</code>
:::<code>s := 'test.dat' asFilename readStream.</code>
:::<code>firstLine := s nextLine.</code>
:::<code>nextChar := s next.</code>
:::<code>nextNumber := Number readFrom:s</code>
:::<code>s close</code></code>


:''aFilename'' '''utf8ReadStream'''
Example:
:''aFilename'' '''utf8WriteStream'''
|s|
::Return a stream for reading or writing to the file. The stream will automatically generate/convert utf8 code sequences; i.e. the stream will not deliver/expect escape sequences, but possibly character instances with codepoints above 0xFF.
s := 'test.dat' asFilename readStream.
::Example:
firstLine := s nextLine.
::Writing as UTF8, reading binary:
nextChar := s next.
:::<code>|s bytes|</code>
nextNumber := Number readFrom:s
:::<code>s := 'test.dat' asFilename utf8WriteStream.</code>
s close
:::<code>s nextPutLine:'αβγψω⮠'.</code>
:::<code>s close.</code></code>
:::<code>bytes := 'test.dat' asFilename binaryContents.</code>
:::<code>bytes.</code>
:::
:::=> #[206 177 206 178 206 179 207 136 207 137 226 174 160 10]
:::
::Reading UTF8:
:::<code>|s text|</code>
:::<code>s := 'test.dat' asFilename utf8ReadStream.</code>
:::<code>text := s nextLine.</code>
:::<code>s close.</code></code>
:::<code>text.</code>
:::
:::=> 'αβγψω⮠' (a widestring)

Aktuelle Version vom 29. März 2025, 19:01 Uhr

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

Back to Useful API Functions

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
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).
Example:
'data.txt' asFilename isReadable
=> false
aFilename fileSize
Returns the file's size (in bytes). 0 if it does not exist (or is empty).
Example:
'data.txt' asFilename fileSize
=> 0
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

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'
'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'
'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'
'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
'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'
'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')
'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
=> #( ... )


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*'
=> #( ... )
'/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
=> #( ... )
'c:\' asFilename directories
=> #( ... 'c:\Program Files' ...)

Enumerating Directory Contents[Bearbeiten]

aFilename directoriesDo: aOneArgBlock
aFilename.directoriesDo(aOneArgLambdaOrFunction) [JS]
Enumerates the sub-directories into the argument non-recursive (i.e. calls the block/function for each direct sub-directory).
Examples:
'c:\' asFilename directoriesDo:[:eachDir | ... do something with eachDir ...]
"c:\".asFilename.directoriesDo( function(eachDir) { ... do something with eachDir ...} ) [JS]
aFilename allDirectoriesDo: aOneArgBlock
aFilename.allDirectoriesDo(aOneArgLambdaOrFunction) [JS]
Same as above, but recurses into sub-directories (i.e. the whole tree of directories is enumerated)
aFilename filesDo: aOneArgBlock
aFilename.filesDo(aOneArgLambdaOrFunction) [JS]
Enumerates the non-directories into the argument non-recursive (i.e. calls the block/function for each direct non-directory).


aFilename filesDo: aOneArgBlock
aFilename.filesDo(aOneArgLambdaOrFunction) [JS]
Enumerates the non-directories into the argument non-recursive (i.e. calls the block/function for each direct non-directory).
aFilename recursiveDirectoryContentsDo: aOneArgBlock filterForVisitingDirectories: filterOrNil
Recursively enumerates files and directories into aOneArgBlock. Subdirectories are only processed/enumerated if filterOrNil is either nil or returns true. The processing block is called once for each relative filename.
Example:
to enumerate all files and folders under a top folder, but not in folders named "*.tmp", you could write:
'c:\myDir' asFilename recursiveDirectoryContentsDo:[:eachFile | ... ] filterForVisitingDirectories:[:dir | (dir hasSuffix:'tmp') not]
to enumerate all files and folders under a top folder, but only in subfolders named "Font", you could write:
'c:\Windows' asFilename recursiveDirectoryContentsDo:[:eachFile | ... ] filterForVisitingDirectories:[:dir | dir baseName = 'Fonts']

Notice that there are many more enumeration/filter functions provided by the Filename class. Use a class browser or consult the full online documentation.

Contents[Bearbeiten]

The following functions read/write the whole contents of a file as one possibly big data object.
You should not use them for very large files, as they might allocate/require large amounts of memory;
use stream functions instead (and process it linewise, blockwise or characterwise).

aFilename contents
Returns a collection of text-lines (i.e. an Array-like object where each element represents one line of text)
aFilename contentsAsString
Returns a single (possibly big) string, a collection of characters
aFilename binaryContents
Returns a single (possibly big) byte array, a collection of integers in the range 0..255
aFilename contents: aStringOrByteArrayOrCollectionOfLines
Creates (i.e. writes) the file.
Examples:
'foo1' asFilename contents:'Hello world'.
'foo2' asFilename contents:#[1 2 3 4 0xFF 0xFE 0xFD].
'foo3' asFilename contents:#('line1' 'line2' 'line3').

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
aFilename utf8ReadStream
aFilename utf8WriteStream
Return a stream for reading or writing to the file. The stream will automatically generate/convert utf8 code sequences; i.e. the stream will not deliver/expect escape sequences, but possibly character instances with codepoints above 0xFF.
Example:
Writing as UTF8, reading binary:
|s bytes|
s := 'test.dat' asFilename utf8WriteStream.
s nextPutLine:'αβγψω⮠'.
s close.
bytes := 'test.dat' asFilename binaryContents.
bytes.
=> #[206 177 206 178 206 179 207 136 207 137 226 174 160 10]
Reading UTF8:
|s text|
s := 'test.dat' asFilename utf8ReadStream.
text := s nextLine.
s close.
text.
=> 'αβγψω⮠' (a widestring)



Copyright © 2014-2024 eXept Software AG