- 1 year ago
Is it possible to append prefix to files names taken from folder name, automatically? (Windows)
Comment
1 / 1
Yes, it is possible to append a prefix to file names automatically based on the folder name in Windows. You can achieve this using a PowerShell script. Here's an example:
powershell$folderPath = "C:\Path\To\Folder"
# Get the folder name from the path
$folderName = (Get-Item -Path $folderPath).Name
# Get all files in the folder
$files = Get-ChildItem -Path $folderPath -File
# Iterate through each file and append the folder name as a prefix
foreach ($file in $files) {
$newFileName = $folderName + "_" + $file.Name
$newFilePath = Join-Path -Path $folderPath -ChildPath $newFileName
Rename-Item -Path $file.FullName -NewName $newFilePath
}
Make sure to replace "C:\Path\To\Folder"
with the actual path of the folder you want to process. The script retrieves the folder name from the folder path, gets all the files in the folder using Get-ChildItem
, and then iterates through each file. It constructs a new file name by appending the folder name as a prefix, and renames the file using Rename-Item
.
Please note that running this script will permanently rename the files in the specified folder. Make sure to test it on a sample folder or make backups of your files before executing it on a production folder.