遍历文件夹的子文件夹下的所有文件,将上个月的文件集中到一起,然互压缩,并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压缩文件夹中上个月的文件,并将备份文件拷贝到服务器的更多相关文章

  1. python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件

    python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...

  2. PHP遍历、删除文件夹中的所有文件

    <?php header("Content-type:text/html;charset=utf-8"); /** * getDirFile 遍历文件夹中的所有文件 * @p ...

  3. C++读取文件夹中所有的文件或者是特定后缀的文件

    由于经常有读取一个文件夹中的很多随机编号的文件,很多时候需要读取某些特定格式的所有文件. 下面的代码可以读取指定文件家中的所有文件和文件夹中格式为jpg的文件 参考: http://www.2cto. ...

  4. C#遍历指定文件夹中的所有文件(转)

    原文链接:http://www.cnblogs.com/qianqianfy/archive/2009/07/08/1518974.html 1. C#遍历指定文件夹中的所有文件 DirectoryI ...

  5. C#遍历指定文件夹中的所有文件(转)

    C#遍历指定文件夹中的所有文件 DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);//遍历文件夹foreach(DirectoryIn ...

  6. 在linux和Mac中访问某个文件夹中所有的文件

    在linux和Mac中访问某个文件夹中所有的文件——输出所有的文件名 #include <stdio.h> #include <dirent.h> int main() { s ...

  7. [R语言]读取文件夹下所有子文件夹中的excel文件,并根据分类合并。

    解决的问题:需要读取某个大文件夹下所有子文件夹中的excel文件,并汇总,汇总文件中需要包含的2部分的信息:1.该条数据来源于哪个子文件夹:2.该条数据来源于哪个excel文件.最终,按照子文件夹单独 ...

  8. 【集中工作薄】 当前文件夹中所有Excel文件中 多个工作簿的第一个工作表 复制到工作簿中

    功能:当前文件夹中所有Excel文件中 多个工作簿的第一个工作表 复制到工作簿中 Sub Books2Sheets() '定义对话框变量 Dim fd As FileDialog Set fd = A ...

  9. Java以流的方式将指定文件夹里的.txt文件全部复制到另一文件夹,并删除原文件夹中所有.txt文件

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

随机推荐

  1. 【原】1.1RDD源码解读(二)

    (6)transformation 操作,通过外在的不同RDD表现形式来达到内部数据的处理过程.这类操作并不会触发作业的执行,也常被称为lazy操作. 大部分操作会生成并返回一个新的RDD,例sort ...

  2. 非阻塞,send后马上close消息能成功发出去吗

    send返回成功的话就能. 判断send返回,假如返回的值是成功,则说明要发送的放到了tcp的发送缓冲区内了,然后在close的话等于再放了一个关闭信号到缓冲区里的. 由于TCP是有序的,假如对端此时 ...

  3. Configure the AD FS server for claims-based authentication -zhai zi wangluo

    Applies To: Microsoft Dynamics CRM 2011, Microsoft Dynamics CRM 2013 After enabling claims-based aut ...

  4. sudo 和 sudoers设置

    转: http://www.cnblogs.com/zhuowei/archive/2009/04/13/1435190.html sudo是linux下常用的允许普通用户使用超级用户权限的工具,允许 ...

  5. requirejs 打包参数

    https://github.com/requirejs/r.js/blob/master/build/example.build.js

  6. Android模拟器如何加载本机地址及访问本机服务器

    首先获取本机ip地址: 在cmd 命令窗口中输入 ipconfig 查看本地电脑ip地址如下: 获取服务器上的Json数据,并返回结果,部分代码如下: 注:StreamUtils是自定义的一个工具类, ...

  7. WinForm简单多国语言实现

    参考:http://minmin86121.blog.163.com/blog/static/4968115720119259151898/ http://www.cnblogs.com/hakuci ...

  8. 部署服务--NLB

    通过服务部署NLB,是对某一层(一层下面可以自定义VM实例数量)服务下的多台VM进行NLB,而不是对多个层进行NLB.需要先进行如下准备: 1.VM需要使用静态IP和静态MAC,所以需要先在进行NLB ...

  9. [2014.5.22][UBUNTU]Ubuntu与Windows系统时间不同步的问题

    安装Ubuntu+Windows双系统时会遇到Windows和Ubuntu系统时间不同步的问题,这是由于Windows系统默认读取主板bios等硬件系统时间作为OS的当地时间;而MAc,Linux类的 ...

  10. ListBox基础

    关键点   在列表框生成后需要向其中加入或是删除行,可以利用: int AddString( LPCTSTR lpszItem )添加行, int DeleteString( UINT nIndex ...