一坨屎一样的代码 Typora 去除无用本地图片+同步图片到 github 本地 Repository
感谢 10 楼的 idea !
目的去除 md 笔记目录下的无用图片,顺便将图片同步到 github 本地仓库,方便提交做远程……
使用粗鲁的死循环,可打成 jar 用 cmd 运行…对于需要的人有点用,否则就看看热闹。
public class Main {
static ArrayList<String> list = new ArrayList<>();
static StringBuilder text = new StringBuilder();
static String sycDir="D:\\坚果云\\CodeNotes"; //笔记本同步路径
static String hubDir="D:\\github\\assets"; //github 库本地路径
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("正在处理中……");
while (true) {
mainClear();
sleep(30000); //间隔时间
}
}
public static void mainClear() throws IOException {
ArrayList<String> picList = new ArrayList<>();
File file = new File(sycDir);
func(file);
func(new File(hubDir));
String ContentArea = text.toString();
String regex = "\\(assets/.*?\\.\\w+\\)";
Pattern pt = Pattern.compile(regex);
Matcher mt = pt.matcher(ContentArea);
while (mt.find()) {
String replace = mt.group().replace("(assets/", "").replace(")", "");
picList.add(replace);
}
for (String s : list) {
String fileName = s.substring(s.lastIndexOf("\\") + 1, s.length());//获取文件名
int count = 0;
for (String pic : picList) {
if (pic.equals(fileName)) {
count++;
}
}
if (count <= 0) {
System.out.println(s+"已放入"+sycDir+"回收站");
String hs = file + "\\回收站\\";
if (!new File(hs).exists())
new File(hs).mkdir();
if (!new File(s).renameTo(new File(hs + fileName))) {
new File(hs + fileName).delete();
new File(s).renameTo(new File(hs + fileName));
}
}
}
text = new StringBuilder();
list.clear();
}
private static void func(File file) throws IOException {
File[] fs = file.listFiles();
String fName;
for (File f : fs) {
if (f.isDirectory() && !f.toString().contains("回收站") && !f.toString().contains(".git")) //排除回收站目录
func(f);
if (f.isFile()) { //若是文件,直接打印详细路径
String s = f.toString();
if (s.endsWith(".md")) {//获取 md 文件内容
text.append(readToString(s));
} else {
list.add(s);
if (s.contains(sycDir) && !s.contains(hubDir)) {
fName = hubDir +"\\"+ s.substring(s.lastIndexOf("\\") + 1, s.length());
if (!new File(fName).exists()) {
System.out.println(s + "已同步");
Files.copy(new File(s).toPath(), new File(fName).toPath());
}
}
}
}
}
}
public static String readToString(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line;
StringBuilder txt = new StringBuilder();
while ((line = br.readLine()) != null) {
txt.append(line);
}
br.close();
return txt.toString();
}
}
效果图:
1
KingEngine OP 至于为什么图片文件都储存在相对目录./assets 下只是因为安卓上“易码” app 可以直接阅读坚果云 assets 下图片而已,方便,结
|
2
ThirdFlame 2018-11-11 20:16:08 +08:00
试试 vnote+git 同步?
|
3
xml123 2018-11-11 20:53:01 +08:00
vnote 了解一下,自动清理无用图片
|
4
KingEngine OP |