FIles/Node api examples

I was lookin into Files/Node API but I wasn’t able to find good examples on how you could loop through users files. And I would like know more about File and Folder classes and how you could use those.

Any examples and tutorials would be greatly appreciated. :slight_smile:

Thank you

Best Regards Tuomas


I would like to redirect you to the API reference:

Did you read over them?

What is it you want to do? Just iterate over all user files?

Here is a rough sketch of what you might (or might not) want to implement, depending on your use-case:

  1. You can use the getUserFolder to obtain a Folder of the user’s root.
  2. Then you can use getDirectoryListing to list all nodes in the root node.
  3. For each found node, you can use getType to distinguish between File and Folder classes.
  4. Process the files as you like.
  5. For each folder you found you might need to process according to your business case. Also, repeat step 2 for each found folder.

Does this help you?


Thank you again for answering, those class definitions are great and your example helps a lot. I would like to loop through users files, read all the metadata from the file that is available and possibly move and/or tag the file. Those examples would help even more :wink:

Thank you so much. This is a great that I am possible to get help.

Best Regards Tuomas

And I have little confusion with regarding the namespaces… There seems to be OC and OCP namespaces and the examples and the bootstrap application that is created seem to use these quite mixed. :wink:

I will have to answer later, i am currently abroad. Sorry for not being able to help faster.

No worries. This is more than enough. There is no hurry with these. :slight_smile:

You should stick to using OCP wherever possible. The OCP namespace is the “Public” namespace which contains all the interfaces that are available to be used by other apps. The OC namespace is generally where the specific implementations of the interfaces live, and your application should not rely on these.

As a sidenote, depending on what exactly you are trying to accomplish you may be able to do it with the files_scripts app (iterating over files and tagging them is certainly possible). This is an app I wrote which allows you to run scripts inside your nextcloud instance without the need to write a whole app. The scripts can be run with: flow, occ, or as file actions directly in the files app.


Regarding the namespace issue as mentioned by @Raudius see also my post. The OC is perfectly valid in the front end.

The documentation I have sent to you are from the public API definition, aka from the OCP namespace. So, these methods are save to use.

To give you an idea of this might be working, I can write you a small snippet. Do not get fooled, I did not test anything and it might simply be wrong.

/** @var ITags */
private $tagger;
/** @var IRootFolder */
private $rootFolder;
/** @var String */
private $userId;

public function __construct(ITag $tagger, IRootFolder $rootFolder, $user_id){
  $this->$tagger = $tagger;
  $this->rootFolder = $rootFolder;
  $this->userId = $user_id;

// Method for parsing a single folder
private function parseFolder(Folder $folder): void {
  // No handling of the folders was requested by the OT.
  // That could/would go here.

  foreach ($folder->getDirectoryListing() as $node) {
    if ($node->getType() === FileInfo::TYPE_FOLDER) {
    } else {

// Extract the data of a single file
private function parseFile(File $file){
  // Here we do the stuff with the files.

  // This is rather costly. Maybe you can replace it with something more restricted?
  $stat = $file->stat();

  if (false /* ... */) {

  if (false /* ... */){
    // Add tag to file
    $fileId = $file->getId();
    $tag = ...

    $this->tagger->tagAs($fileId, $tag);
    // This will only add new tags. If you want to remove them as well (sync them), you need to do that as well.

public function iterateFolder() {
  $userFolder = $this->rootFolder->get($this->userId)->get('files');


You will have to tweak according to your needs.

Does this help you with your problem?

1 Like

Thank you again! I think this will help me to get forward.
This is great :wink:

1 Like