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
+6.3k Golang : Handling image beyond OpenCV video capture boundary
+22k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+29.2k Golang : Saving(serializing) and reading file with GOB
+21.7k Golang : Use TLS version 1.2 and enforce server security configuration over client
+10.6k Golang : Get currencies exchange rates example
+5.1k JavaScript/JQuery : Redirect page examples
+44.6k Golang : Use wildcard patterns with filepath.Glob() example
+9.2k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+13.3k Golang : Increment string example
+6.5k Golang : Warp text string by number of characters or runes example
+17.1k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+27.7k Golang : Decode/unmarshal unknown JSON data type with map[string]interface