/// <summary> /// 迭代的复制指定属性的文件 /// </summary> /// <param name="newFolder"></param> /// <param name="type"></param> /// <param name="fileAttr"></param> /// <param name="dirAttr"></param> public void batchTreeCopyFromByAttribute(String newFolder, FileAttributes fileAttr, FileAttributes dirAttr) { DirectoryInfo newDir = new DirectoryInfo(newFolder); if (newDir.Exists && !newDir.FullName.ToLower().Equals(dir.FullName.ToLower())) { FileInfo[] files = newDir.GetFiles(); for (int i = 0; i < files.Length; i++) { if ((files[i].Attributes & fileAttr) != 0) { FileFunction fileFunc = new FileFunction(files[i]); fileFunc.CopyTo(dir.FullName); } } DirectoryInfo[] dirs = newDir.GetDirectories(); for (int i = 0; i < dirs.Length; i++) { if ((dirs[i].Attributes & dirAttr) == 0) { DirectoryFunction dirFunc = new DirectoryFunction(dir); dirFunc.batchTreeCopyFromByAttribute(dirs[i].FullName, fileAttr, dirAttr); } } } }
/// <summary> /// 提取指定类型的文件,排除指定属性 /// </summary> /// <param name="newFolder"></param> /// <param name="type"></param> public void extractFileByType(String newFolder, String type, FileAttributes fileAttr, FileAttributes dirAttr) { DirectoryInfo newDir = new DirectoryInfo(newFolder); if (newDir.Exists && !newDir.FullName.ToLower().Equals(dir.FullName.ToLower())) { FileInfo[] files = newDir.GetFiles(); for (int i = 0; i < files.Length; i++) { if ((files[i].Attributes & fileAttr) == 0) { FileFunction fileFunc = new FileFunction(files[i]); if (fileFunc.FileExtName.ToLower().Equals(type.ToLower())) { fileFunc.MoveTo(dir.FullName); } } } DirectoryInfo[] dirs = newDir.GetDirectories(); for (int i = 0; i < dirs.Length; i++) { if ((dirs[i].Attributes & dirAttr) == 0) { DirectoryFunction dirFunc = new DirectoryFunction(dir); dirFunc.extractFileByType(dirs[i].FullName, type, fileAttr, dirAttr); } } } }
/// <summary> /// 批量修改文件的全名,排除指定属性 /// </summary> /// <param name="nameOrReplace"></param> /// <param name="contactStr"></param> /// <param name="startNumber"></param> /// <param name="strategy"></param> /// <param name="attr"></param> public void batchModifyFileName(String nameOrReplace, String contactStr, int startNumber, NameStrategy strategy, FileAttributes attr) { if (nameOrReplace == null) { return; } for (int i = 0; i < childFiles.Length; i++) { if ((childFiles[i].Attributes & attr) == 0) { FileFunction file = new FileFunction(childFiles[i]); if (strategy == NameStrategy.IncreaseAtHead) { file.ModifyName(startNumber + contactStr + nameOrReplace); } else if (strategy == NameStrategy.IncreaseAtMiddle) { file.ModifyName(nameOrReplace + startNumber + contactStr); } else if (strategy == NameStrategy.IncreaseAtTail) { file.ModifyName(nameOrReplace + contactStr + startNumber); } else if (strategy == NameStrategy.CutStrAtCharForIncHead) { int index = file.FileName.IndexOf(nameOrReplace); if (index == -1) { file.ModifyName(startNumber + file.FileName); } else { file.ModifyName(startNumber + file.FileName.Substring(index)); } } else if (strategy == NameStrategy.CutStrAtCharForIncTail) { int index = file.FileName.IndexOf(nameOrReplace); if (index == -1) { file.ModifyName(file.FileName + startNumber); } else { index += nameOrReplace.Length; file.ModifyName(file.FileName.Substring(0, index) + startNumber); } } startNumber++; } } }
/// <summary> /// 修改当前目录下的文件的全名为大小写,排除文件属性 /// </summary> /// <param name="caseName"></param> /// <param name="fileAttr"></param> public void batchCaseName(CaseName caseName, FileAttributes fileAttr) { for (int i = 0; i < childFiles.Length; i++) { if ((childFiles[i].Attributes & fileAttr) == 0) { FileFunction fileFunc = new FileFunction(childFiles[i]); String name = ((caseName == CaseName.ToLower) ? fileFunc.FileName.ToLower() : fileFunc.FileName.ToUpper()); System.Console.WriteLine("正在修改文件[" + fileFunc.FilePath + "]的全名为[" + name + "]......"); childFiles[i].MoveTo(dirName + "/" + name); } } }
/// <summary> /// 批量修改当前目录下文件的文件扩展名,排除其中的属性 /// </summary> /// <param name="extName"></param> public void batchModifyFileExtName(String extName, FileAttributes attr) { if (extName == null) { return; } for (int i = 0; i < childFiles.Length; i++) { if ((childFiles[i].Attributes & attr) == 0) { FileFunction file = new FileFunction(childFiles[i]); file.ModifyExt(extName); } } }
/// <summary> /// 将当前目录下的文件复制到指定目录中 /// </summary> /// <param name="newFolder"></param> /// <param name="attr"></param> public void batchCopyTo(String newFolder, FileAttributes attr) { DirectoryInfo newDir = new DirectoryInfo(newFolder); if (newDir.Exists) { if (!newDir.FullName.ToLower().Equals(dir.FullName.ToLower())) { for (int i = 0; i < childFiles.Length; i++) { if ((childFiles[i].Attributes & attr) == 0) { FileFunction file = new FileFunction(childFiles[i]); file.CopyTo(newDir.FullName); } } } } }
/// <summary> /// 迭代得把源目录下的文件复制到每一个子目录中,排除文件,目录属性 /// </summary> /// <param name="sourceDir"></param> /// <param name="fileAttr"></param> /// <param name="dirAttr"></param> private void batchTreeAddFileForDirectory(DirectoryInfo sourceDir, FileAttributes fileAttr, FileAttributes dirAttr) { for (int i = 0; i < childDirs.Length; i++) { if ((childDirs[i].Attributes & dirAttr) == 0) { FileInfo[] subFiles = sourceDir.GetFiles(); for (int j = 0; j < subFiles.Length; j++) { if ((subFiles[j].Attributes & fileAttr) == 0) { FileFunction subFile = new FileFunction(subFiles[j]); subFile.CopyTo(childDirs[i].FullName); } } DirectoryFunction dirFunc = new DirectoryFunction(childDirs[i]); dirFunc.batchTreeAddFileForDirectory(sourceDir, fileAttr, dirAttr); } } }