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.
- Ensure you have two folders:
- A Source Folder: Contains the files with the names you want to use.
- 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.
- Open the script file (
SyncFileNames.ps1
) in a text editor, such as Notepad or Visual Studio Code. - Locate the following lines at the top of the script:
$sourceFolder = "<SOURCE DIR>" $targetFolder = "<TARGET DIR>"
- 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.
- Save the script after editing the paths.
- Save the script with your changes.
- Open PowerShell:
- Press
Windows + S
, typePowerShell
, and press Enter.
- Press
- Navigate to the directory where you saved the script using the
cd
command. For example:cd C:\Scripts
- Run the script:
.\SyncFileNames.ps1
- 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.
- 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.
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"
- 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.