Golang : Rename part of filename
This simple tutorial is a code fragment extract from previous tutorial for scanning and renaming partial filenames showing how you can rename part of a filename in Go.
var f os.FileInfo
path := `\targetfile.txt`
// check each file if starts with the word "dumb_"
// depending on your needs
// you can replace HasPrefix with http://golang.org/pkg/strings/#Contains or http://golang.org/pkg/strings/#HasSuffix
if strings.HasPrefix(f.Name(), "dumb_") {
base := filepath.Base(path) // get the file's basename
dir := filepath.Dir(path)
renameto := filepath.Join(dir, strings.Replace(base, "dumb_", "smart_", 1))
os.Rename(path, renameto)
}
See full example at https://www.socketloop.com/tutorials/golang-scan-files-for-certain-pattern-and-rename-part-of-the-files
See also : Golang : Scan files for certain pattern and rename part of the files
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+46.1k Golang : Marshal and unmarshal json.RawMessage struct example
+13.2k Golang : Increment string example
+5k PHP : See installed compiled-in-modules
+9.7k Golang : Translate language with language package example
+30k Golang : How to verify uploaded file is image or allowed file types
+4.8k Python : Convert(cast) bytes to string example
+5.8k Golang : Compound interest over time example
+14.2k Golang : How to filter a map's elements for faster lookup
+8.5k Golang : Find duplicate files with filepath.Walk
+12k Golang : List running EC2 instances and descriptions
+6.1k Golang : Test input string for unicode example
+4.5k MariaDB/MySQL : Form select statement or search query with Chinese characters