zoukankan      html  css  js  c++  java
  • 我的go练手项目--使用go实现“在文件中替换”功能

    写这个项目的需求比较简单,就是想批量替换一批文件里面的关键字(实际场景是为了迁移到达梦,需要把php代码里面使用oci的函数名全替换了)

    不过由于替换规则比较复杂,现有的文本编辑器不好操作,所以写了一个工具辅助

    package main
    
    import (
    	"bytes"
    	"fmt"
    	"io/ioutil"
    	"log"
    	"os"
    	"regexp"
    	"strings"
    )
    
    //获取指定目录下的所有文件和目录
    func GetFilesAndDirs(dirPth string) (files []string, dirs []string, err error) {
    	dir, err := ioutil.ReadDir(dirPth)
    	if err != nil {
    		return nil, nil, err
    	}
    
    	PthSep := string(os.PathSeparator)
    	//suffix = strings.ToUpper(suffix) //忽略后缀匹配的大小写
    
    	for _, fi := range dir {
    		if fi.IsDir() { // 目录, 递归遍历
    			dirs = append(dirs, dirPth+PthSep+fi.Name())
    			GetFilesAndDirs(dirPth + PthSep + fi.Name())
    		} else {
    			// 过滤指定格式
    			ok := strings.HasSuffix(fi.Name(), ".go")
    			if ok {
    				files = append(files, dirPth+PthSep+fi.Name())
    			}
    		}
    	}
    
    	return files, dirs, nil
    }
    
    
    //获取指定目录下的所有文件,包含子目录下的文件
    func GetAllFiles(dirPth string) (files []string, err error) {
    	var dirs []string
    	dir, err := ioutil.ReadDir(dirPth)
    	if err != nil {
    		return nil, err
    	}
    
    	PthSep := string(os.PathSeparator)
    	//suffix = strings.ToUpper(suffix) //忽略后缀匹配的大小写
    
    	for _, fi := range dir {
    		if fi.IsDir() { // 目录, 递归遍历
    			dirs = append(dirs, dirPth+PthSep+fi.Name())
    			GetAllFiles(dirPth + PthSep + fi.Name())
    		} else {
    			files = append(files, dirPth+PthSep+fi.Name())
    			/*
    			// 过滤指定格式
    			ok := strings.HasSuffix(fi.Name(), ".go")
    			if ok {
    				files = append(files, dirPth+PthSep+fi.Name())
    			}
    
    			 */
    		}
    	}
    
    	// 读取子目录下文件
    	for _, table := range dirs {
    		temp, _ := GetAllFiles(table)
    		for _, temp1 := range temp {
    			files = append(files, temp1)
    		}
    	}
    
    	return files, nil
    }
    
    //获取目录里面的文件名与expr正则匹配的文件
    func GetAllFilesMatch(dirPth string, expr string) (files []string, err error) {
    	var reg *regexp.Regexp = nil
    
    	if expr != "" {
    		//先编译正则 提高匹配效率
    		reg, err = regexp.Compile(expr)
    		if err != nil {
    			return nil, err
    		}
    	}
    
    	return getAllFilesMatchInter(reg, dirPth)
    }
    
    //获取指定目录下的所有文件,包含子目录下的文件
    func getAllFilesMatchInter(reg *regexp.Regexp, dirPth string) (files []string, err error) {
    	var dirs []string
    
    	dir, err := ioutil.ReadDir(dirPth)
    	if err != nil {
    		return nil, err
    	}
    
    	PthSep := string(os.PathSeparator)
    	//suffix = strings.ToUpper(suffix) //忽略后缀匹配的大小写
    
    	for _, fi := range dir {
    		if fi.IsDir() { // 目录, 递归遍历
    			dirs = append(dirs, dirPth+PthSep+fi.Name())
    		} else {
    			if (reg != nil) {
    				// 过滤指定格式
    				ok := reg.MatchString(fi.Name())
    				if ok {
    					files = append(files, dirPth+PthSep+fi.Name())
    				}
    			} else {
    				files = append(files, dirPth+PthSep+fi.Name())
    			}
    		}
    	}
    
    	// 读取子目录下文件
    	for _, table := range dirs {
    		temp, err := getAllFilesMatchInter(reg, table)
    		if err != nil {
    			return nil, err
    		}
    
    		for _, temp1 := range temp {
    			files = append(files, temp1)
    		}
    	}
    
    	return files, nil
    }
    
    func changeKey(fileName string) {
    	buf, _ := ioutil.ReadFile(fileName)
    	//log.Printf("%v", string(buf))
    	if !bytes.Contains(buf, []byte("OCI")) {
    		return
    	}
    
    	ok, _ := regexp.Match("OCI[a-zA-Z]", buf)
    	if !ok {
    		return
    	}
    
    	fmt.Printf("修改文件[%s]
    ", fileName)
    	buf = bytes.ReplaceAll(buf, []byte("
    OCI"), []byte("
    HT_DPI"))
    	buf = bytes.ReplaceAll(buf, []byte("
    OCI"), []byte("
    HT_DPI"))
    	/*
    	buf = bytes.ReplaceAll(buf, []byte(";OCI"), []byte(";HT_DPI"))
    	buf = bytes.ReplaceAll(buf, []byte(""OCI"), []byte(""HT_DPI"))
    	buf = bytes.ReplaceAll(buf, []byte("/OCI"), []byte("/HT_DPI"))
    	buf = bytes.ReplaceAll(buf, []byte("@OCI"), []byte("@HT_DPI"))
    	buf = bytes.ReplaceAll(buf, []byte("
    OCI"), []byte("
    HT_DPI"))
    	buf = bytes.ReplaceAll(buf, []byte("!OCI"), []byte("!HT_DPI"))
    	buf = bytes.ReplaceAll(buf, []byte("(OCI"), []byte("(HT_DPI"))
    	buf = bytes.ReplaceAll(buf, []byte("=OCI"), []byte("=HT_DPI"))
    	buf = bytes.ReplaceAll(buf, []byte(" OCI"), []byte(" HT_DPI"))
    	buf = bytes.ReplaceAll(buf, []byte("	OCI"), []byte("	HT_DPI"))
    	*/
    	ioutil.WriteFile(fileName, buf, os.ModePerm)
    }
    
    func changeKey1(fileName string) {
    	buf, _ := ioutil.ReadFile(fileName)
    	//log.Printf("%v", string(buf))
    	if !bytes.Contains(buf, []byte("ocifetchinto")) {
    		return
    	}
    
    	fmt.Printf("修改文件[%s]
    ", fileName)
    	buf = bytes.ReplaceAll(buf, []byte("ocifetchinto"), []byte("HT_DPIFetchInto"))
    
    	ioutil.WriteFile(fileName, buf, os.ModePerm)
    }
    
    
    func changeKey2(fileName string) {
    	buf, _ := ioutil.ReadFile(fileName)
    	//log.Printf("%v", string(buf))
    	if !bytes.Contains(buf, []byte("HT_DPIFetchinto")) {
    		return
    	}
    
    	fmt.Printf("修改文件[%s]
    ", fileName)
    	buf = bytes.ReplaceAll(buf, []byte("HT_DPIFetchinto"), []byte("HT_DPIFetchInto"))
    
    	ioutil.WriteFile(fileName, buf, os.ModePerm)
    }
    
    
    
    func changeKey3(fileName string) {
    	buf, _ := ioutil.ReadFile(fileName)
    	//log.Printf("%v", string(buf))
    	if !bytes.Contains(buf, []byte("HT_DPIexecute")) {
    		return
    	}
    
    	fmt.Printf("修改文件[%s]
    ", fileName)
    	buf = bytes.ReplaceAll(buf, []byte("HT_DPIexecute"), []byte("HT_DPIExecute"))
    
    	ioutil.WriteFile(fileName, buf, os.ModePerm)
    }
    
    /*
    ocibindbyname(
    ociBindByName(
    OCIBindByName(
    OCICommit(
    ocidefinebyname(
    OCIExecute(
    ocifetch(
    ocifetchstatement(
    OCIFreeStatement(
    ocilogoff(
    OCILogoff(
    OCIRollback(
    ocisetprefetch(
    */
    func changeKey4(fileName string) {
    	buf, _ := ioutil.ReadFile(fileName)
    	//log.Printf("%v", string(buf))
    	if !bytes.Contains(buf, []byte("oci")) {
    		return
    	}
    
    	fmt.Printf("修改文件[%s]
    ", fileName)
    	buf = bytes.ReplaceAll(buf, []byte("ocibindbyname"), []byte("HT_DPIBindByName"))
    	buf = bytes.ReplaceAll(buf, []byte("ociBindByName"), []byte("HT_DPIBindByName"))
    	buf = bytes.ReplaceAll(buf, []byte("ocidefinebyname"), []byte("HT_DPIDefineByName"))
    	buf = bytes.ReplaceAll(buf, []byte("ocifetch"), []byte("HT_DPIFetch"))
    	buf = bytes.ReplaceAll(buf, []byte("ocifetchstatement"), []byte("HT_DPIFetchStatement"))
    	buf = bytes.ReplaceAll(buf, []byte("ocilogoff"), []byte("HT_DPILogoff"))
    	buf = bytes.ReplaceAll(buf, []byte("ocisetprefetch"), []byte("HT_DPISetPreFetch"))
    
    	ioutil.WriteFile(fileName, buf, os.ModePerm)
    }
    
    
    func main() {
    	files, dirs, _ := GetFilesAndDirs(`C:inc_chk`)
    
    	for _, dir := range dirs {
    		fmt.Printf("获取的文件夹为[%s]
    ", dir)
    	}
    
    
    	for _, dir := range files {
    		fmt.Printf("获取的文件为[%s]
    ", dir)
    	}
    
    	xfiles, err := GetAllFilesMatch(`C:inc_chk_dpi`, `.*.php$`)
    	if err != nil {
    		log.Printf("err = %v", err)
    		os.Exit(1)
    	}
    
    	for _, file := range xfiles {
    		//fmt.Printf("获取的文件为[%s]
    ", file)
    		changeKey(file)
    		/*
    		changeKey4(file)
    		changeKey1(file)
    		changeKey2(file)
    		changeKey3(file)
    
    		 */
    	}
    }
    
    
  • 相关阅读:
    C语言第0次作业
    c语言博客作业02循环结构
    C语言博客作业04数组
    存储过程,函数参数默认值的一些问题
    路线查询
    C# 猜数字
    使用 Ext.Net TreePanel,TabPanel控件 布局
    SQL SERVER 2005 动态行转列SQL
    CROSS APPLY 和OUTER APPLY 的区别
    处理表重复记录(查询和删除)
  • 原文地址:https://www.cnblogs.com/kingstarer/p/13379419.html
Copyright © 2011-2022 走看看