lunarmodules / luafilesystem

LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution.
https://lunarmodules.github.io/luafilesystem/
MIT License
907 stars 293 forks source link

Copying/moving files #160

Open mystery-z opened 2 years ago

mystery-z commented 2 years ago

Is there any way to copy/move files using LFS?

I found this from a couple years ago asking for this very feature.

Any updates? And if not what are the reasons this hasn't been implemented yet?

mystery-z commented 2 years ago

@hishamhm Really really sorry for the @ but you seem pretty active here, maybe you could provide a response (if I may humbly ask for this feature).

Thanks!

Tieske commented 2 years ago

It can be done in pure Lua I think. That said; if you can contribute a PR, that would be most welcome I think.

hishamhm commented 2 years ago

@Tieske yes, it can. I have similar pure-Lua code in luarocks.fs.* using lfs. Might even move it out of there and into an lfs/util.lua module here, or something.

mystery-z commented 2 years ago

@Tieske As far as my research goes, the way to do it in pure lua is to use os.execute, but the problem with that is that Unix and Windows have different commands, so the OS needs to be checked first and then there can be an if...then...elseif... statement to switch depending on the OS.

And for files that can be opened using the io.read can be done by making another file (in the destination directory) and copying over the contents.

Is this something similar to what you mean?

@hishamhm Also could you copy and paste a snippet of your code here? It would really help if I could see how you did this. Thanks!

hishamhm commented 2 years ago

And for files that can be opened using the io.read can be done by making another file (in the destination directory) and copying over the contents.

Yes, this is what the LuaRocks code does for pure-Lua copying, and os.rename can be used for moving. Using lfs to create folders, there's no need to os.execute.