- 1 year ago
How to edit file extensions across multiple subfolders using powershell or other
To edit file extensions across multiple subfolders using PowerShell, you can use the Get-ChildItem
cmdlet to recursively traverse the directory structure, and then modify the file extensions using the Rename-Item
cmdlet. Here's an example:
powershell$rootPath = "C:\Path\to\root\folder"
$oldExtension = ".old"
$newExtension = ".new"
# Get all files with the old extension in the specified root folder and its subfolders
$files = Get-ChildItem -Path $rootPath -Filter "*$oldExtension" -File -Recurse
# Rename files by replacing the old extension with the new extension
$files | ForEach-Object {
$newName = $_.Name -replace [regex]::Escape($oldExtension), $newExtension
$_ | Rename-Item -NewName $newName -Force
}
In this example, you need to set the $rootPath
variable to the root folder where you want to modify the file extensions. Specify the $oldExtension
variable with the current extension you want to change, and set the $newExtension
variable with the desired new extension.
The script recursively searches for files with the old extension in the specified root folder and its subfolders using Get-ChildItem
. Then, it renames each file by replacing the old extension with the new extension using Rename-Item
.
Save the script as a .ps1
file, open a PowerShell session, navigate to the directory where the script is located, and execute it. It will modify the file extensions across the specified root folder and its subfolders.
Please note that modifying file extensions can have consequences, and it's recommended to perform a backup of your files before running such scripts.