Automatically do a files:scan on that specific file when a new file is added

Hi, let’s say I have another app that generates files in the nextcloud directory “user/files/folder1/”, how can I get nextcloud to automate adding the new file into NC without having to type out the OCC command manually each time?

I currently normally run this with the occweb app
files:scan --path=user/files/folder1
Would usually run this instead of files:scan --all as it would take too long. Any alternate ways to add the file without scanning the whole folder would be even better!

I’m running NC20 on docker, using the latest image.

I note that there’s the workflow scripts app, and I also use rcdailey/nextcloud-cronjob. Any simple way to get the desired effect working?

2 Likes

You can also scan for username . Perhaps path not works.

If the folder is on a linux system you might be able to do what you want with ‘inotifywait’ as described in this stackoverflow post:

 inotifywait -m -e close_write /full/path/to/folder1/ | gawk '{print $1$3; fflush()}' | xargs -L 1 occ files:scan --path=path/to/folder1

I think that’s the easiest way to go :+1:

You can also scan for username . Perhaps path not works.

How does this help my situation? I already know how to scan for username…

If the folder is on a linux system you might be able to do what you want with ‘inotifywait’ as described in this stackoverflow post:

Cool solution, but as I mentioned, I run my NC on docker. The change you advised won’t stand an update.

I think that’s the easiest way to go :+1:

Well, how?

Perhaps another way. I use an additional file uploader (https://tinyfilemanager.github.io/) to upload to an external dir on the same server but outside Nextcloud. In Nextcloud i have mounted the external directory (not in nextcloud/data) with “external storage” (type local). If i upload files i found them after reload the folder in Nextcloud.

https://docs.nextcloud.com/server/latest/admin_manual/configuration_files/external_storage_configuration_gui.html

https://docs.nextcloud.com/server/latest/admin_manual/configuration_files/external_storage/local.html

I think that’s the easiest way to go :+1:

Well, how?

Install the workflow_scripts app, create a batch script which is being executed on a new file creation event and force an “occ files:scan ...” command. Other users have already used it for various jobs, see e.g.:

https://help.nextcloud.com/search?q=workflow_scripts%20%40j-ed%20

These are the options available to the workflow triggers. It does not have a “get newly added files” or a scheduler that I know of.

image

There’s also a condition for file activity:
image

But as I understand it, it refers to files being created legitimately via the web interface, or am I missing something here? :slight_smile:

That’s right, because this is the only case where Nextcloud is aware of file system changes. If you’re by-passing the Nextcloud gui you have to make sure that a file scan is triggered afterwards.

That’s exactly what I’m trying to achieve! So how to do this?

I feel like we’ve not gotten anywhere with the past few replies!

Was I not clear enough in my opening question??

Have you test my idea with external storage or why does it not makes sense for you?

Old post but i just encountered this issue and ill share how i solved it.

Inside Nextcloud dir you run this command:

sudo -u www-data php occ files:scan --path=“user_id/files/path”
Example: sudo -u www-data php occ files:scan --path=“/alice/files/Music”

That scans the specific directory you want to scan.

You create a cron job to run each minute to scan the directory

*/1 * * * * php8.1 -f /var/www/html/occ files:scan --path=“/alice/files/Music”

Do not forget to change the php version according to your installation and the path to you Nextcloud installation.

I have solve this problem by writing an simple auto_scan.sh.
I use docker for nextcloud server,and when starting docker,i run this script at the same time.
Here is the script

#!/bin/bash

DIRECTORY=“/root/data/docker_data/nextcloud/data/data/lh”
COMMAND=“sudo docker exec --user www-data nextcloud-app /var/www/html/occ files:scan lh”
eval “$COMMAND”

监视文件夹变化

while inotifywait -r -e modify,create,delete “$DIRECTORY”
do
# 执行命令
echo “文件夹发生变化,执行命令: $COMMAND”
sleep 5
eval “$COMMAND”
done

change DIRECTORY and COMMAND to yours,and save it.
run ./auto_scan.sh and then it’s solved.
if you want to put the program into the background,you can use the screen for it.

oh,I just find another method to solve it.
get to your config.php and add a new line like this:
‘filesystem_check_changes’ => 1,
1 represent checking any request to the folder and update it in nextcloud.
0 represent doing nothing
this method is such wonderful! it’s seem that i had wasted much time for writting a script

2 Likes