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
+10.6k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+9.6k Android Studio : Indicate progression with ProgressBar example
+14.9k Golang : Get URI segments by number and assign as variable example
+9.6k Golang : Convert(cast) string to int64
+9.2k Golang : Serving HTTP and Websocket from different ports in a program example
+8.2k Golang : Check from web if Go application is running or not
+23.7k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+19.7k Golang : Get current URL example
+8.2k Android Studio : Rating bar example
+5.1k Golang : Display packages names during compilation
+6.1k nginx : force all pages to be SSL
+7.5k Golang : Gorrila set route name and get the current route name