forked from go/golangs_learn
VIVIMAN
2 years ago
2 changed files with 55 additions and 0 deletions
@ -0,0 +1,54 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"io/ioutil" |
||||
|
"os" |
||||
|
"strconv" |
||||
|
"strings" |
||||
|
) |
||||
|
|
||||
|
var replaceword = []string{"_xlnm.Print_Title", "_xlnm.Print_Area"} |
||||
|
|
||||
|
func main() { |
||||
|
|
||||
|
pwd, _ := os.Getwd() |
||||
|
//获取文件或目录相关信息
|
||||
|
fileInfoList, err := ioutil.ReadDir(pwd) |
||||
|
if err != nil { |
||||
|
err = fmt.Errorf("发生异常:%s", err) |
||||
|
} |
||||
|
fmt.Println(len(fileInfoList)) |
||||
|
for i := range fileInfoList { |
||||
|
name := fileInfoList[i].Name() |
||||
|
if strings.HasSuffix(name, "xlsx") || strings.HasSuffix(name, "xls") { |
||||
|
fmt.Printf("%s 执行 %v \n", name, make(name, "new-"+name)) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func make(inFilePath string, outFilePath string) bool { |
||||
|
f, err := os.Open(inFilePath) |
||||
|
if err != nil { |
||||
|
err = fmt.Errorf("发生异常 Open :%s", err) |
||||
|
return false |
||||
|
} |
||||
|
|
||||
|
fSrc, err := ioutil.ReadAll(f) |
||||
|
if err != nil { |
||||
|
err = fmt.Errorf("发生异常 ReadAll :%s", err) |
||||
|
return false |
||||
|
} |
||||
|
s := string(fSrc) |
||||
|
for _, w := range replaceword { |
||||
|
for i := 0; ; i++ { |
||||
|
ind := strings.Index(s, w) |
||||
|
if ind < 0 { |
||||
|
break |
||||
|
} |
||||
|
s = strings.Replace(s, w, w[:len(w)-1]+strconv.Itoa(i), 1) |
||||
|
} |
||||
|
} |
||||
|
_ = ioutil.WriteFile(outFilePath, []byte(s), os.ModeAppend) |
||||
|
return true |
||||
|
} |
Loading…
Reference in new issue