Created
November 6, 2015 19:16
-
-
Save colinbate/855214279268042f90bf to your computer and use it in GitHub Desktop.
Find-UnusedFilename
This file contains 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 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