A downloadable tool

A python program with graphic interface for replacing large numbers of values in multiple files. 

Needs tkinter module to run. 

Download

Download
Batch file name replace gui.py 2.4 kB

Install instructions

Source code;

# -*- coding: utf-8 -*-

"""

Created on Tue May 21 11:17:13 2024

@author: atutt-wi

"""

import os

import shutil

import tkinter as tk

from tkinter import filedialog, messagebox

# Create a Tkinter window with a pink theme

window = tk.Tk()

window.configure(background='#FFC0CB')

# Create two text boxes to enter the replacement values

list1_box = tk.Text(window, height=100, width=50)

list2_box = tk.Text(window, height=100, width=50)

# Create a frame to hold the text boxes and labels

lists_frame = tk.Frame(window)

lists_frame.pack(side='top', padx=10, pady=10)

# Create a label for each text box

list1_label = tk.Label(lists_frame, text='List 1:', width=10)

list1_label.pack(side='left')

list1_box.pack(side='left', padx=10)

list2_label = tk.Label(lists_frame, text='List 2:', width=10)

list2_label.pack(side='left')

list2_box.pack(side='left', padx=10)

# Create a button to choose the input folder

def choose_input_folder():

    global input_folder_path

    input_folder_path = filedialog.askdirectory()

    input_folder_label.configure(text='Input folder: ' + input_folder_path)

input_folder_button = tk.Button(window, text='Choose input folder', command=choose_input_folder)

# Create a label to display the chosen input folder

input_folder_label = tk.Label(window, text='Input folder: ')

# Create a button to choose the output folder

def choose_output_folder():

    global output_folder_path

    output_folder_path = filedialog.askdirectory()

    output_folder_label.configure(text='Output folder: ' + output_folder_path)

output_folder_button = tk.Button(window, text='Choose output folder', command=choose_output_folder)

# Create a label to display the chosen output folder

output_folder_label = tk.Label(window, text='Output folder: ')

# Create a function to replace strings in file names

def replace_strings():

    # Get the replacement values from the text boxes

    list1 = [line for line in list1_box.get("1.0", "end-1c").split("\n") if line.strip()]

    list2 = [line for line in list2_box.get("1.0", "end-1c").split("\n") if line.strip()]

    # Create a dictionary to map the old values to the new values

    replacements = dict(zip(list1, list2))

    # Get the files in the input folder

    files = os.listdir(input_folder_path)

    # Rename the files and move them to the output folder

    for file in files:

        old_path = os.path.join(input_folder_path, file)

        new_file = file

        for old, new in replacements.items():

            new_file = new_file.replace(old, new)

        new_path = os.path.join(output_folder_path, new_file)

        

        # Check if the new path is too long

        if len(new_path) > 260:

            messagebox.showwarning("Warning", f"The file name '{new_file}' is too long. The process will be stopped.")

            return

        

        shutil.move(old_path, new_path)

    

    # Update the input and output folder labels

    input_folder_label.configure(text='Input folder: ' + input_folder_path + ' (renamed and moved)')

    output_folder_label.configure(text='Output folder: ' + output_folder_path + ' (files moved)')

# Create a button to start the replacement process

replace_button = tk.Button(window, text='Replace strings', command=replace_strings)

# Add the widgets to the window

input_folder_button.pack(pady=10)

input_folder_label.pack(pady=5)

output_folder_button.pack(pady=10)

output_folder_label.pack(pady=5)

lists_frame.pack(pady=10)

replace_button.pack(pady=10)

# Start the Tkinter event loop

window.mainloop()


Leave a comment

Log in with itch.io to leave a comment.