Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

A Script to clone/copy a folder that contains multi folders and files to another folder

Community Beginner ,
Jun 17, 2021 Jun 17, 2021

Hello, I have a folder that contains multi folders and I want to clone it to another path (myDocuments or userData) but the method .copy() doesn't do that, it's copy only the files, i tried to create a new folder with .create method but my knowledge in coding helps me to do that only for 01 folder but i have multi folders and each one of them contains image files. i know that complex but please if someone can help to do that, 

Thanks on advanced! 

 

- Here's a small preview of my code: 

 

function PackPreviewsFilesCopier(packName, packPath) {
var mainPackPreviewsLocate = Folder(
packPath + "/Prism Preview Assets"
).getFiles();
var mainPackPreviewsDest = Folder(
Folder.userData + "/" + packName + "/Prism Preview Assets"
).getFiles();

if (mainPackPreviewsDest.exists) {
for (var c = 0; c < mainPackPreviewsLocate.length; c++) {
var FxNamesLocate = mainPackPreviewsLocate[i].name;
}
for (var d = 0; d < mainPackPreviewsDest.length; d++) {
var FxNamesDest = mainPackPreviewsDest[i].name;
}
if (FxNamesLocate === FxNamesDest) {
// I'm blocked here 😞
}
}
}

 

- Here's the tree of my folders: 

 

Tree.jpg

TOPICS
Actions and scripting , SDK , Windows
5.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Jun 17, 2021 Jun 17, 2021

Copies everything from the first folder to the second, including subfolders.

 

 

var ret = copy_folder("c:/1", "c:/2"); 

alert(ret);

function copy_folder(src_path, dst_path) 
    {
    try {
        var f = (new Folder(src_path)).getFiles();

        for (var i = 0; i < f.length; i++) if (!copy_file(f[i], dst_path)) return false;

        return true;
        }
    catch(e) { alert(e); }
    }

function copy_file(full_path, new_path)
    {
    try {
        var file = new File(full_path);

   
...
Translate
Adobe
Community Expert ,
Jun 17, 2021 Jun 17, 2021

I think what you want to do would be a job for  Windows File Explorer, Mac Finder and Adobe Bridge. Why do you want to manage your files via Photoshop?

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 17, 2021 Jun 17, 2021

Hi JJMack, 

My project is a photoshop extension that will help the user to generate the Effects in 01 click, so the idea is to create a package and include all the examples kind of images scripts and all assets. and in order for the user not to lose its package, I'm trying to copy the package folders to a safe place (MyDocuments or userData) and this is why I need this script. 

 

Thanks! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 17, 2021 Jun 17, 2021

Copies everything from the first folder to the second, including subfolders.

 

 

var ret = copy_folder("c:/1", "c:/2"); 

alert(ret);

function copy_folder(src_path, dst_path) 
    {
    try {
        var f = (new Folder(src_path)).getFiles();

        for (var i = 0; i < f.length; i++) if (!copy_file(f[i], dst_path)) return false;

        return true;
        }
    catch(e) { alert(e); }
    }

function copy_file(full_path, new_path)
    {
    try {
        var file = new File(full_path);

        var folder = new File(new_path);

        if (file.length < 0)
            {
            if (!create_path(new_path + "/" + file.name)) return false;
            if (!copy_folder(full_path, new_path + "/" + file.name)) return false;

            return true;
            }
        else
            {
            if (!create_path(new_path)) return false;
            if (!file.copy(new_path + "/" + file.name)) return false;

            return true;
            }

        function create_path(path)
            {
            var folder = new Folder(path);

            if (!folder.exists)
                {
                var f = new Folder(folder.path);
                if (!f.exists) if (!create_path(folder.path)) return false;

                if (!folder.create()) return false;
                }

            return true;
            }
        } 
    catch(e) { throw(e); }
    }

 

 

 

upd. was edited

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 17, 2021 Jun 17, 2021

Hi there r-bin! Thanks for your help. 

I use your code but i get a horrible result i don't know if it's a mistake from me but here's how i test the code: 

 

function Tester() {
var src=Folder("~/Desktop/main");
var dst = Folder("~/Desktop/Dest");
copy_folder(src, dst);
}

 

function copy_folder(src_path, dst_path) {

..... 

}

 

function copy_file(full_path, new_path){

..... 

}

 

When I execute the function all my folders and files on the desktop get duplicated many times. I'm sure I didn't change anything in the "copy_folder" & "copy_file" functions. 

 

Thanks. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 18, 2021 Jun 18, 2021

What did you fail?

Give an example of the structure in the "main" folder and what you got in the "Dest" folder.

 

What OS do you have?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 18, 2021 Jun 18, 2021

Hi! here's the structure: 

 

Screen Shot 2021-06-18 at 12.23.52.png

 

Here are The Results, as you see the function duplicates the files and folders from the desktop, not from the main folder. I'm using MACOS. 

 

Screen Shot 2021-06-18 at 12.25.22.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 18, 2021 Jun 18, 2021
I do not know what to say. I don't have access to MACOS.
 
What does this script show?
 
alert((new Folder("~/Desktop/main")).getFiles().length);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 18, 2021 Jun 18, 2021

alert(new Folder("~/Desktop/main").getFiles().length);

This alert 4 (.Dstore file is hidden). 

 

and when i use the function as you tell me i get the same result. 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 18, 2021 Jun 18, 2021

Show what it says

 

var f = (new Folder("~/Desktop/main")).getFiles();

var s = "";

for (var i = 0; i < f.length; i++) s += f[i].length + "\n";

alert(s);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 18, 2021 Jun 18, 2021

This: 

 

Screen Shot 2021-06-18 at 13.05.04.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 18, 2021 Jun 18, 2021

Well..., everything is correct.

I don't even know what kind of glitch this is. Something special in MACOS.

 

Try to replace in the code  + "\\" +  with  + "/" +

 

I can't think of anything else yet. : (

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 18, 2021 Jun 18, 2021

I replace it, but when I execute the function nothing happened 😕 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 18, 2021 Jun 18, 2021

Anyway Thank you very much Mr. @r-bin  you help me a lot and I get some new knowledges. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 18, 2021 Jun 18, 2021

Maybe you have a bug in your code?

Show exactly the code (script file) that you run.

It would also be nice if someone on the MAC could test the behavior of the script.

 

JJMack, do You have MACOS? 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 18, 2021 Jun 18, 2021

Good news the code is worked with me and here's the final code that i write: 

 

function Copier() {
var src=Folder.desktop + "/main";
var dst = Folder.desktop + "/Dest";
copy_folder(src, dst);
}

function copy_folder(src_path, dst_path) {
try {
var f = new Folder(src_path).getFiles();
for (var i = 0; i < f.length; i++)
if (!copy_file(f[i], dst_path)) return false;
return true;
} catch (e) {
alert(e);
}
}

function copy_file(full_path, new_path) {
try {
var file = new File(full_path);
var folder = new File(new_path);
if (file.length < 0) {
if (!create_path(new_path + "/" + file.name)) return false;
if (!copy_folder(full_path, new_path + "/" + file.name)) return false;

return true;
} else {
if (!create_path(new_path)) return false;
if (!file.copy(new_path + "/" + file.name)) return false;

return true;
}

function create_path(path) {
var folder = new Folder(path);

if (!folder.exists) {
var f = new Folder(folder.path);
if (!f.exists) create_path(folder.path);

folder.create();
}

return true;
}
} catch (e) {
throw e;
}
}

 

Here are the changes: 

 

01:  I replace "\\" with "/"

02: I replace var src="~/Desktop/main"; with  var src=Folder.desktop + "/main"; and the same for var dst = Folder.desktop + "/Dest"; 

 

I think I was missing something but when I checked the code again I found those mistakes. 

 

Thank you very much! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 18, 2021 Jun 18, 2021
It is more correct to write Folder.desktop.fsName
 
I.e., everything worked for you, did I understand correctly?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 18, 2021 Jun 18, 2021

Yes, everything is worked fine and I get what I'm looking for. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 18, 2021 Jun 18, 2021

Time to mark r-bin post as correct solution. You won't find better answer.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 18, 2021 Jun 18, 2021

Maybe you use mistakenly same source and destination?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 18, 2021 Jun 18, 2021

I'm sure I'm using different folders. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 18, 2021 Jun 18, 2021

Do you think double occurence of double r in return makes any difference for empty folder? 😛

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 18, 2021 Jun 18, 2021

No i make a correction on returrn to return 🙂 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 18, 2021 Jun 18, 2021

That was to r-bin who seems to love this kind of humour 😄

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 18, 2021 Jun 18, 2021

To be honest I didn't understand anything : )))

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines