Questions about smalltalk/x and its class library

Directory usage

Postby smalltalker89 » 29. Nov 2009, 02:08

I want to read all the files with extension .vm (for example) from specific directory by one program. how can i to do it :?:
smalltalker89
 
Posts: 3
Joined: 27. Nov 2009, 14:17

Re: Directory usage

Postby stefan » 30. Nov 2009, 15:55

This two code sniplets print the file names on the Transcript (ST/X Launcher). The first one works only in Windows.
Look at the method category 'enumerating-contents' in the Filename class for more methods that e.g. walk down a directory hierarchy.

Code: Select all
'C:\' asFilename directoryContentsAsFilenamesDo:[:eachFilename|
    eachFilename hasSuffix:'vm' ifTrue:[
        Transcript showCR:eachFilename
    ]
].

'.' asFilename directoryContentsAsFilenamesDo:[:eachFilename|
    (eachFilename hasSuffix:'vm') ifTrue:[
        Transcript showCR:eachFilename
    ]
].
stefan
Site Admin
 
Posts: 10
Joined: 8. Nov 2008, 00:27

Re: Directory usage

Postby cgittinger » 30. Nov 2009, 17:47

of course, the following works just as well (reads the whole directory before filtering, though):
Code: Select all
   'myDir' asFilename directoryContentsAsFilenames select:[:fn | fn hasSuffix:'vm' ]


Claus
cgittinger
 
Posts: 61
Joined: 12. Nov 2008, 17:05

Re: Directory usage

Postby cgittinger » 18. Dec 2009, 00:05

a final note:
to get the names as strings instead of as filenames, use:
Code: Select all
    aFilename directoryContents

instead.
If I had a choice, I would have used that method-name to get the fileNames - however, for compatibility reasons, we use the same as VisualWorks (and Squeak ?).

Claus
cgittinger
 
Posts: 61
Joined: 12. Nov 2008, 17:05


Return to Programming Help

Users browsing this forum: No registered users and 1 guest

Design by GB