Skip to content

Instantly share code, notes, and snippets.

@sean-m
Created February 2, 2017 21:05
Show Gist options
  • Save sean-m/fed257411bf631b015daa5aacb36f937 to your computer and use it in GitHub Desktop.
Save sean-m/fed257411bf631b015daa5aacb36f937 to your computer and use it in GitHub Desktop.
Increments a number in a file path until the path is unique. Got tired of writing this every time I need it.
function Make-UniquePath {
param([string]$path)
$ptemp = $path
$inc = 1;
$ext = [IO.Path]::GetExtension($path)
while (Test-Path $ptemp) {
$tmp_ext = [String]::Concat("-",$inc.ToString("D2"),$ext)
if ([String]::IsNullOrEmpty($ext)) {
$ptemp = [String]::Concat($path,$tmp_ext)
} else {
$ptemp = $path.Replace($ext,$tmp_ext)
}
$inc++
}
return $ptemp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment