During my work, I encountered batch modification of file names, so I wrote a small program for this purpose for easy viewing in the future.
usage:
-d "E:\shared\picture material\ps123_20121120_01\background image packaging and download" -p "bg%d"
The code is as follows:
package main import ( "flag" "fmt" "os" "path/filepath" ) func main() { // parse command line parameters var dir string (&dir, "d", "", "directory path") var pattern string (&pattern, "p", "", "name pattern, eg. newname%d") () if dir == "" || pattern == "" { () return } // traverse the folder and get the file path paths := make([]string, 0) (dir, func(path string, info , err error) error { if !() { paths = append(paths, path) } return nil }) // traverse the file path and modify the file name for i, path := range paths { newPath := ((path), (pattern, i+1)+(path)) (path, newPath) } }
Added: Use Golang to make extensive modifications to file names
background
When we download materials, the name of the materials we download often contains the domain name of a certain website as an advertisement. Although it is a free party, it looks very uncomfortable. Next, we use Go language to modify the file name a lot and remove the advertising domain name in the file name.
Code
Function: Circularly traverse the files in this folder, and remove the file suffix name with the word "etc"
import ( "fmt" "io/ioutil" "log" "os" "strings" ) func main() { path:="C:\\Users\\Desktop\\3_2020up to date_Python_(MySQL_SQL_Redis)Detailed database explanation" listFiles(path) } func listFiles(dirname string) { fileInfos, err := (dirname) if err!=nil { (err) } for _, f := range fileInfos { filename := dirname + "\\" + () //Record the file name in the current folder //If the file name contains the following fields, change its file name if ((),"(More resources access:)") { (filename,dirname+"\\"+((),"(More resources access:)","",-1)) } (filename) //Print file address if () { //Discuss whether it is a folder. If it is a folder, continue to call recursively listFiles(filename) } } }
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.