ZIP compresses the last month's files in the folder and copies the backup files to the server. zip last month

Source: Internet
Author: User

ZIP compresses the last month's files in the folder and copies the backup files to the server. zip last month

 

Traverse all the files in the subfolders of the folder, combine the files from the previous month, compress each other, and copy the files to the ing disk of the server.

Static void Main (string [] args)
{
// Location where the original file is stored
DirectoryInfo theFolder = new DirectoryInfo (@ "E: \ TestFloder ");
// Copy the object to this folder
String targetPath = @ "D: \ test ";
DirectoryInfo [] dirInfo = theFolder. GetDirectories ();
// Traverse subfolders in a folder
Foreach (DirectoryInfo folder in dirInfo)
{
FileInfo [] filesInfo = folder. GetFiles ();
// Traverse objects in subfolders
Foreach (FileInfo file in filesInfo)
{
// Copy the last month's file
If (file. CreationTime. tow.datestring (). Equals (DateTime. Now. AddMonths (-1). tow.datestring ()))
{
If (! CopyFile (file, targetPath ))
{
// Log copy failed
}
Else
{
File. Delete ();
}
}
}
}


String zipFilePath = @ "D: \" + DateTime. Now. AddMonths (-1). ToString ("yyyyMMdd") + ". zip ";
String remotePath = @ "\ 127.1.0.0" + @ "D: \" + DateTime. Now. AddMonths (-1). ToString ("yyyyMMdd") + ". zip ";

If (! ZipFiles (targetPath, zipFilePath ))
{
// Failed to compress the file
}
Else
{
// The file is compressed successfully.
// Copy the compressed file to the mapped server disk
If (! CopyFile (zipFilePath, remotePath ))
{
// Copy failed
}
Else
{
// Copy successful
// Delete the original
File. Delete (zipFilePath );
}

}

}

/// <Summary>
/// Move the file to the specified directory
/// </Summary>
/// <Param name = "file"> file </param>
/// <Param name = "targetPath"> target path </param>
/// <Returns> </returns>
Public static bool CopyFile (FileInfo file, string targetPath)
{
Try
{
If (! Directory. Exists (targetPath ))
{
Directory. CreateDirectory (targetPath );
}
TargetPath = targetPath + @ "\" + file. Name;

File. Copy (file. FullName, targetPath, true );
Return true;
}
Catch (Exception)
{
Return false;
}
}

/// <Summary>
/// Move the file to the specified directory
/// </Summary>
/// <Param name = "filePath"> </param>
/// <Param name = "targetPath"> </param>
/// <Returns> </returns>
Public static bool CopyFile (string filePath, string targetPath)
{
Try
{
File. Copy (filePath, targetPath, true );
Return true;
}
Catch (Exception)
{
Return false;
}
}

/// <Summary>
/// Compressed file
/// </Summary>
/// <Param name = "folderPath"> compressed folder path </param>
/// <Param name = "zipPath"> compressed file path </param>
/// <Returns> </returns>
Public static bool ZipFiles (string folderPath, string zipPath)
{
Try
{
System. Diagnostics. Process process = new System. Diagnostics. Process ();
// Installation directory of software 7Z
Process. StartInfo. FileName = @ "D: \ Program Files \ 7-Zip \ 7z.exe ";
Process. StartInfo. Arguments = string. Format ("a-tzip {0} {1}", zipPath, folderPath );
Process. Start ();
// You must wait until the deletion is completed.
Process. WaitForExit ();

// Delete objects in a folder
DeleteFiles (folderPath );

Return true;
}
Catch (Exception ex)
{
Throw ex;
}
}

/// <Summary>
/// Delete files in a folder
/// </Summary>
/// <Param name = "folderPath"> folder path </param>
Public static void DeleteFiles (string folderPath)
{
Try
{
DirectoryInfo folder = new DirectoryInfo (folderPath );
FileInfo [] filesInfo = folder. GetFiles ();
Foreach (FileInfo file in filesInfo)
{
File. Delete (file. FullName );
}
// Log: the file in the folder is successfully deleted.
}
Catch (Exception ex)
{
// Log failed to delete files in the folder
Throw ex;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.