Left here for archival purposes.

User avatar
By HealthyHands
#31910 Hi,

I have tried to rename multiple files in my project, but the system always halts after third renaming.
To give you a better understanding of the problem, let me give you guys a couple of examples how I started narrowing down this thing.
To start with I have files read.txt, write.txt, foo.txt, bar.txt, file.txt uploaded to the ESP with ESPlorer.
I use script "print_all_files.lua" to get a listing of the files and print them out for me.
Attempt 1:
Code: Select all> file.rename("read.txt", "dir/read.txt")
> file.rename("write.txt", "dir/write.txt")
> file.rename("foo.txt", "dir/foo.txt")
> file.rename("bar.txt", "dir/bar.txt")
> file.rename("file.txt", "dir/file.txt")
> dofile("print_all_files.lua")
cannot open print_all_files.lua
stack traceback:
   [C]: in function 'dofile'
   stdin:1: in main chunk

Attemp 2:
Code: Select all> file.rename("foo.txt", "dir/foo.txt")
> dofile("print_all_files.lua")
File:   dir/foo.txt   size:   139
File:   file.txt   size:   152
File:   print_all_files.lua   size:   181
File:   read.txt   size:   151
File:   write.txt   size:   151
File:   bar.txt   size:   138
> file.rename("bar.txt", "dir/bar.txt")
> dofile("print_all_files.lua")
File:   dir/foo.txt   size:   139
File:   file.txt   size:   152
File:   print_all_files.lua   size:   181
File:   read.txt   size:   151
File:   write.txt   size:   151
File:   dir/bar.txt   size:   138
> file.rename("write.txt", "dir/write.txt")
> dofile("print_all_files.lua")
cannot open print_all_files.lua
stack traceback:
   [C]: in function 'dofile'
   stdin:1: in main chunk

Attemp 3:
Code: Select all> file.rename("foo.txt", "dir/foo.txt")
> file.rename("bar.txt", "dir/bar.txt")
> file.remove("dir/bar.txt")
> file.rename("write.txt", "dir/write.txt")
> file.remove("dir/foo.txt")
> file.rename("read.txt", "dir/read.txt")
> dofile("print_all_files.lua")
File:   print_all_files.lua   size:   181
File:   dir/read.txt   size:   151
File:   dir/write.txt   size:   151
File:   file.txt   size:   152
> file.rename("file.txt", "dir/file.txt")
> dofile("print_all_files.lua")
cannot open print_all_files.lua
stack traceback:
   [C]: in function 'dofile'
   stdin:1: in main chunk


The third attempt implies that those files would stay somehow open and block the use of another files. Or maybe there is some buffer that is filling up with data associated to the new files, and when the file is removed, so is the associated data.
I have tried it so that after every time I have renamed a file, I have opened it, checked it and if it's ok I have closed it. But this also fails after the third renaming. In this case I cannot open the third file to verify that the renaming was successful.

Is there something I am missing here?
User avatar
By TerryE
#32069 Rename can fail, so check the status. Also the docs advice calling file.close() between each name to flush the SPIFFS buffers. Also not that the later firmware releases include some SPIFFS fixes so it's well worth moving up form 0.95 or 0.96 to dev140 or the like.