From 7c337eaf430123ba34123b08b8e52a7d84b3e24d Mon Sep 17 00:00:00 2001 From: viviman Date: Fri, 12 Nov 2021 00:04:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E6=98=AF=E5=BE=88=E5=A5=BD=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...‰æ–‡ä»¶æˆ–目录信æ¯(包括å­ç›®å½•).go | 20 ++++++ ...–目录信æ¯(ä¸åŒ…å«å¤šçº§å­ç›®å½•).go | 21 ++++++ ...¶æˆ–目录å(ä¸åŒ…å«å¤šçº§å­ç›®å½•).go | 24 +++++++ golang_learn/data_office/get_hy_sql/main.go | 68 ++++++++++++++++++- 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 golang_learn/data_func/go_to_file/go_to_dir/获å–当å‰æ–‡ä»¶æˆ–目录下的所有文件或目录信æ¯(包括å­ç›®å½•).go create mode 100644 golang_learn/data_func/go_to_file/go_to_dir/获å–当å‰ç›®å½•ä¸‹çš„文件或目录信æ¯(ä¸åŒ…å«å¤šçº§å­ç›®å½•).go create mode 100644 golang_learn/data_func/go_to_file/go_to_dir/获å–当å‰ç›®å½•ä¸‹çš„文件或目录å(ä¸åŒ…å«å¤šçº§å­ç›®å½•).go diff --git a/golang_learn/data_func/go_to_file/go_to_dir/获å–当å‰æ–‡ä»¶æˆ–目录下的所有文件或目录信æ¯(包括å­ç›®å½•).go b/golang_learn/data_func/go_to_file/go_to_dir/获å–当å‰æ–‡ä»¶æˆ–目录下的所有文件或目录信æ¯(包括å­ç›®å½•).go new file mode 100644 index 0000000..d72bfe0 --- /dev/null +++ b/golang_learn/data_func/go_to_file/go_to_dir/获å–当å‰æ–‡ä»¶æˆ–目录下的所有文件或目录信æ¯(包括å­ç›®å½•).go @@ -0,0 +1,20 @@ +package go_to_dir + +import ( + "fmt" + //"io/ioutil" + //"log" + "os" + "path/filepath" +) + +func main() { + pwd, _ := os.Getwd() + + //获å–当å‰ç›®å½•ä¸‹çš„æ‰€æœ‰æ–‡ä»¶æˆ–ç›®å½•ä¿¡æ¯ + filepath.Walk(pwd, func(path string, info os.FileInfo, err error) error { + fmt.Println(path) //打å°pathä¿¡æ¯ + fmt.Println(info.Name()) //打å°æ–‡ä»¶æˆ–目录å + return nil + }) +} diff --git a/golang_learn/data_func/go_to_file/go_to_dir/获å–当å‰ç›®å½•ä¸‹çš„文件或目录信æ¯(ä¸åŒ…å«å¤šçº§å­ç›®å½•).go b/golang_learn/data_func/go_to_file/go_to_dir/获å–当å‰ç›®å½•ä¸‹çš„文件或目录信æ¯(ä¸åŒ…å«å¤šçº§å­ç›®å½•).go new file mode 100644 index 0000000..f7172bc --- /dev/null +++ b/golang_learn/data_func/go_to_file/go_to_dir/获å–当å‰ç›®å½•ä¸‹çš„文件或目录信æ¯(ä¸åŒ…å«å¤šçº§å­ç›®å½•).go @@ -0,0 +1,21 @@ +package go_to_dir + + +import ( + "fmt" + "io/ioutil" + "log" + "os" +) +func main() { + pwd, _ := os.Getwd() + //获å–æ–‡ä»¶æˆ–ç›®å½•ç›¸å…³ä¿¡æ¯ + fileInfoList, err := ioutil.ReadDir(pwd) + if err != nil { + log.Fatal(err) + } + fmt.Println(len(fileInfoList)) + for i := range fileInfoList { + fmt.Println(fileInfoList[i].Name()) //打å°å½“å‰æ–‡ä»¶æˆ–目录下的文件或目录å + } +} \ No newline at end of file diff --git a/golang_learn/data_func/go_to_file/go_to_dir/获å–当å‰ç›®å½•ä¸‹çš„文件或目录å(ä¸åŒ…å«å¤šçº§å­ç›®å½•).go b/golang_learn/data_func/go_to_file/go_to_dir/获å–当å‰ç›®å½•ä¸‹çš„文件或目录å(ä¸åŒ…å«å¤šçº§å­ç›®å½•).go new file mode 100644 index 0000000..4a190a5 --- /dev/null +++ b/golang_learn/data_func/go_to_file/go_to_dir/获å–当å‰ç›®å½•ä¸‹çš„文件或目录å(ä¸åŒ…å«å¤šçº§å­ç›®å½•).go @@ -0,0 +1,24 @@ +package go_to_dir + +import ( + "fmt" + //"io/ioutil" + "log" + "os" + "path/filepath" +) + +func main() { + pwd, _ := os.Getwd() + + //获å–当å‰ç›®å½•ä¸‹çš„文件或目录å(包å«è·¯å¾„) + filepathNames, err := filepath.Glob(filepath.Join(pwd, "*")) + if err != nil { + log.Fatal(err) + } + + for i := range filepathNames { + fmt.Println(filepathNames[i]) //打å°path + } + +} diff --git a/golang_learn/data_office/get_hy_sql/main.go b/golang_learn/data_office/get_hy_sql/main.go index a1344c1..6e5d0d8 100644 --- a/golang_learn/data_office/get_hy_sql/main.go +++ b/golang_learn/data_office/get_hy_sql/main.go @@ -1,12 +1,78 @@ package main +import ( + "io/ioutil" + "log" + "os" + "strings" + "time" +) + /* æ€è·¯: 脚本按照顺åºæ‹¼æŽ¥ï¼Œæ—§æ–‡ä»¶æŒ‰ç…§ utf8 æ ¼å¼æ¸…空 */ -import () + +var splitStr = "/* 202" + +var endName = ".sql" +var endFileName string +var History = "/history/" +var DBStr = []string{"/MYSQL/update", "/ORACLE/update", "/SHENTONG/update", "/DM/update", "/KingBase/update"} +var mapStr = make(map[string]string) +var mapInt = make(map[int8]string) //å­˜åœ¨æ‰€æœ‰å¯¹è±¡ä¿¡æ¯ - é›†åˆ func main() { + // Todo åˆå¹¶çš„SQL文件å称,超过1000行,分割多个文件 + endFileName = History + time.Now().Format("20060102") + endName + + // aaa := os.Args //当å‰ç›®å½•æ•°ç»„ + var aaa string = "/Users/zhangxianwei/IDEA/hy_sql" + for i := range DBStr { + kkk := aaa + DBStr[i] + getAllFile(kkk) + } + + for s := range mapStr { + log.Println(s, mapStr[s]) + } +} + +func getAllFile(pwd string) { + //获å–æ–‡ä»¶æˆ–ç›®å½•ç›¸å…³ä¿¡æ¯ + fileInfoList, err := ioutil.ReadDir(pwd) + if err != nil { + log.Fatal(err) + } + for i := range fileInfoList { + if strings.HasSuffix(fileInfoList[i].Name(), endName) { + path := pwd + "/" + fileInfoList[i].Name() + s := read(path) + split := strings.Split(s, splitStr) + + for i2 := range split { + // log.Println(split[i2], "\n") + mapStr[path] = s + s2 := split[i2] + log.Println(s2) + } + } + } +} + +func read(path string) string { + f, _ := os.Open(path) + all, _ := ioutil.ReadAll(f) + return string(all) +} + +/** +write 输出到文件 +*/ +func write(s, path string) { + file3, _ := os.Create(path) + file3.WriteString(s) + file3.Close() }