Skip to content

Instantly share code, notes, and snippets.

@colinbate
Created November 6, 2015 19:16
Show Gist options
  • Save colinbate/855214279268042f90bf to your computer and use it in GitHub Desktop.
Save colinbate/855214279268042f90bf to your computer and use it in GitHub Desktop.
Find-UnusedFilename
function Find-UnusedFilename
{
param (
[string]$folder,
[string]$filename
)
if (-not (Test-Path (Join-Path $folder $filename)))
{
return Join-Path $folder $filename
}
$counter = 1
$ext = [System.IO.Path]::GetExtension($filename)
$basename = [System.IO.Path]::GetFileNameWithoutExtension($filename)
do
{
$filename = "$basename-$counter$ext"
$counter++
} while (Test-Path (Join-Path $folder $filename))
return Join-Path $folder $filename
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment