function Get-ResultFilePath {
param (
# Путь к файлу, который копируется
[Parameter(Mandatory = $true)]
[System.String]
$SourcePath,
# Путь к папке, в которую необходимо скопировать файл
[Parameter(Mandatory = $true)]
[System.String]
$DestinationFolder
)
# Вариант 1
$File = Get-Item -Path $SourcePath
$DestinationPath = Join-Path -Path $DestinationFolder -ChildPath "$($File.BaseName)$($File.Extension)"
$i = 1
While (Test-Path -Path $DestinationPath) {
$DestinationPath = Join-Path -Path $DestFolder -ChildPath ($File.BaseName, $File.Extension -join "($i)")
$i++
}
# Вариант 2
# $FileBaseName = [System.IO.Path]::GetFileNameWithoutExtension($SourcePath)
# $FileExtension = [System.IO.Path]::GetExtension($SourcePath)
# $DestinationPath = Join-Path -Path $DestinationFolder -ChildPath "$FileBaseName$FileExtension"
# $i = 1
# While (Test-Path -Path $DestinationPath) {
# $DestinationPath = Join-Path -Path $DestFolder -ChildPath ($FileBaseName, $FileExtension -join "($i)")
# $i++
# }
return $DestinationPath
}
$DestFolder = 'F:\tmp'
$Path = 'C:\tmp\FileName.pdf'
Copy-Item -Path $Path -Destination (Get-ResultFilePath -SourcePath $Path -DestinationFolder $DestFolder)