Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JySzE/7f9de2b7dd8b27c36d5eb9b3819b04b4 to your computer and use it in GitHub Desktop.
Save JySzE/7f9de2b7dd8b27c36d5eb9b3819b04b4 to your computer and use it in GitHub Desktop.
How to Sync File Names Between Two Folders Using Powershell

How to Sync File Names Between Two Folders Using Powershell

Purpose

This script renames files in a target folder to match the names of files in a source folder. It’s helpful for:

  • Batch Renaming: Ensuring file names in the target folder align with those in the source folder.
  • Complex Naming Requirements: Handling scenarios where file-specific naming conventions are required, and bulk renaming utilities aren’t suitable.

Step-by-Step Instructions

1. Prepare Your Folders

  • Ensure you have two folders:
    1. A Source Folder: Contains the files with the names you want to use.
    2. A Target Folder: Contains the files you want to rename.
  • Both folders should have the same number of files for the script to work correctly.

2. Edit the Script

  1. Open the script file (SyncFileNames.ps1) in a text editor, such as Notepad or Visual Studio Code.
  2. Locate the following lines at the top of the script:
    $sourceFolder = "<SOURCE DIR>"
    $targetFolder = "<TARGET DIR>"
  3. Replace the paths with your desired folder locations:
    • Source Folder: Enter the full path of the folder containing the reference files.
    • Target Folder: Enter the full path of the folder containing the files you want to rename.
  4. Save the script after editing the paths.

3. Save and Open PowerShell

  1. Save the script with your changes.
  2. Open PowerShell:
    • Press Windows + S, type PowerShell, and press Enter.

4. Run the Script

  1. Navigate to the directory where you saved the script using the cd command. For example:
    cd C:\Scripts
  2. Run the script:
    .\SyncFileNames.ps1
  3. The script will:
    • Check for the existence of the source and target folders.
    • Verify that both folders contain the same number of files.
    • Rename the files in the target folder.

5. Verify the Results

  • If successful, files in the target folder will be renamed to match the files in the source folder.
  • If any issues occur (e.g., mismatched file counts), error messages will appear in PowerShell.

Editing the Source and Target Folders: Example

If the source folder is located at C:\Users\John\Documents\Source and your target folder is C:\Users\John\Documents\Target, update the script as follows:

$sourceFolder = "C:\Users\John\Documents\Source"
$targetFolder = "C:\Users\John\Documents\Target"

Tips and Troubleshooting

  • Backup: Always create backups of your target folder before running the script.
  • Execution Policy: If you encounter errors about script execution policies, run:
    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
  • File Order: Ensure both folders are sorted consistently if file order is important.
# Set the paths for source and target folders.
$sourceFolder = "<SOURCE DIR>"
$targetFolder = "<TARGET DIR>"
# Check if the source and target folders exist.
if (!(Test-Path -Path $sourceFolder)) {
Write-Error "Source folder '$sourceFolder' does not exist."
return
}
if (!(Test-Path -Path $targetFolder)) {
Write-Error "Target folder '$targetFolder' does not exist."
return
}
# Get all files in the source and target folders.
$sourceFiles = Get-ChildItem -Path $sourceFolder -File
$targetFiles = Get-ChildItem -Path $targetFolder -File
# Check if the number of files match.
if ($sourceFiles.Count -ne $targetFiles.Count) {
Write-Error "The number of files in the source and target folders do not match."
return
}
# Loop through each file in the source folder and rename the corresponding file in the target folder.
for ($i = 0; $i -lt $sourceFiles.Count; $i++) {
try {
$sourceFile = $sourceFiles[$i]
$targetFile = $targetFiles[$i]
# Rename the file in the target folder with the name from the source folder.
$newName = $sourceFile.Name
$newPath = Join-Path -Path $targetFolder -ChildPath $newName
Rename-Item -Path $targetFile.FullName -NewName $newName -Force
Write-Host "Renamed '$($targetFile.Name)' to '$newName'" -ForegroundColor Green
} catch {
Write-Error "Failed to rename '$($targetFile.Name)' due to: $($_.Exception.Message)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment