Copying directory structure without files

I would like to copy a directory structure without copying the files inside of it but I cannot figure out how. Copying everything and then deleting the files is not an option because it would take long and I don't have so much space. Using mkdir would be an option but then I would need to use loops to create the subfolders etc. so I was wondering if there is something simple to just copy the structure directly? Thank you in advance!

 Réponse acceptée

Walter Roberson
Walter Roberson le 3 Août 2022

1 vote

You only have to mkdir the leaf directories: mkdir will create any intermediate directory needed.
You can use dir() with the '**' pattern to find all names, and then discard from the list the ones for which isdir is false. Then use ismember on the name to discard the . and .. directories. Use the folder field to find the path, but make sure to discard the prefix to the root of the tree.
Do you happen to be using Linux or MacOS? Because you can system a shell command to do all the work.

4 commentaires

Walter Roberson
Walter Roberson le 3 Août 2022
Modifié(e) : Walter Roberson le 3 Août 2022
The shell command can look like
cd START; find . -depth -type d -print0 | cpio -p0d DESTINATION
where START is the path to the root to be copied and DESTINATION is where to copy them to. The cd part is not necessary if you are already cd there.
TL
TL le 3 Août 2022
Perfect, thank you! I'm not using Linux or MacOS, but I'll keep this option in mind, it looks much more convenient
Walter Roberson
Walter Roberson le 3 Août 2022
https://www.gnu.org/software/cpio/cpio.html
Walter Roberson
Walter Roberson le 3 Août 2022
https://docs.microsoft.com/en-us/windows/wsl/about should have find command, not sure if it would have cpio

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Produits

Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by