Skip to content

Instantly share code, notes, and snippets.

@agatemosu
Created June 18, 2024 18:37
Show Gist options
  • Save agatemosu/3ff389f004806c0a65d7efa1328c86a0 to your computer and use it in GitHub Desktop.
Save agatemosu/3ff389f004806c0a65d7efa1328c86a0 to your computer and use it in GitHub Desktop.
function Add-Copy {
param (
[Parameter(
Mandatory = $true,
Position = 0,
HelpMessage = "The path of the file to clone."
)]
[ValidateNotNullOrWhiteSpace()]
[string]$OriginalFilePath,
[Parameter(
Mandatory = $true,
Position = 1,
HelpMessage = "The number of copies to create."
)]
[ValidateRange(2, [int]::MaxValue)]
[int]$NumberOfCopies,
[Parameter(
Mandatory = $false,
Position = 2,
HelpMessage = "The destination directory where copies will be placed."
)]
[ValidateNotNullOrWhiteSpace()]
[string]$DestinationDirectory
)
if (-not (Test-Path $OriginalFilePath)) {
Write-Output "The file '$OriginalFilePath' does not exist."
return
}
if (-not $DestinationDirectory) {
$DestinationDirectory = Split-Path $OriginalFilePath -Parent
}
if (-not (Test-Path $DestinationDirectory)) {
Write-Output "The folder '$DestinationDirectory' does not exist."
return
}
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($OriginalFilePath)
$fileExtension = [System.IO.Path]::GetExtension($OriginalFilePath)
for ($i = 1; $i -le $NumberOfCopies; $i++) {
$newFileName = "{0}{1}{2}" -f $fileName, $i, $fileExtension
$destinationFilePath = Join-Path -Path $DestinationDirectory -ChildPath $newFileName
Copy-Item -Path $OriginalFilePath -Destination $destinationFilePath
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment