github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/file/bounds.go (about)

     1  package file
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"path/filepath"
     7  )
     8  
     9  // LatestFileInFolder returns the latest (alphabetically) file in the folder
    10  func LatestFileInFolder(path string) (string, error) {
    11  	files, err := os.ReadDir(path)
    12  	if err != nil {
    13  		return "", err
    14  	}
    15  	if len(files) == 0 {
    16  		return "", errors.New("No files found in folder " + path)
    17  	}
    18  	return filepath.Join(path, files[len(files)-1].Name()), nil
    19  }
    20  
    21  // EarliestFileInFolder returns the first (alphabetically) file in the folder
    22  func EarliestFileInFolder(path string) (string, error) {
    23  	files, err := os.ReadDir(path)
    24  	if err != nil {
    25  		return "", err
    26  	}
    27  	if len(files) == 0 {
    28  		return "", errors.New("No files found in folder " + path)
    29  	}
    30  	return filepath.Join(path, files[0].Name()), nil
    31  }