I have quite a few folders that in turn have their respective sub-folders, inside one of those sub-folders there is a subfolder_1_2, subfolder_2_2, subfolder_3_2 that contains files for example:
carpeta
│── Subcarpeta_Principal1
│ │──Subcarpeta_1_1
│ │ │── Subcarpeta_1_2
│ │ │ │──Archivo.txt
│ │ │ │──Archivo.txt
│── Subcarpeta_Principal2
│ │──Subcarpeta_2_1
│ │ │── Subcarpeta_2_2
│ │ │ │──Archivo.txt
│ │ │ │──Archivo.txt
│── Subcarpeta_Principal3
│ │──Subcarpeta_3_1
│ │ │── Subcarpeta_3_2
│ │ │ │──Archivo.txt
│ │ │ │──Archivo.txt
And what I want to know is if it is possible to move those files to their respective subfolders: Subfolder_Main1 , Subfolder_Main2, Subfolder_Main3 , as shown in the example:
carpeta
│── Subcarpeta_Principal1
│ │ │ │──Archivo.txt
│ │ │ │──Archivo.txt
│ │──Subcarpeta_1_1
│ │ │── Subcarpeta_1_2
│── Subcarpeta_Principal2
│ │ │ │──Archivo.txt
│ │ │ │──Archivo.txt
│ │──Subcarpeta_2_1
│ │ │── Subcarpeta_2_2
│── Subcarpeta_Principal3
│ │ │ │──Archivo.txt
│ │ │ │──Archivo.txt
│ │──Subcarpeta_3_1
│ │ │── Subcarpeta_3_2
How could I do it?
We are going to need some imports:
Let's get a list of folders located in a base folder:
Those folders are the main folders to which we want to move all the files that are in their subfolders.
For each parent folder let's get a list of the subfolders that are immediately contained in it.
For each of these subfolders we want to move the files contained in them to its main folder:
Now we need to define
moverFicherosEnSubcarpetas
, which should move the files contained in the folder of the first parameter to the folder of the second parameter.And with this almost everything is done. But one thing is missing. So far we have moved files from subfolders to their parent folders. But what about the files in the subfolders of the subfolders? And those of the subfolders of the subfolders of the subfolders?, etc...
These we can move
moverFicherosEnSubcarpetas
recursively called:The code as a whole is: