6.10.94-stable

Files and Folders

Navigating trough files and folders is quite simple, and makes use of only two utilities FileWalker and File.

Folders

To navigate a folder, you wanna use a FileWalker. You initialize the walker with the path of your folder. A loop will then allow you to go through all the content of the folder.

fn run() {
  var walker = FileWalker::new("./dataFolder");
  while(!walker.isEmpty()) {
    var file = walker.next();
    //Do something with the file.
  }
}

To recurse into the directories, you have various options:

  1. The recursive calls: this will work as long as you don’t have a great depth of nesting. The order of processing the file (prefix, infix or postfix) will depend on how you implement the recursion.
fn processFileOrDirectory(path: String) {
  var walker = FileWalker::new(path);
  while(!walker.isEmpty()) {
    var file = walker.next();
    if(file != null && file.isDir()) {
      //Recurse
      processFileOrDirectory(file.path);
    } else {
      //Do something with the file.
    }
  }
}

fn run() {
  processFileOrDirectory("./dataFolder");
}
  1. Iterative, depth-first or breadth-first: this approach will work the same way as the recursion, but it is guaranteed to scale whatever your nesting depth (no stack-overflow). Here below a breadth-first example.
fn run() {
  var dirToProcess = Queue<String>::new();
  dirToProcess.enqueue("./dataFolder");

  while(dirToProcess.size() != 0) {
    var fileWalker = FileWalker::new(dirToProcess.dequeue() as String);
    while(!fileWalker.isEmpty()) {
      var file = fileWalker.next();
      if(file != null) {
        println("Processing ${file.path}");
        if(file.isDir()) {
          dirToProcess.enqueue(file.path);
        } else {
          //Do something with the file.
        }
      }
    }
  }
}

Files & Directories

The type File provides mostly static utility methods to help the handling of files and directories.

Create Directory

Just call the following with the full path:
File::mkdir("./path/to/my/directory"): bool;

Copy file or directory

You can copy files by specifying the source first, then the path of the destination.
File::copy("./origin.txt", "./copy.txt"): bool;

Delete file of directory

Files or directories can be deleted from their path
File::delete("./file/to/delete.txt"): bool;

Rename file or directory

Directories and files can also be renamed
File::rename("./oldName", "./newName"): bool;

Path utilities

Depending on your context of execution, it might not be easy to figure the base path for writing or reading files. The File type provides three utility functions providing some base paths.
File::baseDir():String returns the relative path, from the executable working dir, to the files folder in GreyCat.
File::userDir():String returns the relative path, from the executable working dir, to the directory in GreyCat for the current user.
File::taskDir():String returns the relative path, from the executable working dir, to the directory in GreyCat for the task currently executing.