如何通过脚本批量修改文件名并基于原文件名进行命名?
Hey there! Let's sort out your batch file renaming task where you want to modify filenames based on their original names. I'll break down practical solutions for different operating systems so you can pick what fits your setup perfectly.
Windows (PowerShell) Solution
PowerShell is built right into Windows and ideal for this kind of task. Let's use a common example: adding a vacation_ prefix and _2024 suffix to all .jpg files in a folder. Here's how to do it safely:
# First, navigate to your target folder cd "C:\Your\Target\Folder\Path" # Preview changes first with -WhatIf (no actual renaming happens yet!) Get-ChildItem -Filter "*.jpg" | ForEach-Object { $newName = "vacation_$($_.BaseName)_2024$($_.Extension)" Rename-Item -Path $_.FullName -NewName $newName -WhatIf }
Quick Tips:
- Remove the
-WhatIfflag once you're happy with the preview to apply the changes. - Tweak the
$newNamestring to match your needs:$_.BaseNamegrabs the original filename without its extension$_.Extensionpreserves the original file type (like.jpg,.txt)
- For regex-based replacements (e.g., swapping "old_" with "new_"), use
-replace:$newName = $_.BaseName -replace "old_", "new_" + $_.Extension
Linux/macOS (Bash) Solution
On Unix-like systems, a bash script gets the job done smoothly. Let's reuse the same prefix/suffix example for .jpg files:
# Navigate to your target folder cd /path/to/your/target/folder # Preview changes first with echo for file in *.jpg; do # Skip if it's a directory [ -f "$file" ] || continue # Split filename into base and extension base=$(basename "$file" .jpg) ext=".jpg" new_name="vacation_${base}_2024${ext}" echo "Preview: Renaming $file to $new_name" # Uncomment the line below to apply changes # mv "$file" "$new_name" done
Quick Tips:
- Remove the
echoline and uncommentmvto start renaming for real. - For a script that works with any file extension, use parameter expansion:
for file in *; do [ -f "$file" ] || continue base="${file%.*}" ext="${file##*.}" # Handle files without extensions if [ "$base" = "$file" ]; then new_name="vacation_${base}_2024" else new_name="vacation_${base}_2024.${ext}" fi mv "$file" "$new_name" done - Use
sedfor regex swaps, like replacing "summer" with "winter":new_name=$(echo "$file" | sed 's/summer/winter/')
Cross-Platform Python Script
If you need something that works everywhere (Windows, Linux, macOS), Python is a reliable choice. This script lets you define your renaming logic clearly:
import os # Set your target folder path folder_path = "/path/to/your/target/folder" # Loop through all items in the folder for filename in os.listdir(folder_path): file_path = os.path.join(folder_path, filename) # Skip directories if os.path.isdir(file_path): continue # Split filename into base and extension base, ext = os.path.splitext(filename) # Define your new naming rule here new_filename = f"vacation_{base}_2024{ext}" # Preview changes first print(f"Preview: Renaming {filename} to {new_filename}") # Uncomment below to apply changes # os.rename(file_path, os.path.join(folder_path, new_filename))
Quick Tips:
- Adjust the
new_filenameline to match your exact renaming needs. - For regex replacements, use the
remodule:import re new_base = re.sub(r"old_pattern", "new_pattern", base) new_filename = f"{new_base}{ext}" - Critical: Always back up your files before running any batch renaming script—better safe than sorry!
内容的提问来源于stack exchange,提问作者MN65

