//开始移动文件夹 public static void moveDir(String dir, FileMoveConfig config) { string[] srr = FileHelper.GetFileNames(dir, "*", true); //循环 foreach (string s in srr) { move(s, config); } }
//开始全部的移动 public static void actionMove(Array array, FileMoveConfig config) { if (array == null || array.Length == 0) { return; } foreach (String s in array) { move(s, config); } }
//获取配置 private FileMoveConfig getConfig() { FileMoveConfig config = new FileMoveConfig(); config.copyToDir = textBox1.Text; config.removeFirst = textBox3.Text; config.removeLast = textBox2.Text; config.oneName = textBox4.Text; config.addFirst = textBox5.Text; config.addLast = textBox6.Text; if (rbMipmap.Checked) { config.imgDirType = "mipmap"; } else if (rbDrawable.Checked) { config.imgDirType = "drawable"; } else if (rbEmpty.Checked) { config.imgDirType = ""; } else if (rbDiy.Checked) { config.imgDirType = etDiy.Text; } if (radioButton2.Checked) { config.repeatType = 0; } else if (radioButton1.Checked) { config.repeatType = 1; } else if (radioButton3.Checked) { config.repeatType = 2; } else if (radioButton4.Checked) { config.repeatType = 3; } config.isToWebP = cbWebp.Checked; config.webpValue = trackBar1.Value; config.isAutoCopyImgId = cbCopyId.Checked; //保存config到本地 config.saveConfig(); config.form1 = this; return(config); }
//打印日志 public void printLog(String from, String toName, FileMoveConfig config) { if (rvLog.SmallImageList == null) { rvLog.SmallImageList = new ImageList(); } rvLog.SmallImageList.Images.Add(ImageUtil.getImageFromFile(from)); rvLog.Items.Add(toName, rvLog.SmallImageList.Images.Count - 1); rvLog.EnsureVisible(rvLog.Items.Count - 1); if (config.isAutoCopyImgId) { copyId(toName); } }
//开始单个移动 public static void move(String file, FileMoveConfig config) { if (FileHelper.IsExistFile(file)) { if (file.LastIndexOf(".rar") == file.Length - 4) { MessageBox.Show("暂不支持rar压缩包"); } else if (file.LastIndexOf(".zip") == file.Length - 4) { String cachePath = "cache\\" + id + "\\"; id++; FileHelper.checkDir(cachePath); ZipUtil.UnZip(file, cachePath); move(cachePath, config); FileHelper.DeleteDirAndFile(cachePath); //如果删除了当前文件夹后,cache文件夹无内容,就删除cache文件夹 String[] ss = FileHelper.getDirAndFiles("cache\\"); if (ss == null || ss.Length == 0) { FileHelper.DeleteDir("cache"); } } else if (file.LastIndexOf(".7z") == file.Length - 3) { MessageBox.Show("暂不支持7z压缩包"); } else { moveFile(file, config); } } else if (FileHelper.IsExistDirectory(file)) { moveDir(file, config); } }
//开始移动文件,单个文件的复制操作,主要操作函数 public static void moveFile(String file, FileMoveConfig config) { //检查父文件夹存不存在 FileHelper.checkDir(config.copyToDir); FileInfo fileinfo = new FileInfo(file); //文件名字和后缀 String name = fileinfo.Name; String dir = ""; if (file.IndexOf("@2x") >= 0) { name = name.Replace("@2x", ""); dir = "\\xhdpi\\"; } else if (file.IndexOf("@3x") >= 0) { name = name.Replace("@3x", ""); dir = "\\xxhdpi\\"; } else if (file.IndexOf("xxxhdpi") >= 0) { name = name.Replace("xxxhdpi", ""); dir = "\\xxxhdpi\\"; } else if (file.IndexOf("xxhdpi") >= 0) { name = name.Replace("xxhdpi", ""); dir = "\\xxhdpi\\"; } else if (file.IndexOf("xhdpi") >= 0) { name = name.Replace("xhdpi", ""); dir = "\\xhdpi\\"; } else if (file.IndexOf("hdpi") >= 0) { name = name.Replace("hdpi", ""); dir = "\\hdpi\\"; } else if (fileinfo.DirectoryName.IndexOf("xxxhdpi") >= 0) { dir = "\\xxxhdpi\\"; } else if (fileinfo.DirectoryName.IndexOf("xxhdpi") >= 0) { dir = "\\xxhdpi\\"; } else if (fileinfo.DirectoryName.IndexOf("xhdpi") >= 0) { dir = "\\xhdpi\\"; } else if (fileinfo.DirectoryName.IndexOf("hdpi") >= 0) { dir = "\\hdpi\\"; } else { dir = "\\其他\\"; } if (config.oneName.Length == 0) { //不是统一设置命名 //去掉前缀 if (name.IndexOf(config.removeFirst) == 0) { name = name.Substring(config.removeFirst.Length, name.Length - config.removeFirst.Length); } //去掉后缀 int lastPosition = name.LastIndexOf(config.removeLast); if (config.removeLast.Length != 0 && lastPosition > 0) { String nameCopy = name; name = nameCopy.Substring(0, lastPosition) + nameCopy.Substring(lastPosition + config.removeLast.Length, nameCopy.Length - lastPosition - config.removeLast.Length); } //增加前缀 name = config.addFirst + name; //增加后缀 if (config.addLast.Length > 0) { int index = name.LastIndexOf('.'); name = name.Substring(0, index) + config.addLast + name.Substring(index); } } else { //如果是统一设置命名 name = config.oneName + Path.GetExtension(file); } String toName; if ("" == config.imgDirType) { toName = name; } else { toName = dir + name; } copy(file, config.copyToDir, toName, config); }
//检查,如果有相同的则移动到新的文件夹 //from 之前的全路径 C\\b\\a.jpg //toDir 要移动到的父目录 C\\b //toName 要移动到的详细目录/文件名 \\xxhdpi\\a.jpg public static void copy(String from, String toDir, String toName, FileMoveConfig config) { //检测如果有非小英文,数字,下划线,就把其设置为下划线 int index1 = toName.LastIndexOf(@"\"); if (index1 < 0) { index1 = 0; } int index2 = toName.LastIndexOf("."); String trueName; if (index2 >= 0) { int a = "" == config.imgDirType ? 0 : 1; trueName = toName.Substring(index1 + a, index2 - index1 - a); } else { trueName = toName.Substring(index1); } //大写要转成小写 trueName = trueName.ToLower(); trueName = Regex.Replace(trueName, "[^a-z0-9_]", "_"); if (trueName.Length == 0 || (trueName[0] >= '0' && trueName[0] <= '9')) { trueName = "_" + trueName; } if (index2 >= 0) { toName = toName.Substring(0, index1) + "\\" + trueName + toName.Substring(index2); } else { toName = toName.Substring(0, index1) + "\\" + trueName; } //增加图片上层文件夹的类型 if (config.imgDirType != "") { if ("mipmap" != config.imgDirType && "drawable" != config.imgDirType) { toName = "\\" + config.imgDirType + toName.Substring(3); } else { toName = "\\" + config.imgDirType + "-" + toName.Substring(1); } } if (config.isToWebP) { toName = toName.Substring(0, toName.LastIndexOf('.')) + ".webp"; } if (FileHelper.IsExistFile(toDir + toName)) { //发现了重复,重复策略:0尾数增加,1复制到新文件夹中,2忽略,3覆盖 switch (config.repeatType) { case 0: //获取最后的数字 int index = toName.LastIndexOf('.'); String first = toName.Substring(0, index); String last = toName.Substring(index); String lastNumber = ""; index = first.Length - 1; while (index >= 0) { char c = first[index]; index--; if (c >= '0' && c <= '9') { lastNumber = c + lastNumber; } else { break; } } String number = System.Text.RegularExpressions.Regex.Replace(lastNumber, @"[^0-9]", ""); bool isNotHaveNumber = false; if (number.Length == 0) { number = "0"; isNotHaveNumber = true; } int numberLength = number.Length; if (isNotHaveNumber) { numberLength = 0; } number = (Int64.Parse(number) + 1).ToString(); toName = first.Substring(0, first.Length - numberLength) + number + last; while (FileHelper.IsExistFile(toDir + toName)) { number = Int64.Parse(number) + 1 + ""; toName = first.Substring(0, first.Length - numberLength) + number + last; } break; case 1: toDir += "\\重复的文件_请检查后自行操作_lt"; while (true) { if (FileHelper.IsExistFile(toDir + toName)) { toDir += "_lt"; } else { break; } } MessageBox.Show(toName + " 重复,复制到了另外的文件夹 " + toDir + toName); break; case 2: MessageBox.Show(toName + " 被忽略了"); return; case 3: //如果不是统一命名才提醒 if (config.oneName.Length <= 0) { MessageBox.Show(toName + " 被覆盖了"); } File.Delete(toDir + toName); break; } } //检查父文件夹是否存在 FileHelper.checkDir(new FileInfo(toDir + toName).Directory.ToString()); if (config.isToWebP) { imageChangeToWebp(from, toDir + toName, config.webpValue); } else { FileHelper.copy(from, toDir + toName); } if (config.form1 != null) { config.form1.printLog(from, toName, config); } }
//从本地获取配置 private void getConfigFromNative() { //初始化默认路径 String defaultDir = System.AppDomain.CurrentDomain.BaseDirectory; textBox1.Text = defaultDir.Substring(0, defaultDir.Length - 1); //尝试从本地获取配置,获取不到就使用默认配置 FileMoveConfigJson j = FileMoveConfig.getJsonConfig(); if (j == null) { return; } textBox2.Text = j.removeLast; textBox3.Text = j.removeFirst; textBox4.Text = j.oneName; textBox6.Text = j.addLast; textBox5.Text = j.addFirst; String imgDirType = j.imgDirType; if ("mipmap" == imgDirType) { rbMipmap.Checked = true; } else if ("drawable" == imgDirType) { rbDrawable.Checked = true; } else if ("" == imgDirType) { rbEmpty.Checked = true; } else { rbDiy.Checked = true; etDiy.Text = imgDirType; } int repeatType = j.repeatType; switch (repeatType) { case 0: radioButton2.Checked = true; break; case 1: radioButton1.Checked = true; break; case 2: radioButton3.Checked = true; break; case 3: radioButton4.Checked = true; break; } if (j.copyToDirs.First != null) { textBox1.Text = j.copyToDirs.First.Value; } cbWebp.Checked = j.isToWebP; tvWebp.Enabled = j.isToWebP; trackBar1.Enabled = j.isToWebP; trackBar1.Value = j.webpValue; cbCopyId.Checked = j.isAutoCopyImgId; tvWebp.Text = "压缩率" + j.webpValue + "(推荐75)"; textBox1.DataSource = j.copyToDirs.ToList(); //setAppTheme(1);emmm,太难看了,先这样吧 }