Golang : Scan files for certain pattern and rename part of the files
It is useful to have the ability to scan filenames that match certain pattern and rename part of the filenames in a directory. This tutorial builds on from the previous tutorial on how to rename a file with Go.
This program below will first scan the given path for files with matching names and rename the first :
package main
import (
"flag"
"os"
"path/filepath"
"strings"
)
var flagPath = flag.String("path", "", "path to walk in search for dumb_* files.")
func partialrename(path string, f os.FileInfo, err error) (e error) {
// check each file if starts with the word "dumb_"
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)
}
return
}
func init() {
flag.Parse()
}
func main() {
// check if the user specify a path
if *flagPath == "" {
flag.Usage() // if no, prompt usage
os.Exit(0) // and exit
}
// walk through the files in the given path and perform partialrename()
// function
filepath.Walk(*flagPath, partialrename)
}
Test output :
ls -la /Users/sweetlogic/testdir
total 0
drwxr-xr-x 5 sweetlogic staff 170 Oct 23 15:13 .
drwxr-xr-x+ 39 sweetlogic staff 1326 Oct 23 15:13 ..
-rw-r--r-- 1 sweetlogic staff 0 Oct 23 15:13 dumb3.txt
-rw-r--r-- 1 sweetlogic staff 0 Oct 23 15:13 dumb_1.txt
-rw-r--r-- 1 sweetlogic staff 0 Oct 23 15:13 dumb_2.txt
and after executing ./renamepartial -path="/Users/sweetlogic/testdir"
ls -la /Users/sweetlogic/testdir
total 0
drwxr-xr-x 5 sweetlogic staff 170 Oct 23 15:14 .
drwxr-xr-x+ 39 sweetlogic staff 1326 Oct 23 15:13 ..
-rw-r--r-- 1 sweetlogic staff 0 Oct 23 15:13 dumb3.txt
-rw-r--r-- 1 sweetlogic staff 0 Oct 23 15:13 smart_1.txt
-rw-r--r-- 1 sweetlogic staff 0 Oct 23 15:13 smart_2.txt
the dumb3.txt
file remains the same because its name doesn't match the strings.HasPrefix() patter of "dumb_".
Hope this tutorial can be useful for your work.
See also : Golang : Rename file
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
+19.3k Golang : Close channel after ticker stopped example
+17.4k Golang : How to make a file read only and set it to writable again?
+7.4k Golang : Error reading timestamp with GORM or SQL driver
+13.2k Golang : Read XML elements data with xml.CharData example
+14.9k Golang : Get all local users and print out their home directory, description and group id
+19.8k Golang : Check if os.Stdin input data is piped or from terminal
+5.4k Swift : Get substring with rangeOfString() function example
+10k Golang : cannot assign type int to value (type uint8) in range error
+5.5k Fix yum-complete-transaction error
+6.1k Unix/Linux : Use netstat to find out IP addresses served by your website server
+42.6k Golang : Convert []byte to image
+6.5k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?