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 ...
随机推荐
- 2015年10月16日HTML标签表单笔记
textarea只是纯文本编辑框,要想输入各种样式的文本.图片.表格等需要使用“富文本编辑框”.html4暂无富文本编辑框,可使用第三方工具实现此效果. <textarea></te ...
- kernel网址
http://www.kernel.org HTTP https://www.kernel.org/pub/ FTP ftp://ftp.kernel.org/pub/ http://www.oldl ...
- 如何在编译Xcode-Plugin工程的时候增加Cocoapods依赖
关于如何在编译Xcode-Plugin工程的时候增加Cocoapods依赖 以及在Mac App上使用Cocoapods的时候遇到Library not found for -lPods时的解决办法 ...
- VMWare虚拟机USB连接问题
错误31:连接到系统上的设备没有发挥作用 原文链接 描述 常用 VMware 虚拟机的有事应该遇到这种情况,就是装完 VMware ,启动时 VMware 下面会有个黄框中有" USB di ...
- algorithm@ Matrix fast power
一. 什么是快速幂: 快速幂顾名思义,就是快速算某个数的多少次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高.一般一个矩阵的n次方,我们会通过连乘n-1次来得到它的n次 ...
- cygwin设置中文
cygwin\home\username\.bashrc # 让ls和dir命令显示中文和颜色 alias ls='ls --show-control-chars --color' alias dir ...
- storm-starter项目概述
storm-starter项目包含使用storm的各种各样的例子.项目托管在GitHub上面,其网址为: http://github.com/nathanmarz/storm-starter stor ...
- oc学习之路----代理模式
今天刚学完oc的代理模式,觉得有点新奇,第一次接触,原理 A完成一件事,但是自己不能完成,于是他找个代理人B 替他完成这个事情,他们之间便有个协议 (protocol),B继承该协议来完成A代理给他的 ...
- 【python自动化第八篇:网络编程】
一.拾遗 动态导入模块 目的是为了在导入模块的过程中将模块以字符的格式导入. #!/usr/bin/env python # -*- coding:utf-8 -*- #Author:wanghui ...
- MSSQLSERVER数据库- 上移和下移的存储过程
做一下备忘 MOVEUP: set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROCEDURE [dbo].[Proc_MoveUp] @id ...