Skip to content

Commit

Permalink
✨ Feat: dir_auto 模式下支持按照正则表达式解析时间
Browse files Browse the repository at this point in the history
  • Loading branch information
soxft committed Nov 12, 2024
1 parent 4ea5a29 commit 1a3d3ae
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
Binary file modified .DS_Store
Binary file not shown.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ Usage of quexif:
操作模式: qumagie (QuMagie 备份照片处理), dir (指定文件夹批量修改 EXIF时间), dir_auto (按照路径推导时间), read (读取目录或文件的 EXIF 时间信息) (default "read")
-p string
文件夹路径 (default "./")
-r string
仅 dir_auto 模式适用, 在解析时间之前, 从文件名中提取时间的正则表达式
-skip
跳过安全询问, 直接执行
-t string
日期时间模板, 默认为 '2006-01-02 15.04.05' 请参照 Golang 时间 layout 设置, 不适用于 QuMagie 模式 (default "2006-01-02 15.04.05")

```

- 批量修改某个目录及其子目录下的所有图片为指定时间
Expand Down Expand Up @@ -86,7 +89,26 @@ $ ./quexif -m dir -d '2024-11-23' -t '2006-01-02' -p ./pics

$ ./quexif -m dir_auto -t '2006-01-02' -p ./pics

# -m dir_date 表示修改目录下的所有图片, 并按照文件名推导时间
# -m dir_auto 表示修改目录下的所有图片, 并按照文件名推导时间
# -t '2006-01-02' 表示时间格式为 2006-01-02
# -p ./pics 表示目录为 ./pics
```

- 使用正则, 批量推导文件名中的时间

```shell
# 例如您的目录结构为
.
├── wechat_2022-11-23.jpg
├── wechat_2023-11-23.jpg
│── wechat_2024-11-23.jpg

此时您可以执行如下脚本, 脚本将自动根据正则表达式提取时间字符串, 按照 dateTpl 进行时间解析后写入 Exif

$ ./quexif -m dir_auto -r "wechat_(.*).jpg" -t "2006-01-02" -p ./pics

# -m dir_auto 表示修改目录下的所有图片, 并按照文件名推导时间
# -r 为正则表达式, 例如 "wechat_(.*).jpg" 表示提取 wechat_2022-11-23 10.22.23.jpg 中的 2022-11-23 10.22.23
# -t '2006-01-02' 表示时间格式为 2006-01-02
# -p ./pics 表示目录为 ./pics
```
Expand Down
21 changes: 21 additions & 0 deletions dir_auto/dir_auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"photo_exif_do/fg"
"photo_exif_do/x_exif"
"regexp"
"strings"
"time"
)
Expand Down Expand Up @@ -71,6 +72,26 @@ func tryGetDate(path string) (time.Time, error) {
sp := strings.Split(path, "/")

for i := len(sp) - 1; i >= 0; i-- {
if fg.Regex != "" {
// 尝试进行正则匹配
re := regexp.MustCompile(fg.Regex)

if re.MatchString(sp[i]) {
timeStr := re.FindStringSubmatch(sp[i])

for _, v := range timeStr {
parsedTime, err := time.Parse(fg.DateTpl, v)
if err == nil {
return parsedTime, nil
}
}

continue
}

continue
}

// 对于后缀
if i == len(sp)-1 && strings.Contains(sp[i], ".") {
sp := strings.Split(sp[i], ".")
Expand Down
2 changes: 2 additions & 0 deletions fg/fg.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ var DateTime string
var DateTpl = "2006-01-02 15.04.05"
var Force bool
var SkipSafeQA bool
var Regex string

// Parse 解析命令行参数
func Parse() {
flag.StringVar(&Mode, "m", "read", "操作模式: qumagie (QuMagie 备份照片处理), dir (指定文件夹批量修改 EXIF时间), dir_auto (按照路径推导时间), read (读取目录或文件的 EXIF 时间信息)")
flag.StringVar(&Path, "p", "./", "文件夹路径")
flag.StringVar(&DateTime, "d", "", "日期时间")
flag.StringVar(&DateTpl, "t", "2006-01-02 15.04.05", "日期时间模板, 默认为 '2006-01-02 15.04.05' 请参照 Golang 时间 layout 设置, 不适用于 QuMagie 模式")
flag.StringVar(&Regex, "r", "", "仅 dir_auto 模式适用, 在解析时间之前, 从文件名中提取时间的正则表达式")
flag.BoolVar(&Force, "f", false, "强制执行, 不会检查是否已经有日期")
flag.BoolVar(&SkipSafeQA, "skip", false, "跳过安全询问, 直接执行")

Expand Down

0 comments on commit 1a3d3ae

Please sign in to comment.