Skip to content
Advertisement

Copy folder tree-recursively with depth control

I want to copy a whole folders/files tree from one location to another, but for a particular depth I want to perform a transliteration of the destination folder from one language to another.

So for /src/depth1/depth2/depth3/depth4/file I want to transliterate all depth3 folders to another language before copying them over to the destination path.

So I found this little, robust recursive function, which should help me in my task, and then I tried to add a depth control feature so that the transliteration replacement takes place only on depth3 folders. But at first, what I did only added to the depth. Then I thought I found the correct place to add the $depth--;, but unfortunately I can’t figure out where to subtract from it in order to start over a new branch…

Could someone help figure this out please?

JavaScript

Advertisement

Answer

Not tested, but if my understanding is right this should work, plus minus 1 depth.

JavaScript

Addition: This version will rename some directories according to their depth.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement