Bu using a recursive function that would create folders for each folder and copy files for each file. Here's the function I use:
copyFolderContents({
source: "/path/to/source",
target: "/path/to/target",
supressAlerts: true,
})
function copyFolderContents(data)
{
if (data.source.constructor != Folder) data.source = new Folder(data.source);
if (data.target.constructor != Folder) data.target = new Folder(data.target);
if (data.supressAlerts == undefined) data.supressAlerts = false
var errors = false;
copyFolder(data.source, data.target)
function copyFolder(sourceFolder, destinationFolder)
{
var sourceChildrenArr = sourceFolder.getFiles();
for (var i = 0; i < sourceChildrenArr.length; i++)
{
var sourceChild = sourceChildrenArr[i];
var destinationChildStr = destinationFolder.fsName + "/" + sourceChild.name;
if (sourceChild instanceof File)
{
copyFile(sourceChild, new File(destinationChildStr));
}
else
{
copyFolder(sourceChild, new Folder(destinationChildStr));
}
}
}
function copyFile(sourceFile, destinationFile)
{
createFolder(destinationFile.parent);
if (!sourceFile.copy(destinationFile))
{
if (!data.supressAlerts)
{
alert('Unable to copy\n' + sourceFile + '\nto\n' + destinationFile + '\nAborting.');
errors = true;
return false;
}
};
}
function createFolder(folder)
{
if (folder.parent !== null && !folder.parent.exists)
{
createFolder(folder.parent);
}
if (!folder.create())
{
alert('Unable to create:\n' + folder + "\nAborting.");
errors = true;
return false;
};
}
if (errors)
{
return false;
}
else
{
return true;
}
}