44 lines
825 B
Go
44 lines
825 B
Go
package files
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func IsFileExists(fileLocation string) bool {
|
|
if _, err := os.Stat(fileLocation); errors.Is(err, os.ErrNotExist) {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func GetFirstIndexedNonExistingFilePath(parentDir string, fileBaseName string) string {
|
|
filePath := filepath.Join(parentDir, fileBaseName)
|
|
|
|
if !IsFileExists(filePath) {
|
|
return filePath
|
|
}
|
|
|
|
fileIndex := 0
|
|
fileBaseNameWithoutExtension := GetSimpleNameWithoutExtension(fileBaseName)
|
|
fileExtension := GetExtension(fileBaseName)
|
|
if fileExtension != "" {
|
|
fileExtension = "." + fileExtension
|
|
}
|
|
|
|
for {
|
|
if !IsFileExists(filePath) {
|
|
break
|
|
}
|
|
|
|
fileIndex++
|
|
|
|
filePath = filepath.Join(parentDir, fmt.Sprintf("%s-%d%s", fileBaseNameWithoutExtension, fileIndex, fileExtension))
|
|
}
|
|
|
|
return filePath
|
|
}
|