Windows下面替换命令实现,最新修复一个BUG
来源:互联网 发布:美术教程视频软件 编辑:程序博客网 时间:2024/06/05 20:58
package mainimport ("bufio""bytes""flag""fmt""io/ioutil""os""path/filepath""regexp""strings")var (dir stringfile stringoldstr stringnewstr stringReg *regexp.RegexpErr errortest boolreadall bool)func main() {flag.StringVar(&dir, "d", "./", "-d directory")flag.StringVar(&file, "f", "", "-f *.ini")flag.StringVar(&oldstr, "s", "", "-s oldstr")flag.StringVar(&newstr, "r", "", "-r newstr")flag.BoolVar(&test, "t", false, "-t true")flag.BoolVar(&readall, "a", false, "-a true")flag.Parse()if file == "" {fmt.Printf("Args Error: %s must not null\n", "-f")return}if oldstr == "" || newstr == "" {if !test {fmt.Printf("Args Error: %s and %s must not null.\n\n", "-s", "-r")flag.Usage()return}}if strings.Index(file, "*") == 0 {file = "." + file} else {file = "^" + file}file += "$"Reg, Err = regexp.Compile(file)if Err != nil {fmt.Printf("Regexp string error.\nError info: %s\n", Err)return}err := filepath.Walk(dir, walk)if err != nil {fmt.Println(err)}}func walk(root string, info os.FileInfo, err error) error {root = filepath.ToSlash(root)if err != nil {return err}if info.IsDir() || !Reg.MatchString(info.Name()) {return nil}if test {fmt.Println(root)return nil}if readall {ReadAll(root)} else {path := filepath.Dir(root) + "/." + filepath.Base(root)tmp := fmt.Sprintf("%s.tmp", path)if err := ReadLine(root, tmp); err != nil {return nil}os.Remove(root)os.Rename(tmp, root)}return nil}func ReadLine(root, tmp string) error {File, err := os.Open(root)if err != nil {fmt.Printf("Open file %s faild.\nError Info: %s\n", root, err)return err}defer File.Close()tmpFile, err := os.Create(tmp)if err != nil {fmt.Printf("Create temporary files for %s faild.\nError info %s\n", root, err)return err}defer tmpFile.Close()buf := bufio.NewReader(File)var num int = 0for {line, err := buf.ReadSlice('\n')num += bytes.Count(line, []byte(oldstr))if err != nil {if err.Error() == "EOF" {line = bytes.Replace(line, []byte(oldstr), []byte(newstr), -1)tmpFile.Write(line)break}fmt.Printf("Read %s error.\nError Info: %s\n", root, err)return nil}line = bytes.Replace(line, []byte(oldstr), []byte(newstr), -1)tmpFile.Write(line)}if num > 0 {fmt.Printf("File: %s Replace: %d\n", root, num)}return nil}func ReadAll(root string) {body, err := ioutil.ReadFile(root)if err != nil {fmt.Printf("Read file %s faild.\nError Info: %s\n", root, err)return}num := bytes.Count(body, []byte(oldstr))body = bytes.Replace(body, []byte(oldstr), []byte(newstr), -1)File, err := os.Create(root)if err != nil {fmt.Printf("Create replace file %s faild.\nError Info: %s\n", root, err)return}defer File.Close()File.Write(body)if num > 0 {fmt.Printf("File: %s Replace: %d\n", root, num)}}
1 0
- Windows下面替换命令实现,最新修复一个BUG
- 一个nullPointer bug的修复
- 替换dex实现热修复
- 一个替换windows自带CMD的超级命令终端
- Windows Service的PreMessageLoop BUG修复
- 查找打补丁后修复的BUG命令
- hibernate3.2.6终于修复一个bug
- Reflector.FileDisassembler的一个bug修复
- 一个多线程示例程序的BUG修复
- 修复Git打包的一个Bug
- Cascade_Master的一个小bug修复
- pomelo 框架一个session bug的修复
- 一个Spring MVC bug的修复
- yarn client中的一个BUG的修复
- Windows的一个Bug
- Windows命令修复MBR表
- windows磁盘修复命令chkdsk
- Windows修复系统最佳命令
- Android获取图片的路径方法总结
- flaskweb遇到的问题总结(持续更新)
- React服务器渲染
- Win7 下基于 Oracle VM VirtualBox 安装 fedora8
- SVN安装、配置、使用笔记
- Windows下面替换命令实现,最新修复一个BUG
- Linux 的变量命名规则
- SizeClass和AutoLayout教程1
- shiro学习和使用实例(2)——登陆认证和授权
- Linux TCP队列相关参数的总结
- 错误711:操作无法完成,因为它无法及时启动远程访问连接管理服务 宽带连接提示错误711的解决办法
- Android特效 五种Toast详解
- Java数据类型字节数
- 关于Android中添加id