Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time
function Get-AvailableFileName
Gets an avaialble file name
Test a path to see if it exists. If a file already exists, it
will append '(x)' to the file name, where x is the next available number.
Get-AvailableFileName -Path C:\Temp\SomeFilePath.txt
# Specifies a path to one or more locations.
[Parameter(Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[Alias("PSPath", "FullName")]
begin {}
foreach ($newName in $Path)
if (Test-Path -Path $newName)
$folder = Split-Path -Path $newName -Parent
$basename = [IO.Path]::GetFileNameWithoutExtension($newName)
$extension = [IO.Path]::GetExtension($newName)
$counter = 1
while (Test-Path -Path $newName)
$newName = "$($folder)\$($basename)($counter)$($extension)"
Write-Output -InputObject $newName
end {}