Created
February 2, 2017 21:05
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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