I just wanted information to see how to approach it. The 99.9% of the code that I do for my tools is written by me (inside of that can be considered our own code, since we all use LUA). I’m interested in learning to do it.
In this particular case, it is not very clear how LUA faces the cloning of files. In fact, I’ve been surprised by this issue. Apparently, there is no os.copy (origin, destination) or something similar, and I’m not the first to look for this.
@joule @ffx thanks for the help!
Finally, I opted for a non-recursive function. Only access to 2 levels of subdirectories. It is a very concrete way to make a backup of small files through a button. It can probably be improved. There must be some more direct (and quicker) way to copy one file to another without having to open and close it.
The function that I have done is like this:
--backup all banks
function backup_all_banks()
--check if exist main_folder
local origin_main_folder="chord_banks"
if io.exists(origin_main_folder) then
--create destiny main folder
local path=rna:prompt_for_path("Choose a destination folder to save a backup of all the banks.")
local destin_main_folder=("%s/%s"):format(path,origin_main_folder)
--check if exist destination folder
if io.exists(destin_main_folder) then
local mpt=rna:show_prompt((" %s"):format(main_title),("The folder %s already exist.\nDo you want to overwrite it?"):format(destin_main_folder),{"Ok","Cancel"})
if (mpt=="Ok") then
else
return
end
end
--local x=os.clock()
os.mkdir(destin_main_folder)
local mainfiles=os.filenames(origin_main_folder)
--rprint(mainfiles)
--copy main files
for main_fls=1,#mainfiles do
local orig=("%s/%s"):format(origin_main_folder,mainfiles[main_fls])
local dest=("%s/%s"):format(destin_main_folder,mainfiles[main_fls])
--open and read content file
local old_file=io.open(orig,"r")
local content=old_file:read("*a")
old_file:close()
--copy the content
local new_file=io.open(dest,"w")
new_file:write(content)
new_file:close()
end
--copy subfolders
local subfolders=os.dirnames(origin_main_folder)
--rprint(subfolders)
for sub_fld=1,#subfolders do
if io.exists(("%s/%s"):format(origin_main_folder,subfolders[sub_fld])) then
--create subfolders
os.mkdir(("%s/%s"):format(destin_main_folder,subfolders[sub_fld]))
--check files into subfolders
local subfiles=os.filenames(("%s/%s"):format(origin_main_folder,subfolders[sub_fld]))
--rprint(subfiles)
--copy files into subfolders
for sub_fls=1,#subfiles do
local orig=("%s/%s/%s"):format(origin_main_folder,subfolders[sub_fld],subfiles[sub_fls])
local dest=("%s/%s/%s"):format(destin_main_folder,subfolders[sub_fld],subfiles[sub_fls])
--open and read content file
local old_file=io.open(orig,"r")
local content=old_file:read("*a")
old_file:close()
--copy the content
local new_file=io.open(dest,"w")
new_file:write(content)
new_file:close()
end
end
end
--print(string.format("folder cloned: %.2f ms\n",(os.clock()- x)*1000))
else
local mpt_2=rna:show_prompt(
(" %s"):format(pre_main_title),("The folder \"%s\" not exist! Operation cancelled.\n"..
"Check that it is inside the root folder of the %s tool."):format(origin_main_folder,main_title),{"Close"}
)
end
end