Search code examples
filesystemspharosqueak

Pharo or Squeak: How to get a list of directories and files in a recursive way


First of all: I'm not a programmer, just an amateur with some lights about Squeak/Pharo. So Please be gentle with the technical explanations...

I have a personal library with thousands of books, articles, etc., some on paper, some on digital support. All of this is garbage unless I can organize and classify everything.

I thought this would be an easy task with Smalltalk.

I created 3 main classes:

Object>>Cards 
    inst. var: name, localAdress, notes, tags, ...
Cards>>Work 
    inst. var: authors "aSet of Cards"
Cards>>Author
Object>>MyLibrary
    inst var: works "aSet of Cards" authors "aSet of Cards" myLibraryBase "aString? aPath?"

Plain simple. Instances of MyLibrary #initialize with a UIManager chooseDirectory, to establish definitely where the files must be found.

Now here is my problem: I should create a method for MyLibrary to «scan» all the relevant files (suffix) in all the folders and subfolders under myLibraryBase. Meaning, the method should answer a collection of files. This method or another method should further create a new Card for each document found in the collection, registering its localAdress.

I simply cannot figure out how to do it. I tried, under FileSystem, #allDirectories, but it doesn't work. Also, I can't figure out how to force the FileSystem to start at myLibraryBase, instead of root, workingDirectory, etc.

Can someone point me in the right direction? Thanks


Solution

  • In Pharo, you could try this for a start. I used jpg extension in the sample. If you use children you get the current folder. If you use allChildren, subfolders are included:

    | folder imageFiles |
    folder := '/path/to/your/folder' asFileReference.
    imageFiles := folder allChildren select: [ :each | each basename endsWith: 'jpg' ].
    

    This documentation is a bit dated, but I think it mostly still applies: https://eng.libretexts.org/Bookshelves/Computer_Science/Programming_Languages/Book%3A_Deep_into_Pharo_(Bergel_Cassou_Ducasse_and_Laval)/02%3A_Files_with_FileSystem