- 1 year ago
Duplicate and rename by increment of 1
Comment
1 / 1
To duplicate a file and rename it with an increment of 1 using PowerShell, you can use the following script:
powershell$filePath = "C:\Path\to\your\file.ext"
# Determine the new file name
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
$extension = [System.IO.Path]::GetExtension($filePath)
$newFileName = $fileName + "_1" + $extension
# Determine the new file path
$folderPath = [System.IO.Path]::GetDirectoryName($filePath)
$newFilePath = Join-Path -Path $folderPath -ChildPath $newFileName
# Copy the file to the new path
Copy-Item -Path $filePath -Destination $newFilePath
Write-Host "File duplicated and renamed."
Make sure to replace "C:\Path\to\your\file.ext"
with the actual path to your file. The script performs the following steps:
- Sets the
$filePath
variable to the path of the file you want to duplicate and rename. - Uses the
GetFileNameWithoutExtension
method to extract the file name without the extension from the original file path. - Uses the
GetExtension
method to extract the file extension from the original file path. - Constructs the new file name by appending "_1" to the original file name and adding the original file extension.
- Determines the folder path of the original file using the
GetDirectoryName
method. - Constructs the new file path by combining the folder path and the new file name using the
Join-Path
cmdlet. - Copies the original file to the new file path using the
Copy-Item
cmdlet. - Displays a completion message.
When you execute the script, it will duplicate the file, rename it with an increment of 1, and save it in the same folder as the original file. The new file will have a name in the format "originalFileName_1.extension".