feature: updated GetFirstIndexedNonExistingFilePath logic

master
amorozov 2024-06-25 15:52:10 +03:00
parent 18f289fe90
commit bbe979b97b
2 changed files with 18 additions and 3 deletions

View File

@ -17,15 +17,26 @@ func IsFileExists(fileLocation string) bool {
func GetFirstIndexedNonExistingFilePath(parentDir string, fileBaseName string) string {
filePath := filepath.Join(parentDir, fileBaseName)
index := 0
if !IsFileExists(filePath) {
return filePath
}
fileIndex := 0
fileBaseNameWithoutExtension := GetSimpleNameWithoutExtension(fileBaseName)
fileExtension := GetExtension(fileBaseName)
if fileExtension != "" {
fileExtension = "." + fileExtension
}
for {
if !IsFileExists(filePath) {
break
}
index++
fileIndex++
filePath = filepath.Join(parentDir, fmt.Sprintf("%s-%d", fileBaseName, index))
filePath = filepath.Join(parentDir, fmt.Sprintf("%s-%d%s", fileBaseNameWithoutExtension, fileIndex, fileExtension))
}
return filePath

View File

@ -26,6 +26,10 @@ func GetSimpleName(filePath string) string {
func GetExtension(filePath string) string {
split := strings.Split(filePath, ".")
if len(split) == 1 {
return ""
}
return split[len(split)-1]
}