Move a file with file_move function lua script - solved

I want to write a lua script that performs moving of a file selected. i’m using nextcloud 25. Can someone share with me a script that performs this action ?

Hi Lolita,

You can read the file_move documentation here

An example script:

-- Get selected files
input_files = get_input_files()

-- To keep it simple the script only allows selecting one file
if (#input_files ~= 1) then
  abort("More than one file was selected.")
end
file = input_files[1]

-- Path to the destination folder (from the users home directory)
destination_path = "/destination_folder"

-- Move the file
success = file_move(file, destination_path)

-- Check if move succeeded
if (success) then
  add_message("File moved!")
else
  abort("Failed to move file")
end
2 Likes

Thank you :innocent: :innocent:. It runs very well. The only problem is that it can’t move files that weigths 1kb or less.

i have another issue. I wanted to use this script in a workflow with the flow “run file action” but it seems not to work. I chose to perfoms this flow with tags.

I tested a flow with the example above, and it worked as expected:

I am also able to move files of sizes smaller than 1kb. Do you have any more information? Are there any relevant errors in the nextcloud.log?

The only reason for the script not to work that I can think of, is if there is already a file with that name in the destination folder.

let me check

i checked. I emptied the nextcloud.log and re-run the script but i got no one error.

What does the flow configuration look like?

Are you using external storages?

i use “block access file”, “automated tagging” and “run file action”.

i can now move file that weigths 1kb or less. But when i tag a file it don’t move to the folder

Could you post a screenshot of your “Run file action” flow configuration like above?

I am out of ideas of why it might be failing.

You can use the log function to try to narrow down the problem.

Log at the start of the script to check that it got triggered by the flow.
Log during the move operation to make sure the code reached that point in the execution.
Log the file_move return value to check whether the move failed.

:sweat_smile: i’m not pretty good in programming. i don’t know if you can just show me the script that performs this action

Sure!

log("Started file move action")

-- Get selected files
input_files = get_input_files()

-- To keep it simple the script only allows selecting one file
if (#input_files ~= 1) then
  log("Aborted: more than one file was selected")
  abort("More than one file was selected.")
end
file = input_files[1]

-- Path to the destination folder (from the users home directory)
destination_path = "/destination_folder"
log("Moving file " .. file.name .. " to " .. destination_path)

-- Move the file
success = file_move(file, destination_path)

-- Check if move succeeded
if (success) then
  log("File moved!")
  add_message("File moved!")
else
  log("Move failed")
  abort("Failed to move file")
end
2 Likes

i get nothing in apache or nextcloud logs

anyway thanks a lot i will just manage to deal with this. I don’t know if you can help me to go further? :face_in_clouds: I would like to deal with meta data :face_in_clouds:. I would like to move a file to a folder based on its metadata :face_in_clouds:. I mean if the script detects that in the name there are character strings “service notes” i will redirect it to the folder services notes" :face_in_clouds:

the name or the file tag