Quantcast
Channel: Search for a files that matches another file name in another folder - Ask Ubuntu
Viewing all articles
Browse latest Browse all 4

Answer by Jacob Vlijm for Search for a files that matches another file name in another folder

$
0
0

I believe this is exactly doing what you describe.

The script below seems a bit verbose, but a great part of it is taken by the necessary information the user has to give in this somewhat complicated construction.

How to use

As always, copy the script below, enter the appropriate paths, in this case quite a few, the appropriate identifying strings, and save it as reorg.py.

Run it by the command:

python3 /path/to/reorg.py

The script

#!/usr/bin/env python3

import os
import shutil

# --------------------------------------------------------
# existing files directories
original_dir = "/path/to/original_files"
equals_dir = "/path/to/files_with_name_variants"
# original identifying string
id_string = "code" 
# variants + their desired destination
var_1 = "node"; vardir_1 = "/path/to/directory/where_first_variants_shouldbestored"
var_2 = "kode"; vardir_2 = "/path/to/directory/where_second_variants_shouldbestored"

# ---------------------------------------------------------

origs = []

for root, dirs, files in os.walk(original_dir):
    for name in files:
        if id_string in name:
            origs.append(name.replace(id_string, ""))

for root, dirs, files in os.walk(equals_dir):
    for name in files:
        if var_1 in name:
            if name.replace(var_1, "") in origs:
                shutil.copyfile(root+"/"+name, vardir_1+"/"+name)
        if var_2 in name:
            if name.replace(var_2, "") in origs:
                shutil.copyfile(root+"/"+name, vardir_2+"/"+name)

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>