aboutsummaryrefslogtreecommitdiff
path: root/cmd/fixpths/main.go
blob: 673027668742b1a76da5d2d6e6ca68744ba822bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main

import (
	"flag"
	"fmt"
	"io/fs"
	"log"
	"os"
	"path/filepath"
	"regexp"
)

func main() {
	fl := flag.NewFlagSet("fixpths", flag.ExitOnError)
	pth := fl.String("p", "", "Path to scan and fix windows incompatible chars on")
	rename := fl.Bool("rename", false, "Whether or not to actually rename the files to work on windows")
	fl.Parse(os.Args[1:])

	if *pth == "" {
		fmt.Println("need a path")
		os.Exit(2)
	}

	invalidChars := regexp.MustCompile(`(<|>|:|\||"|\\|\*|\?)`)

	err := filepath.WalkDir(*pth, func(path string, d fs.DirEntry, err error) error {
		if d.IsDir() {
			return nil
		}

		if invalidChars.MatchString(path) {
			if !*rename {
				fmt.Printf("%s\n",
					invalidChars.ReplaceAllString(path, "\033[1;31m${1}\033[0m"))
			}

			if *rename {
				npath := invalidChars.ReplaceAllString(path, "_")
				err := os.Rename(path, npath)
				fmt.Printf("%s->%s\n", path, npath)
				if err != nil {
					log.Println(err)
				}
			}
		}

		return nil
	})

	if err != nil {
		log.Println(err)
	}

}