Filtering file name symbols using golang
Recently, the customer service department has encountered a problem: it’s connected with a third party to mail files. Every time you send a file, you need to upload it. It happens that the name of the uploaded file doesn’t support special symbols such as “% &”. It’s too troublesome to change it manually, so I think of a small tool to use go
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
func main() {
var yes int
fmt.Println (enter 1 to rename the current folder file, otherwise enter 2:)
fmt.Scanln(&yes)
if yes != 1 {
return
}
//return
path := "./"
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
if f.IsDir() {
return
}
fmt.Println(f.Name())
//To define rules here, I need to filter from (2020) 0854-0036-1355 to 2020085400361355
str1 := strings.Replace(f.Name(), "-", "", -1)
str2 := strings.Replace(str1, "(", "", -1)
str3 := strings.Replace(str2, ")", "", -1)
//os.Rename(path+"\"+f.Name(), path+"\"+
// fmt.Sprintf(str3, index))
os.Rename(path+"\"+f.Name(), path+"\"+
fmt.Sprintf(str3))
}
}
The advantage of go is cross platform and go build directly xxx.go And then send it to colleagues to run it!