mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
add Replace function for gfile
This commit is contained in:
@ -7,6 +7,7 @@
|
||||
package gfile
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/text/gstr"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@ -160,3 +161,19 @@ func GetBinContentsByTwoOffsetsByPath(path string, start int64, end int64) []byt
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Replace replaces content for files under <path>.
|
||||
// The parameter <pattern> specifies the file pattern which matches to be replaced.
|
||||
// It does replacement recursively if given parameter <recursive> is true.
|
||||
func Replace(search, replace, path, pattern string, recursive ...bool) error {
|
||||
files, err := ScanDir(path, pattern, recursive...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, file := range files {
|
||||
if err = PutContents(file, gstr.Replace(GetContents(file), search, replace)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user