如何将不同子文件夹中的文件重命名为同名并保留原扩展名?
Got it, let's solve this problem. You need to rename every file in subfolders under C:\Workspace to a consistent base name (like scan) while keeping their original file extensions. Below are a few practical methods tailored for different tools you might prefer:
Method 1: PowerShell (Windows Built-in, No Extra Tools)
PowerShell is the easiest built-in option for Windows. This script will rename all files in your target subfolders, and includes a safety check to avoid overwriting files if a subfolder has multiple files with the same extension.
Basic Version (Overwrites duplicate extensions)
Use this only if you're sure each subfolder has exactly one file:
Get-ChildItem -Path "C:\Workspace" -File -Recurse | ForEach-Object { $newFileName = "scan$($_.Extension)" Rename-Item -Path $_.FullName -NewName $newFileName }
Safe Version (Avoids Overwrites)
If subfolders might have multiple files, this adds a counter (e.g., scan-1.jpg, scan-2.jpg) to prevent overwriting:
Get-ChildItem -Path "C:\Workspace" -File -Recurse | ForEach-Object { $baseName = "scan" $extension = $_.Extension $targetPath = Join-Path -Path $_.DirectoryName -ChildPath "$baseName$extension" # Add counter if file already exists $counter = 1 while (Test-Path -Path $targetPath) { $targetPath = Join-Path -Path $_.DirectoryName -ChildPath "$baseName-$counter$extension" $counter++ } Rename-Item -Path $_.FullName -NewName $targetPath }
How it works:
Get-ChildItem -File -Recurse: Scans all files (not folders) inC:\Workspaceand its subfolders$_.Extension: Grabs the original file's extension (e.g.,.jpg,.doc)- The loop checks if the new filename already exists, and appends a counter if needed
Method 2: Batch Script (Windows Command Prompt)
If you prefer using Command Prompt, this batch script does the same job. Again, we'll include a safe version to avoid overwrites.
Basic Version (Overwrites duplicates)
@echo off set "root=C:\Workspace" set "newBase=scan" for /r "%root%" %%f in (*) do ( ren "%%f" "%newBase%%%~xf" )
Safe Version (Avoids Overwrites)
@echo off setlocal enabledelayedexpansion set "root=C:\Workspace" set "newBase=scan" for /r "%root%" %%f in (*) do ( set "ext=%%~xf" set "folder=%%~dpf" set "newFile=!folder!!newBase!!ext!" set "counter=1" :checkExists if exist "!newFile!" ( set "newFile=!folder!!newBase!-!counter!!ext!" set /a counter+=1 goto checkExists ) ren "%%f" "!newFile!" ) endlocal
How it works:
for /r "%root%" %%f in (*): Recursively loops through all files in the target directory%%~xf: Extracts the file's extensionenabledelayedexpansion: Lets us update variables inside the loop (critical for the counter logic)
Method 3: Python Script (Cross-Platform)
If you need a solution that works on Windows, Linux, or macOS, a Python script is perfect. It also includes built-in overwrite protection.
import os # Set your target directory and desired base name root_directory = r"C:\Workspace" target_base_name = "scan" # Walk through all subfolders and files for dir_path, _, file_names in os.walk(root_directory): for file_name in file_names: # Split the file name and extension _, file_ext = os.path.splitext(file_name) new_file_name = f"{target_base_name}{file_ext}" old_file_path = os.path.join(dir_path, file_name) new_file_path = os.path.join(dir_path, new_file_name) # Add counter if the file already exists counter = 1 while os.path.exists(new_file_path): new_file_name = f"{target_base_name}-{counter}{file_ext}" new_file_path = os.path.join(dir_path, new_file_name) counter += 1 # Rename the file os.rename(old_file_path, new_file_path)
How it works:
os.walk(root_directory): Traverses every subfolder and file in the target directoryos.path.splitext(file_name): Separates the file name from its extension- The while loop checks for existing files and appends a counter until a unique name is found
Important Notes:
- Backup first! Always make a copy of your files before running any bulk rename script—mistakes can't be undone easily.
- Test with a small folder first : Run the script on a test subset of your files to make sure it behaves as expected.
内容的提问来源于stack exchange,提问作者Syafiq Latif

