Copy an entire directory

I would like to know if it becomes possible now to copy an entire directory or move a directory and its contents.
has it also become possible to decompress a zip in a subdirectory.

Maybe it’s time for you to share what you’re working on, or trying to accomplish. :smiley:

Speaking strictly from Desktop application, the answer to your questions is, yes, it’s possible. Again, with Lua’s LFS and IO you can get the job done. Via os.execute, you can also get the job done (at least for Windows, definitely).

Decompressing a zip file can be done via this plugin.

Yes, you can copy a ‘folder’, but not directly. You have to create the new/dst folder, the copy file by file.

Having said that, SSK2 has a library to help do this:

  • /files.lua
  • /files/*

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/files/

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/files/#utilities-sskfilesutil

  • cpFolder( src, dst )
1 Like

Thank you for your reply !

This plugin not allow me to change its properties. it only decompress on root directory.
and me I need to decompress on subdirectory.

os.execute need to know the name of the computer Users to copy file from system.DocumentsDirectory

os.execute('start xcopy /E "C:\\Users\\Steeve\\AppData\\Roaming\\MyApps\\Myfolder" "C:\\xyz\\123\\"')

For the zip plugin, you should be able to uncompress to any path as long as access is not restricted, but I’m not sure whether it creates the directory you specify if it doesn’t exist (you can test it out).

The User’s name can be retrieved with %username%, but I wouldn’t assume Documents folder is at the usual path as it can be changed in Windows to another location, instead use the following to get the User’s document’s path:

local docsPath = system.pathForFile("",system.DocumentsDirectory)

In the Simulator, it points to a location within the project’s sandbox directory, but on an actual Windows build app it’ll reference the User’s Document directory per Windows variables.

I am trying to do that :


local zip = require( "plugin.zip" )
local function zipListener( event )
     if ( event.isError ) then  
           message.text= "Error"
    else
          if ( event.response[1]) then 
            btn.isVisible = true     
        end
    end
end
local zipOptions = {
      zipFile = "text_files0.zip",
     zipBaseDir = system.TemporaryDirectory,
     dstBaseDir = system.DocumentsDirectory,       
    listener = zipListener
}
zip.uncompress( zipOptions )

I can’t modify this line dstBaseDir = system.DocumentsDirectory,
like
local docsPath = system.pathForFile("/NewFolder",system.DocumentsDirectory)
dstBaseDir =docsPath
Any change made the zip uncompressed direct

I am working both on windows and android at time.

thanks for %username% it works on Windows 11

you have 2 folders: ssk2.zip and validation.zip

I see validation is a sample application with ssk2 included

what should i choose ?

My mistake, I didn’t realize dstBaseDir requires one of the following constant variables:

system.ResourceDirectory
system.DocumentsDirectory
system.TemporaryDirectory
system.CachesDirectory

You’ll need to uncompress to one of those locations and then move the files (or directory) to your final desired location.

Just to chime in quick, remember that you can’t traverse folders on Android or make any changes to the resource directory either.

I don’t know how to move entire folder from system.TemporaryDirectory to system.Documents.Directory

Go to the main SSK2 github link and click the green (Code button), then select download zip.

image

After unzipping, use the ssk2 subfolder.

Docs are here:

For Windows, you can use the xcopy command I mentioned on your other post, and delete the files afterward, or you can try robocopy which should also be included in the OS.

I’m copying and pasting stuff I found on a quick search…
This will move all the files and folders, including empty ones, deleting each original file after it has moved it.

robocopy /move /e "sourcedir" "destdir"

or

robocopy "source" "destination" /MIR

/MIR … mirror a complete directory tree (also deletes files in the destination)

I haven’t used robocopy with the MIR switch. As always, test things out to make sure they work correctly.

Also, take note of what Xedur mentioned for Android, and since you need a solution for both platforms then I think using RoamingGamer’s SSK2 lib would be good for all supported platforms.

Can I make a zip folder without supplying file name ?

I mean : can I compress a folder if I dont know the files inside.

I want to move folder A to folder B in system.TemporaryDirectory. But I don’t know the name or how many files in folder A.
I don’t know how to call ssk2 module in my code

I see you have this function :

function util.mvFolder( src, dst ) -- Move/Rename File
   local result, reason = os.rename(src,dst)
   if( not result ) then
      print("Tried to move/rename " .. tostring( src ) .. " to " .. tostring( dst ) .. " and failed: " .. tostring( reason ) )
   end
   return result
end

can you tell me how i should call this function in my codes ?

I made an example:
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2023/01/mv_folder.zip

Note: In the example, I copied a folder from resource/images/medieval to temporary so I had something to work with. The important parts of the example are steps #1 and #3

See main.lua:

io.output():setvbuf("no")
display.setStatusBar(display.HiddenStatusBar)

-- =====================================================
-- 1. Load and initialize SSK2 (ONCE ONLY IN MAIN.LUA)
-- =====================================================
require "ssk2.loadSSK"
_G.ssk.init( { measure = false } )
-- =====================================================


-- =====================================================
-- 2. Create sub-folder in temporary and copy some files  to it so we have a folder and files to demonstrate this example.
--
-- (ONLY FOR THIS EXAMPLE)
-- =====================================================
local medievalPath 	= ssk.files.resource.getPath( 'images/medieval' )
local temporaryRoot = ssk.files.temporary.getRoot()

-- ssk.files.desktop.explore(medievalPath) -- Uncomment to open in your Windows or OSX runs
-- ssk.files.desktop.explore(temporaryRoot) -- Uncomment to open in your Windows or OSX runs

local dstFolder     = ssk.files.temporary.getPath( 'medieval' )

ssk.files.util.cpFolder( medievalPath, dstFolder )
-- ssk.files.desktop.explore(dstFolder) -- Uncomment to open in your Windows or OSX runs


-- =====================================================
-- 3. Example of how to move folder SRC to DST
--
--  Folder SRC is system.TemporaryFolder/medieval (created and filed above for this example)
--  Folder DST is system.DocumentsFolder/medieval is the destination for our move
--
-- =====================================================
local src = ssk.files.temporary.getPath( 'medieval' )
local dst = ssk.files.documents.getPath( 'medieval' )

-- local documentsRoot = ssk.files.documents.getRoot()
-- ssk.files.desktop.explore(src)
-- ssk.files.desktop.explore(documentsRoot)

if( ssk.files.util.isFolder( dst ) ) then
	print("Warning!  ", dst, " already exists!  Move may fail (depending upon OS)." )
	print("Deleting folder first." )
	ssk.files.util.rmFolder( dst )
end

ssk.files.util.mvFolder( src, dst )

1 Like

thank you very much
I have a question

why it delete the root directory instead clearing all files inside

local deleteFolder = ssk.files.temporary.getPath( ’ ’ )
ssk.files.util.rmFolder(deleteFolder)

I am trying to delete all files in system.temporaryDirectory, but it also delete the Temporary folder

rmFolder() is meant to delete folders, not just their contents.

UPDATE See my next post.

I took another look at my library:

It looks like there is an undocumented function:
util.getFilesInFolder( path )

You could use that to remove all children from the temporary folder like this (pseudo-code follows):

tempRoot = ssk.files.temporary.getRoot()

filesInTemp = ssk.files.util.getFilesInFolder( tempRoot )

for _, filename in pairs( filesInTemp ) do

   path2 = ssk.files.temporary.getPath( filename )

   if( ssk.files.util.isFile( path2 ) ) then
      ssk.files.util.rmFile( path2 )
   end

   if( ssk.files.util.isFolder( path2 ) then
      ssk.files.util.rmFolder( path2 )
   end
   
end