Append suffix (#) to copied file if destination filename exist

What is the problem you are having with rclone?

With rclone is it possible so it appends a appropriate suffix (#) to the copied file if a file with the same name exist in the destination directory? Exactly how windows explorer handles the filenames, appending (0), (1), (2) etc. to the copied over file.

Currently using the --suffix but it seem to handle by instead renaming the destination file with the suffix instead of the copied file, and it also seem to overwrite the destination file if there already exist a same filename with the suffix too whereas I expect it to instead append a (1) suffix instead like how windows explorer does it.

The command you were trying to run (eg rclone copy /tmp remote:tmp)

rclone copy sourcedir destinationdir --create-empty-src-dirs --suffix "(0)"

Well ended up creating a python script to do this, not good as it passes on single files one at a time to rclone when doing the copyto but there you go.

import os
import subprocess
import datetime

def printRed(skk):
    print("\033[91m{}\033[00m".format(skk))
def printGreen(skk):
    print("\033[92m{}\033[00m".format(skk))
def printYellow(skk):
    print("\033[93m{}\033[00m".format(skk))
def printCyan(skk):
    print("\033[96m{}\033[00m".format(skk))

logPath = "C:/Users/"

while True:
    # Set move operation
    while True:
        print("1. Sync\n"
              "2. Copyto\n"
              "3. Move")
        move_operation = input("Enter the move operation (1, 2, or 3): ")
        if move_operation == "1":
            move_operation = "sync"
            break
        elif move_operation == "2":
            move_operation = "copyto"
            break
        elif move_operation == "3":
            move_operation = "move"
            break
        else:
            printRed("Invalid input, try again")
    printCyan(f"Selected move operation: {move_operation}")

    while True:
        if move_operation == "copyto":
            checksum = None
            break
        checksum = input("Enabled checksum checks? (Y/N): ")
        checksum = checksum.upper()
        if checksum == "Y":
            checksum = "--checksum"
            printCyan("Checksum checks enabled")
            break
        elif checksum == "N":
            printCyan("Checksum checks disabled")
            checksum = None
            break
        else:
            printRed("Invalid input, try again")

    while True:
        # Set source path
        while True:
            source_path = input("Enter the source directory path: ")
            source_path = source_path.replace('"',"")
            if os.path.isdir(source_path):
                printCyan(f"Selected source path: {source_path}")
                break
            else:
                printRed("Directory path not found, try again")

        # Set destination path
        while True:
            destination_path = input("Enter the destination directory path: ")
            destination_path = destination_path.replace('"',"")
            if os.path.isdir(destination_path):
                printCyan(f"Selected destination path: {destination_path}")
                break
            else:
                printRed("Directory path not found, try again")
        
        if source_path == destination_path:
            printRed(f"Source and destination path cannot be the same")
        else:
            break
    
    input("Press any key to continue move operation")

    # Get the current date and time
    current_datetime = datetime.datetime.now()

    # Format the date as YYYY-MM-DD
    formatted_date = current_datetime.strftime('%Y-%m-%d')

    # Format the time as HH.mm.ss
    formatted_time = current_datetime.strftime('%H.%M.%S')

    if move_operation == "copyto":
        # Iterate over files in the source directory
        for root, subdirs, files in os.walk(source_path):
            for file in files:
                source_file = os.path.join(root, file)

                # Construct the destination file path
                relative_path = os.path.relpath(root, source_path)
                destination_file = os.path.join(destination_path, relative_path, file)

                # Check if the file already exists in the destination
                counter = 0
                base, ext = os.path.splitext(file)
                while os.path.exists(destination_file):
                    new_filename = f"{base} ({counter}){ext}"
                    destination_file = os.path.join(destination_path, relative_path, new_filename)
                    counter += 1
                cmd_args = ["rclone", move_operation, source_file, destination_file, "--metadata", "--progress", "--log-file=" + logPath + f"{move_operation} source to destination - Date=" + formatted_date + " & Time=" + formatted_time + ".log", "--log-level=INFO"]
                subprocess.run(cmd_args)
    else:
        cmd_args = ["rclone", move_operation, source_path, destination_path, "--metadata", "--progress", "--check-first", "--create-empty-src-dirs", "--transfers=20", "--checkers=20", "--log-file=" + logPath + f"{move_operation} source to destination - Date=" + formatted_date + " & Time=" + formatted_time + ".log", "--log-level=INFO"]
        if checksum:
            cmd_args.append(checksum)
        subprocess.run(cmd_args)

    current_datetime2 = datetime.datetime.now()
    timeTaken = current_datetime2 - current_datetime

    # Get the total seconds elapsed
    total_seconds = timeTaken.total_seconds()

    # Calculate hours, minutes, and seconds
    hours, remainder = divmod(total_seconds, 3600)
    minutes, seconds = divmod(remainder, 60)

    # Format as HH.MM.SS
    formatted_time = f"Total elapsed time: {int(hours):d}h {int(minutes):d}min {int(seconds):d}s"
    printCyan(formatted_time)
    print("-"*100)

nice script.

maybe, instead of subprocess.run for each file, use the rclone api
https://rclone.org/rc/#operations-movefile

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.