ZIP压缩文件夹中上个月的文件,并将备份文件拷贝到服务器
遍历文件夹的子文件夹下的所有文件,将上个月的文件集中到一起,然互压缩,并copy到服务器的映射磁盘。
static void Main(string[] args)
{
//原始文件存放的位置
DirectoryInfo theFolder = new DirectoryInfo(@"E:\TestFloder");
//将文件拷贝到该文件夹下
string targetPath = @"D:\test";
DirectoryInfo[] dirInfo = theFolder.GetDirectories();
//遍历文件夹下的子文件夹
foreach (DirectoryInfo folder in dirInfo)
{
FileInfo[] filesInfo = folder.GetFiles();
//遍历子文件夹下的文件
foreach (FileInfo file in filesInfo)
{
//将上个月的文件拷贝到
if (file.CreationTime.ToShortDateString().Equals(DateTime.Now.AddMonths(-1).ToShortDateString()))
{
if (!CopyFile(file, targetPath))
{
//log 复制文件失败
}
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))
{
//压缩文件失败
}
else
{
//压缩文件成功
//将压缩后的文件拷贝到映射的服务器磁盘
if (!CopyFile(zipFilePath, remotePath))
{
//Copy失败
}
else
{
//copy成功
//删除原有的
File.Delete(zipFilePath);
}
}
}
/// <summary>
/// 移动文件到指定目录
/// </summary>
/// <param name="file">文件</param>
/// <param name="targetPath">目标路径</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>
/// 移动文件到指定目录
/// </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>
/// 压缩文件
/// </summary>
/// <param name="folderPath">被压缩文件夹路径</param>
/// <param name="zipPath">压缩文件路径</param>
/// <returns></returns>
public static bool ZipFiles(string folderPath, string zipPath)
{
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
//软件7Z的安装目录
process.StartInfo.FileName = @"D:\Program Files\7-Zip\7z.exe";
process.StartInfo.Arguments = string.Format(" a -tzip {0} {1}", zipPath, folderPath);
process.Start();
//一定要等待完成后,才能删除。
process.WaitForExit();
//删除文件夹下文件
DeleteFiles(folderPath);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 删除文件夹下文件
/// </summary>
/// <param name="folderPath">文件夹路径</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 删除文件夹下文件成功
}
catch (Exception ex)
{
//log 删除文件夹下文件失败
throw ex;
}
}
ZIP压缩文件夹中上个月的文件,并将备份文件拷贝到服务器的更多相关文章
- python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件
python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...
- PHP遍历、删除文件夹中的所有文件
<?php header("Content-type:text/html;charset=utf-8"); /** * getDirFile 遍历文件夹中的所有文件 * @p ...
- C++读取文件夹中所有的文件或者是特定后缀的文件
由于经常有读取一个文件夹中的很多随机编号的文件,很多时候需要读取某些特定格式的所有文件. 下面的代码可以读取指定文件家中的所有文件和文件夹中格式为jpg的文件 参考: http://www.2cto. ...
- C#遍历指定文件夹中的所有文件(转)
原文链接:http://www.cnblogs.com/qianqianfy/archive/2009/07/08/1518974.html 1. C#遍历指定文件夹中的所有文件 DirectoryI ...
- C#遍历指定文件夹中的所有文件(转)
C#遍历指定文件夹中的所有文件 DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);//遍历文件夹foreach(DirectoryIn ...
- 在linux和Mac中访问某个文件夹中所有的文件
在linux和Mac中访问某个文件夹中所有的文件——输出所有的文件名 #include <stdio.h> #include <dirent.h> int main() { s ...
- [R语言]读取文件夹下所有子文件夹中的excel文件,并根据分类合并。
解决的问题:需要读取某个大文件夹下所有子文件夹中的excel文件,并汇总,汇总文件中需要包含的2部分的信息:1.该条数据来源于哪个子文件夹:2.该条数据来源于哪个excel文件.最终,按照子文件夹单独 ...
- 【集中工作薄】 当前文件夹中所有Excel文件中 多个工作簿的第一个工作表 复制到工作簿中
功能:当前文件夹中所有Excel文件中 多个工作簿的第一个工作表 复制到工作簿中 Sub Books2Sheets() '定义对话框变量 Dim fd As FileDialog Set fd = A ...
- Java以流的方式将指定文件夹里的.txt文件全部复制到另一文件夹,并删除原文件夹中所有.txt文件
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
随机推荐
- Linux中link,unlink,close,fclose详解
每一个文件,都可以通过一个struct stat的结构体来获得文件信息,其中一个成员st_nlink代表文件的链接数. 当通过shell的touch命令或者在程序中open一个带有O_CR ...
- Already tried 0 time(s); retry policy is RetryUpToMaximumCountWithFixedSleep解决方法
14/03/26 23:10:04 INFO ipc.Client: Retrying connect to server: 0.0.0.0/0.0.0.0:10020. Already tried ...
- 《Introduction to Algorithm》-chaper30-多项式与快速傅里叶变换
两个n次多项式的相加最直接的方法所需要的时间是O(n),而实现两个n次多项式的乘法的直接方法则需要O(n^2),本章讨论的快速傅里叶变换(FFT),将会将这一过程的时间复杂度降至O(nlogn).同时 ...
- 大型网站应用中MySQL的架构演变史
没有什么东西是一成不变的,包含我们的理想和生活!MySQL作为一个免费的开源的关系型数据库,深受大家喜爱,从最初的无人问津到当下的去IOE,都体现出了MySQL举足轻重的作用.今天我们就从淘宝的发展来 ...
- http://www.cnblogs.com/xdp-gacl/p/3622275.html
http://www.cnblogs.com/xdp-gacl/p/3622275.html
- 使用Ready2Search来定制Firefox和Chrome的搜索框
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:使用Ready2Search来定制Firefox和Chrome的搜索框.
- hdu4486 Pen Counts(水题)
Pen Counts Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 在 Android 的 IM 应用中使用 asmack 库实现用户头像的传输(基于VCard协议)
http://quietmadman.blog.51cto.com/3269500/1359495 Beem http://beem-project.com/projects/beem/reposit ...
- oracle并行模式(Parallel)
1. 用途 强行启用并行度来执行当前SQL.这个在Oracle 9i之后的版本可以使用,之前的版本现在没有环境进行测试.也就是说,加上这个说明,可以强行启用Oracle的多线程处理功能.举例的话,就 ...
- strace
-c 统计每一系统调用的所执行的时间,次数和出错的次数等. -d 输出strace关于标准错误的调试信息. -f 跟踪由fork调用所产生的子进程. -ff 如果提供-o filename,则所有进程 ...