Batch copy multiple files from different folders with their paths listed in a txt file, and rename any duplicates

2 min read 07-10-2024
Batch copy multiple files from different folders with their paths listed in a txt file, and rename any duplicates


Batch Copy Multiple Files with Renaming Duplicates: A Step-by-Step Guide

The Problem: You have a list of files scattered across different folders, and you need to copy them to a single destination folder. But, some files might have the same name, leading to potential overwrites. You want to ensure all files are copied while avoiding duplicates and preserving the original filenames.

The Solution: This article will guide you through using a combination of command-line tools and a simple text file to automate the process of batch copying files with duplicate handling.

Step 1: Prepare Your File List

First, create a text file (e.g., file_list.txt) and list each file's full path on a separate line. For example:

C:\Users\John\Documents\file1.pdf
D:\Downloads\image1.jpg
C:\Projects\report.docx

Step 2: The Script (Batch or PowerShell)

Choose a scripting language like Batch or PowerShell. Here's an example using PowerShell:

# Set the destination folder
$DestinationFolder = "C:\CopiedFiles"

# Read the file list from the text file
$Files = Get-Content -Path "file_list.txt"

# Loop through each file
foreach ($File in $Files) {
    # Get the file name and extension
    $FileName = Split-Path -Path $File -Leaf
    $Extension = Split-Path -Path $File -Extension

    # Check if the file already exists in the destination folder
    if (Test-Path -Path "$DestinationFolder\$FileName$Extension") {
        # Find the next available file name for the duplicate
        $Counter = 1
        while (Test-Path -Path "$DestinationFolder\$FileName ($Counter)$Extension") {
            $Counter++
        }
        # Copy the file with the new name
        Copy-Item -Path $File -Destination "$DestinationFolder\$FileName ($Counter)$Extension"
    } else {
        # Copy the file directly
        Copy-Item -Path $File -Destination $DestinationFolder
    }
}

Explanation:

  • $DestinationFolder: Set the destination folder path.
  • $Files: Read the list of files from the text file.
  • foreach loop: Iterates through each file in the list.
  • Split-Path: Extracts the filename and extension.
  • Test-Path: Checks if a file with the same name already exists in the destination folder.
  • while loop: Finds the next available name for duplicates by adding a counter in parentheses (e.g., file1 (1).pdf).
  • Copy-Item: Copies the file to the destination folder.

Step 3: Run the Script

Save the script as a .ps1 file (e.g., copy_files.ps1) and run it from PowerShell. You can also create a batch file that calls the PowerShell script if you prefer.

Additional Tips

  • Error Handling: The script can be further enhanced by adding error handling to gracefully manage situations like file not found or access errors.
  • Customization: Modify the code to suit your specific needs, like adding logging, filtering files by extension, or implementing a different naming scheme for duplicates.
  • Alternative Solutions: Tools like robocopy or third-party file management software can provide similar functionalities.

Conclusion

This guide provides a practical solution for batch copying multiple files from different folders while handling duplicates effectively. Remember to adapt the code to your specific file list and destination folder. By combining the power of scripting with careful planning, you can streamline your file management tasks and ensure a clean and organized workspace.