Kade Williams
Kade Williams

Reputation: 1191

Copy files of certain extension from subdirectories and place them in new subdirectories of same structure

I am going through a list of computers and searching a certain directory and subdirs for anything with a file extension of .exe.config

I want to take those files ending in .exe.config and place them in a backup directory of the same structure. So this would be done recursively.

For example, "main" is the main directory. There are several subdirs within "main" called "1", "2", "3", etc

main
\
 \-----------
 \   \    \
 1   2    3

In each of the numbered directories, there would be a file with the extension of .exe.config

I want to grab the main directory name, sub directory names, and *.exe.config files. Then place them in a backup "main" directory with the same structure.

There are other files in those numbered directories but I want to ignore those.

I'm not able to copy everything recursively. Here is the code I have been testing. A list of computers is placed in the serverlist.txt

import os
import shutil
import fileinput

def copy_names(servername):

    source = r'//' + servername + '/c$/Program Files (x86)/Main/'
    dest = r'//' + servername + '/c$/' + servername + '/Main/'

    for root, dirs, files in os.walk(source):
        for file in files:
            if file.endswith('.exe.config'):
                try:
                    os.makedirs(dest, exist_ok=True)
                    shutil.copyfile(os.path.join(source, file), os.path.join(dest, file))
                    print ("\n" + servername + " " + file + " : Copy Complete")
                except:
                    print (" Directory you are copying to does not exist.")

def start():
    os.system('cls' if os.name == 'nt' else 'clear')
    array = []
    with open("serverlist.txt", "r") as f:
        for servername in f:
            copy_names(servername.strip())


# start program
start()

Upvotes: 1

Views: 42

Answers (1)

ostrokach
ostrokach

Reputation: 20032

Try with these changes?

import os
import shutil
import fileinput

def copy_names(servername):

    source = r'//' + servername + '/c$/Program Files (x86)/Main/'
    dest = r'//' + servername + '/c$/' + servername + '/Main/'

    for root, dirs, files in os.walk(source):
        for file in files:
            if file.endswith('.exe.config'):
                file_src = os.path.join(root, file)
                file_dest = file_src.replace('Program Files (x86)', servername)
                os.makedirs(os.path.dirname(file_dest), exist_ok=True)
                try:
                    shutil.copyfile(file_src, file_dest)
                    print ("\n" + servername + " " + file + " : Copy Complete")
                except:
                    print (" Directory you are copying to does not exist.")

def start():
    os.system('cls' if os.name == 'nt' else 'clear')
    array = []
    with open("serverlist.txt", "r") as f:
        for servername in f:
            copy_names(servername.strip())


# start program
start()

os.path.join(source, file) does not give you the correct location of the source file; you should use something like os.path.join(root, file) instead.

There's probably a more robust way to get file_dest than file_dest = file_src.replace('Program Files (x86)', servername), but I can't think of it ATM.

Upvotes: 2

Related Questions