66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package eliofile
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"gittea.marcokittel.de/elio/eliotools/tools"
|
|
)
|
|
|
|
type filedata struct {
|
|
filename string
|
|
dt time.Time
|
|
}
|
|
type ElioHandleFunc func(filename string) bool
|
|
|
|
type ElioDateiFoo struct {
|
|
process ElioHandleFunc
|
|
dir string
|
|
}
|
|
|
|
func NewElioDateiFoo(dir string, process ElioHandleFunc) *ElioDateiFoo {
|
|
df := ElioDateiFoo{dir: dir,
|
|
process: process,
|
|
}
|
|
return &df
|
|
}
|
|
|
|
func (f *ElioDateiFoo) ScanCsv() {
|
|
files, err := os.ReadDir(f.dir)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
filemap := make(map[string]filedata)
|
|
for _, file := range files {
|
|
if !strings.Contains(file.Name(), "csv") {
|
|
continue
|
|
}
|
|
tl, err := tools.ExtractDateAndConvertToDate(file.Name())
|
|
if err != nil {
|
|
continue
|
|
}
|
|
warehouse := tools.ExtractWarehouseAndType(file.Name())
|
|
value, ok := filemap[warehouse]
|
|
if !ok {
|
|
filemap[warehouse] = filedata{filename: file.Name(), dt: tl}
|
|
} else {
|
|
if tl.Unix() >= value.dt.Unix() {
|
|
err := os.Rename(value.filename, value.filename+".old")
|
|
if err != nil {
|
|
fmt.Printf("Datei %s konnte nicht umbenannt werden!!!", value.filename)
|
|
}
|
|
filemap[warehouse] = filedata{filename: file.Name(), dt: tl}
|
|
}
|
|
}
|
|
}
|
|
for k, v := range filemap {
|
|
fmt.Printf("%s %s", k, v)
|
|
}
|
|
// f.process(file.Name())
|
|
}
|