incredible-go-core/pkg/io/files/file_exists_checker.go

33 lines
537 B
Go
Raw Normal View History

2024-06-20 19:06:29 +02:00
package files
import (
"errors"
"fmt"
2024-06-20 19:06:29 +02:00
"os"
"path/filepath"
2024-06-20 19:06:29 +02:00
)
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)
index := 0
for {
if !IsFileExists(filePath) {
break
}
index++
filePath = filepath.Join(parentDir, fmt.Sprintf("%s-%d", fileBaseName, index))
}
return filePath
}