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
+4.5k Python : Find out the variable type and determine the type with simple test
+19.2k Golang : Example for DSA(Digital Signature Algorithm) package functions
+6.7k Golang : How to call function inside template with template.FuncMap
+14.7k Golang : How to check if IP address is in range
+10.2k Golang : Create matrix with Gonum Matrix package example
+8k Your page has meta tags in the body instead of the head
+9.8k Golang : Print how to use flag for your application example
+7.2k Golang : Get YouTube playlist
+6.8k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+5.8k Java : Human readable password generator