V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
KingEngine
V2EX  ›  程序员

一坨屎一样的代码 Typora 去除无用本地图片+同步图片到 github 本地 Repository

  •  
  •   KingEngine · 2018-11-11 19:52:08 +08:00 · 4128 次点击
    这是一个创建于 1964 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一坨屎一样的代码 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();
        }
    }
    

    效果图:

    4 条回复    2018-11-11 21:11:45 +08:00
    KingEngine
        1
    KingEngine  
    OP
       2018-11-11 20:04:31 +08:00
    至于为什么图片文件都储存在相对目录./assets 下只是因为安卓上“易码” app 可以直接阅读坚果云 assets 下图片而已,方便,结
    ThirdFlame
        2
    ThirdFlame  
       2018-11-11 20:16:08 +08:00
    试试 vnote+git 同步?
    xml123
        3
    xml123  
       2018-11-11 20:53:01 +08:00
    vnote 了解一下,自动清理无用图片
    KingEngine
        4
    KingEngine  
    OP
       2018-11-11 21:11:45 +08:00
    @ThirdFlame
    @xml123
    或许我会试试的,目前暂用
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   964 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:48 · PVG 04:48 · LAX 13:48 · JFK 16:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.