-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.go
81 lines (69 loc) · 2.56 KB
/
script.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strconv"
)
func main() {
// 通过exec.Command函数执行命令或者shell
// 第一个参数是命令路径,当然如果PATH路径可以搜索到命令,可以不用输入完整的路径
// 第二到第N个参数是命令的参数
// 下面语句等价于执行命令: ls -l /var/
cmd := exec.Command("cmd", "copy C:Users/smile/Desktop/其他/MAKEDOWN/gh-md-toc.exe gh-md-toc.exe")
// 执行命令,并返回结果
if output, err := cmd.Output(); err != nil {
panic(err)
} else {
// 因为结果是字节数组,需要转换成string
fmt.Println(string(output))
}
c := exec.Command("cmd", "/C", "copygh-md-toc.exe gh-md-toc.exe")
if err := c.Start(); err != nil {
fmt.Println("Error: ", err)
}
str := "markdown" //目录
// MkdirAll 递归创建目录
if err := os.Mkdir(str, 0666); err != nil {
fmt.Println("err=", err)
}
var a int = 1
for i := 1; i < 51; i++ {
a1 := strconv.Itoa(a)
a2 := strconv.Itoa((a + 1))
a3 := strconv.Itoa((a - 1))
filePath := "markdown/" + a1 + ".md"
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
//在原来的基础上追加666表示访问权限
if err != nil {
fmt.Println("文件打开失败", err)
}
//及时关闭file句柄
defer file.Close()
//写入文件时,使用带缓存的 *Writer
write := bufio.NewWriter(file)
write.WriteString("+ [author](https://github.com/3293172751)\n")
write.WriteString("+ <a href=\"https://github.com/3293172751\" target=\"_blank\"><img src=\"https://img.shields.io/badge/Github-xiongxinwei-inactive?style=social&logo=github\"></a></p>\n")
write.WriteString("# 第" + a1 + "节\n")
//批量加入文件,
write.WriteString("+ [回到目录](../README.md)\n")
write.WriteString("+ [回到项目首页](../../README.md)\n")
write.WriteString("+ [上一节](" + a3 + ".md)\n")
write.WriteString("> ❤️💕💕算法学习笔记和LeetCode的刷题笔记与记录。Myblog:[http://nsddd.top](http://nsddd.top/)\n")
write.WriteString("---\n")
write.WriteString("[TOC]\n")
for i := 0; i < 5; i++ {
write.WriteString("\n")
}
write.WriteString("## END 链接\n")
write.WriteString("+ [回到目录](../README.md)\n")
write.WriteString("+ [上一节](" + a3 + ".md)\n")
write.WriteString("+ [下一节](" + a2 + ".md)\n")
write.WriteString("---\n")
write.WriteString("+ [参与贡献❤️💕💕](https://github.com/3293172751/Block_Chain/blob/master/Git/git-contributor.md)")
//Flush将缓存的文件真正写入到文件中
write.Flush()
a = a + 1
}
}