Manipulation Directories java / python / c

3

I have a directory A, and this directory A has several subdirectories, and in each subdirectory, it has a variety of files. I would like to put the all files in a directory in the order they are in subdirectories, any idea how can I do this in java, python or c?     

asked by anonymous 19.02.2014 / 05:01

2 answers

2

Hmmm this question on stackoverflow.com would be reported, since it is not a doubt about a bug or something, you are simply asking someone to solve a problem for you. However, as said here , this site is not the stackoverflow.com. So I did a python script to help you since I was not even sleepy.

create the file copia.py with the following code:

import os
import sys

origem = sys.argv[1]
destino = sys.argv[2]

if not os.path.exists(destino):
    os.makedirs(destino)

for raiz, subDiretorios, arquivos in os.walk(origem):
    for arquivo in arquivos:
        arqCaminho = os.path.join(raiz, arquivo)
        novoArqNome = "%s/%s" % (destino, arqCaminho.replace('/', '_'))
        os.rename(arqCaminho, novoArqNome)

I have created the following structure to test in dirA:

$ tree dirA/
dirA/
├── arq1.foo
├── subDir1
│   ├── arq1.foo
│   ├── arq2.foo
│   ├── arq3.foo
│   ├── arq4.foo
│   └── subSubDir1
│       ├── arq1.foo
│       └── arq2.foo
├── subDir2
│   └── arq1.foo
└── subDir3
    ├── arq1.foo
    ├── arq2.foo
    └── arq3.foo

You run the script as follows:

$python copia.py dirA/ destino

and you will get the following result:

$ tree destino/
destino/
├── dirA_arq1.foo
├── dirA_subDir1_arq1.foo
├── dirA_subDir1_arq2.foo
├── dirA_subDir1_arq3.foo
├── dirA_subDir1_arq4.foo
├── dirA_subDir1_subSubDir1_arq1.foo
├── dirA_subDir1_subSubDir1_arq2.foo
├── dirA_subDir2_arq1.foo
├── dirA_subDir3_arq1.foo
├── dirA_subDir3_arq2.foo
└── dirA_subDir3_arq3.foo

Note that I renamed the file with the path + file name and replaced the / with _ so that you can keep the files in order by name as if they were in the folder. note how in the tree commands the files keep the same order even though they are no longer hierarchized by folders.

    
19.02.2014 / 06:54
1

why all this if shutil solves your problem

import shutil
shutil.copytree(diretorioA, diretorioB, symlinks=False, ignore=None)

make exact and recursive copy of a directory I already tried it on windows and linux and it does not have any problem

    
27.08.2014 / 19:26