添加modtime
parent
10fb3551c5
commit
76d9e14852
|
|
@ -0,0 +1,86 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
timeLocation *time.Location
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// 初始化时区
|
||||||
|
var err error
|
||||||
|
timeLocation, err = time.LoadLocation("Local")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("LoadLocation fail: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("start processing")
|
||||||
|
pwd, err := os.Open(".")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("os.Open . error: %v", err)
|
||||||
|
}
|
||||||
|
defer pwd.Close()
|
||||||
|
pwdInfo, err := pwd.Stat()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("pwd.Stat error: %v", err)
|
||||||
|
}
|
||||||
|
processDir(pwdInfo, pwd, []string{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func processDir(pwdInfo os.FileInfo, pwd *os.File, paths []string) {
|
||||||
|
fmt.Println("processing dir ", pwdInfo.Name())
|
||||||
|
if !pwdInfo.IsDir() {
|
||||||
|
log.Fatalf("input file %s is not a Dir", pwdInfo.Name())
|
||||||
|
}
|
||||||
|
if pwd == nil {
|
||||||
|
var err error
|
||||||
|
path := strings.Join(paths, "/")
|
||||||
|
pwd, err = os.Open(path + "/" + pwdInfo.Name())
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("os.Open %v %s error: %v", paths, pwdInfo.Name(), err)
|
||||||
|
}
|
||||||
|
defer pwd.Close()
|
||||||
|
}
|
||||||
|
fileInfos, err := pwd.Readdir(0)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("pwd %s Readdir error: %v", pwd.Name(), err)
|
||||||
|
}
|
||||||
|
for _, fileInfo := range fileInfos {
|
||||||
|
if fileInfo.IsDir() {
|
||||||
|
processDir(fileInfo, nil, append(paths, pwdInfo.Name()))
|
||||||
|
} else {
|
||||||
|
ext := path.Ext(fileInfo.Name())
|
||||||
|
switch ext {
|
||||||
|
case ".mp4":
|
||||||
|
processMP4(fileInfo, append(paths, pwdInfo.Name()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func processMP4(fileInfo os.FileInfo, paths []string) {
|
||||||
|
filename := fileInfo.Name()
|
||||||
|
path := strings.Join(paths, "/")
|
||||||
|
if path != "." {
|
||||||
|
filename = path + "/" + filename
|
||||||
|
}
|
||||||
|
t, err := time.ParseInLocation("20060102150405", strings.Split(fileInfo.Name(), "-")[1][:14], timeLocation)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Parse time fail from filename ", fileInfo.Name())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println("processing file ", filename, t.Format(time.RFC3339))
|
||||||
|
err = os.Chtimes(filename, t, t)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Chtimes file %s fail: %v\n", filename, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue