private void ProcessDirectory(string srcDir, string destDir) { if (!Directory.Exists(destDir)) { Directory.CreateDirectory(destDir); } DirectoryInfo dirInfo = new DirectoryInfo(srcDir); IEnumerable <string> fileIterator = PathSearch.FindFiles( dirInfo, "*.*", SearchOption.TopDirectoryOnly); foreach (string fileName in fileIterator) { string destFile = destDir + fileName.Substring( fileName.LastIndexOf('\\')); string extion = Path.GetExtension(fileName).ToLower(); //process .htm and .html files, just copy other files, like css, gif. TFS DCR 318537 if (String.Equals(extion, ".htm") || String.Equals(extion, ".html")) { try { ProcessFile(fileName, destFile); } catch { if (_logger != null) { _logger.WriteLine(String.Format("failed to process file {0}", fileName), BuildLoggerLevel.Error); } throw; } } else { File.Copy(fileName, destFile, true); File.SetAttributes(destFile, FileAttributes.Normal); } } // Recurse into subdirectories of this directory. string[] subdirectoryEntries = Directory.GetDirectories(srcDir, "*", SearchOption.TopDirectoryOnly); foreach (string subdirectory in subdirectoryEntries) { DirectoryInfo di = new DirectoryInfo(subdirectory); string newSubdir = destDir + "\\" + di.Name; ProcessDirectory(subdirectory, newSubdir); } }
/// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="target"></param> /// <param name="logger"></param> private void CopyFilesEx(DirectoryInfo source, DirectoryInfo target, BuildLogger logger) { string targetDirName = target.ToString(); string filePath; // Handle the copy of each file into it's new directory. foreach (string file in PathSearch.FindFiles( source, "*.*", SearchOption.TopDirectoryOnly)) { FileAttributes fileAttr = File.GetAttributes(file); if (!_includeHidden) { if ((fileAttr & FileAttributes.Hidden) == FileAttributes.Hidden) { continue; } } _copiedCount++; string fileName = Path.GetFileName(file); filePath = Path.Combine(targetDirName, fileName); if (logger != null) { logger.WriteLine(String.Format(@"Copying {0}\{1}", target.FullName, fileName), BuildLoggerLevel.Info); } File.Copy(file, filePath, _isOverwrite); // For most of the build files, we will copy and delete, so we must // remove any readonly flag... if ((fileAttr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { fileAttr -= FileAttributes.ReadOnly; } File.SetAttributes(filePath, fileAttr); // if required to set the security or access control if (_includeSecurity) { File.SetAccessControl(filePath, File.GetAccessControl(file)); } } }
protected override bool OnExecute(BuildContext context) { if (_listDirs == null || _listDirs.Count == 0) { return(false); } BuildLogger logger = context.Logger; try { // make a copy of the current stage... bool recursive = _isRecursive; int itemCount = _listDirs.Count; int dirDeleted = 0; for (int i = 0; i < itemCount; i++) { string dirPath = _listDirs[i]; if (String.IsNullOrEmpty(dirPath)) { continue; } DirectoryInfo dirInfo = new DirectoryInfo(dirPath); if (dirInfo.Exists) { // It is a directory... try { dirInfo.Attributes = FileAttributes.Normal; dirInfo.Delete(true); } catch (UnauthorizedAccessException) { // One possible cause of this is read-only file, so first // try another method of deleting the directory... foreach (string file in PathSearch.FindFiles( dirInfo, "*.*", SearchOption.AllDirectories)) { File.SetAttributes(file, FileAttributes.Normal); File.Delete(file); } dirInfo.Delete(true); } if (logger != null) { logger.WriteLine(dirPath, BuildLoggerLevel.Info); } dirDeleted++; } else { if (logger != null) { logger.WriteLine(String.Format("Path does not exists - {0}", dirPath), BuildLoggerLevel.Info); } } } return(true); } catch (Exception ex) { if (logger != null) { logger.WriteLine(ex, BuildLoggerLevel.Error); } return(false); } }
private void Process(string workingDir) { int fileCount = 0; IEnumerable <string> fileIterator = PathSearch.FindFiles( new DirectoryInfo(workingDir), "*.htm", SearchOption.AllDirectories); int itemCount = _listEncoders.Count; foreach (string file in fileIterator) { using (StreamWriter output = new StreamWriter(file + ".tmp", true, _outputEncoding)) { using (StreamReader input = new StreamReader(file)) { Encoding sourceEncoding = input.CurrentEncoding; for (int i = 0; i < itemCount; i++) { FormatChmEncoder encoder = _listEncoders[i]; if (encoder != null) { encoder._inputEncoding = sourceEncoding; } } string line = input.ReadLine(); while (line != null) { for (int i = 0; i < itemCount; i++) { FormatChmEncoder encoder = _listEncoders[i]; if (encoder != null) { line = encoder.Process(line); } } output.WriteLine(line); line = input.ReadLine(); } } } File.Delete(file); if (File.Exists(file)) { // Have experienced delay file deletion, resulting exception // in the next renaming step...just sleep. System.Threading.Thread.Sleep(100); } File.Move(file + ".tmp", file); fileCount++; } if (_logger != null) { if (_verbosity != BuildLoggerVerbosity.Quiet) { _logger.WriteLine(String.Format("Processed {0} files.", fileCount), BuildLoggerLevel.Info); } } }