public void AddPrefixToFileName(string fullPathSource, bool recursive, string whereClause, string prefix) { Log.WriteLog(" -- Init -- ", "Log", this.GetType().Name, this.AppConfig); foreach (FileInfo file in this.SelectFiles(fullPathSource, recursive, whereClause)) { try { if (!File.Exists(string.Concat(file.DirectoryName, "\\", file.Name))) { FileSelectionManager affectedFiles = this; affectedFiles.AffectedFiles = affectedFiles.AffectedFiles - 1; } else { File.Move(string.Concat(file.DirectoryName, "\\", file.Name), string.Concat(file.DirectoryName, "\\", prefix, file.Name)); } } catch (Exception exception) { Exception e = exception; Log.WriteLog(e.Message, "Error", this.GetType().Name, this.AppConfig); throw e; } } }
public void CopyStructureAndMoveFiles(string fullPathSource, bool recursive, string whereClause, string fullPathDestination, bool forceToMove) { Log.WriteLog(" -- Init -- ", "Log", this.GetType().Name, this.AppConfig); foreach (FileInfo file in this.SelectFiles(fullPathSource, recursive, whereClause, fullPathDestination)) { try { string pathDestinationPlusDirectoryName = string.Concat(fullPathDestination, this.RemoveDrivefromDirectory(file.DirectoryName)); if (!Directory.Exists(pathDestinationPlusDirectoryName)) { Directory.CreateDirectory(pathDestinationPlusDirectoryName); } if (!File.Exists(string.Concat(pathDestinationPlusDirectoryName, "\\", file.Name)) || forceToMove) { if (File.Exists(string.Concat(pathDestinationPlusDirectoryName, "\\", file.Name))) { File.Delete(string.Concat(pathDestinationPlusDirectoryName, "\\", file.Name)); } File.Move(string.Concat(file.DirectoryName, "\\", file.Name), string.Concat(pathDestinationPlusDirectoryName, "\\", file.Name)); } else { FileSelectionManager affectedFiles = this; affectedFiles.AffectedFiles = affectedFiles.AffectedFiles - 1; } } catch (Exception exception) { Exception e = exception; Log.WriteLog(e.Message, "Error", this.GetType().Name, this.AppConfig); throw e; } } }
public void ReplaceValueInFileName(string fullPathSource, bool recursive, string whereClause, string oldValue, string newValue) { Log.WriteLog(" -- Init -- ", "Log", this.GetType().Name, this.AppConfig); foreach (FileInfo file in this.SelectFiles(fullPathSource, recursive, whereClause)) { try { if (!File.Exists(string.Concat(file.DirectoryName, "\\", file.Name)) || file.Name.IndexOf(oldValue) < 0) { FileSelectionManager affectedFiles = this; affectedFiles.AffectedFiles = affectedFiles.AffectedFiles - 1; } else if (oldValue != string.Empty) { File.Move(string.Concat(file.DirectoryName, "\\", file.Name), string.Concat(file.DirectoryName, "\\", file.Name.Replace(oldValue, newValue))); } } catch (Exception exception) { Exception e = exception; Log.WriteLog(e.Message, "Error", this.GetType().Name, this.AppConfig); throw e; } } }
public void AddSuffixToFileName(string fullPathSource, bool recursive, string whereClause, string suffix) { Log.WriteLog(" -- Init -- ", "Log", this.GetType().Name, this.AppConfig); foreach (FileInfo file in this.SelectFiles(fullPathSource, recursive, whereClause)) { try { if (!File.Exists(string.Concat(file.DirectoryName, "\\", file.Name))) { FileSelectionManager affectedFiles = this; affectedFiles.AffectedFiles = affectedFiles.AffectedFiles - 1; } else { string str = string.Concat(file.DirectoryName, "\\", file.Name); string[] directoryName = new string[] { file.DirectoryName, "\\", null, null, null }; directoryName[2] = (file.Extension != string.Empty ? file.Name.Replace(file.Extension, "") : file.Name); directoryName[3] = suffix; directoryName[4] = file.Extension; File.Move(str, string.Concat(directoryName)); } } catch (Exception exception) { Exception e = exception; Log.WriteLog(e.Message, "Error", this.GetType().Name, this.AppConfig); throw e; } } }
public void Unzip(string fullPathZipFile, string fullPathToUnzip, bool unZipWithDirectoryStructure) { int Size; Log.WriteLog(" -- Init -- ", "Log", this.GetType().Name, this.AppConfig); System.IO.FileStream zipFileStream = null; string location = string.Empty; try { if (Path.GetExtension(fullPathZipFile).ToLower() != ".zip") { throw new IOException("File Extension in not valid"); } if (!File.Exists(fullPathZipFile)) { throw new FileNotFoundException("Zip file not found"); } zipFileStream = new System.IO.FileStream(fullPathZipFile, FileMode.Open); try { this.ValidatePath(fullPathToUnzip); } catch (DirectoryNotFoundException directoryNotFoundException) { Log.WriteLog(string.Concat("Create Directory ", fullPathToUnzip), "Log", this.GetType().Name, this.AppConfig); Directory.CreateDirectory(fullPathToUnzip); } using (System.IO.Packaging.Package Package = System.IO.Packaging.Package.Open(zipFileStream, FileMode.Open, FileAccess.Read)) { foreach (PackageRelationship Relationship in Package.GetRelationshipsByType("http://schemas.microsoft.com/opc/2006/sample/document")) { Uri UriTarget = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), Relationship.TargetUri); PackagePart document = Package.GetPart(UriTarget); location = (!unZipWithDirectoryStructure ? string.Concat(fullPathToUnzip, "\\", Path.GetFileName(HttpUtility.UrlDecode(document.Uri.ToString()).Replace('\\', '/'))) : string.Concat(fullPathToUnzip, HttpUtility.UrlDecode(document.Uri.ToString()).Replace('\\', '/'))); string folder = Path.GetDirectoryName(location); try { this.ValidatePath(folder); } catch (DirectoryNotFoundException directoryNotFoundException1) { Log.WriteLog(string.Concat("Create Directory ", folder), "Log", this.GetType().Name, this.AppConfig); Directory.CreateDirectory(folder); } byte[] Data = new byte[1024]; using (System.IO.FileStream FileStream = new System.IO.FileStream(location, FileMode.Create)) { Stream DocumentStream = document.GetStream(); do { Size = DocumentStream.Read(Data, 0, 1024); FileStream.Write(Data, 0, Size); }while (Size == 1024); FileStream.Close(); } FileSelectionManager affectedFiles = this; affectedFiles.AffectedFiles = affectedFiles.AffectedFiles + 1; FileSelectionManager involvedFiles = this; involvedFiles.InvolvedFiles = involvedFiles.InvolvedFiles + 1; } if (File.Exists(string.Concat(fullPathToUnzip, "\\[Content_Types].xml"))) { File.Delete(string.Concat(fullPathToUnzip, "\\[Content_Types].xml")); } } } catch (Exception exception) { Exception e = exception; Log.WriteLog(e.Message, "Error", this.GetType().Name, this.AppConfig); throw e; } }