feature: added 'GetParentDir' to files

master
amorozov 2024-06-21 18:39:35 +03:00
parent f8094df331
commit 6019beffe5
2 changed files with 15 additions and 9 deletions

View File

@ -3,6 +3,7 @@ package files
import (
"errors"
"os"
"strings"
)
func IsFileExists(fileLocation string) bool {
@ -12,3 +13,13 @@ func IsFileExists(fileLocation string) bool {
return true
}
func GetParentDir(fileLocation string) string {
fileLocation = strings.ReplaceAll(fileLocation, "\\", "/")
indexOfSlash := strings.LastIndex(fileLocation, "/")
if indexOfSlash > 0 {
return fileLocation[:indexOfSlash]
}
return ""
}

View File

@ -1,16 +1,11 @@
package files
import (
"os"
"strings"
)
import "os"
func MkdirParent(fileLocation string) error {
fileLocation = strings.ReplaceAll(fileLocation, "\\", "/")
indexOfSlash := strings.LastIndex(fileLocation, "/")
if indexOfSlash > 0 {
parentDirLocation := fileLocation[:indexOfSlash]
parentDirCreationErr := os.MkdirAll(parentDirLocation, os.ModeDir)
parentDir := GetParentDir(fileLocation)
if parentDir != "" {
parentDirCreationErr := os.MkdirAll(parentDir, os.ModeDir)
if parentDirCreationErr != nil {
return parentDirCreationErr