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 ( import (
"errors" "errors"
"os" "os"
"strings"
) )
func IsFileExists(fileLocation string) bool { func IsFileExists(fileLocation string) bool {
@ -12,3 +13,13 @@ func IsFileExists(fileLocation string) bool {
return true 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 package files
import ( import "os"
"os"
"strings"
)
func MkdirParent(fileLocation string) error { func MkdirParent(fileLocation string) error {
fileLocation = strings.ReplaceAll(fileLocation, "\\", "/") parentDir := GetParentDir(fileLocation)
indexOfSlash := strings.LastIndex(fileLocation, "/") if parentDir != "" {
if indexOfSlash > 0 { parentDirCreationErr := os.MkdirAll(parentDir, os.ModeDir)
parentDirLocation := fileLocation[:indexOfSlash]
parentDirCreationErr := os.MkdirAll(parentDirLocation, os.ModeDir)
if parentDirCreationErr != nil { if parentDirCreationErr != nil {
return parentDirCreationErr return parentDirCreationErr