using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Configuration;
namespace ChangePdfName
{
class Program
{
static void Main(string[] args)
{
//年报地址
string pdfSourcePath = ConfigurationManager.AppSettings["pdfSourcePath"];
//结尾字符串
string indexOfString = ConfigurationManager.AppSettings["indexOfString"];
//开始日期
string beginDate = ConfigurationManager.AppSettings["beginDate"];
//新转换的pdf存储地址
string newPdfStoragePath = ConfigurationManager.AppSettings["newPdfStoragePath"];
//已经转换过的pdf存储地址
string hasTranslatePdf = ConfigurationManager.AppSettings["hasTranslatePdf"];
//获取下面每一个子文件夹
string[] childFolders = Directory.GetDirectories(pdfSourcePath);
int pdfCount =Convert.ToInt32(ConfigurationManager.AppSettings["pdfCount"]);
//要转换的最大文件大小
string fileMaxSize = ConfigurationManager.AppSettings["fileMaxSize"];
foreach (string pdfFolder in childFolders)
{
//通过pdf名称获得文件日期
int date = int.Parse(pdfFolder.Substring(pdfFolder.LastIndexOf("\\") + 1, pdfFolder.IndexOf('_') - pdfFolder.LastIndexOf("\\") - 1));
//获取开始日期之后的文件
if (date >= int.Parse(beginDate))
{
string[] pdfFiles = Directory.GetFiles(pdfFolder);
foreach (string oldPdfFile in pdfFiles)
{
if (oldPdfFile.IndexOf(indexOfString) > -1 && oldPdfFile.IndexOf("pdf")>-1)
{
string year = GetYear(oldPdfFile);
CreateForder(newPdfStoragePath);
DirectoryInfo Dir = new DirectoryInfo(newPdfStoragePath);
//文件个数pdfCount以内
if (Dir.GetFiles().Length >= pdfCount)
break;
FileInfo fileInfo = new FileInfo(oldPdfFile);
if (fileInfo.Length > 0)
{
string size = CountSize(int.Parse(fileInfo.Length.ToString()));
if (Convert.ToInt32(size.Substring(0, size.Length - 1)) > int.Parse(fileMaxSize))
continue;
string symbol = oldPdfFile.Substring(oldPdfFile.LastIndexOf("\\") + 1, 6);
string type = GetTypeByIndexOf(indexOfString);
//新的存储名称
string strStorageName = GetPdfName(newPdfStoragePath, symbol, date, year, type, size);
string hasExistName = GetPdfName(hasTranslatePdf, symbol, date, year, type, size);
if (File.Exists(hasExistName) || File.Exists(strStorageName))
{
Console.WriteLine("{0}已经存在!", hasExistName);
continue;
}
else
{
File.Copy(oldPdfFile, strStorageName);
Console.WriteLine("{0}复制完成!", strStorageName);
}
}
}
}
}
}
Console.WriteLine("文件遍历完成!");
Console.ReadKey();
}
/// <summary>
/// 拼接得到PDF的名称
/// </summary>
/// <param name="path">PDF路径</param>
/// <param name="symbol">代码</param>
/// <param name="date">日期</param>
/// <param name="year">年份</param>
/// <param name="type">报告类别</param>
/// <param name="size">报告大小</param>
/// <returns></returns>
public static string GetPdfName(string path, string symbol, int date, string year, string type, string size)
{
//000951-20171027-测试报告:2017年第三季度季报_245K
return string.Format("{0}{1}-{2}-测试报告:{3}年{4}_{5}.pdf", path, symbol, date, year, type, size);
}
/// <summary>
/// 获得年份
/// </summary>
/// <param name="oldPdfFile"></param>
/// <returns></returns>
public static string GetYear(string oldPdfFile)
{
string[] oldPdfInfo = oldPdfFile.Split('\\');
//得到年份
string year = oldPdfInfo[oldPdfInfo.Length - 1].Substring(oldPdfInfo[oldPdfInfo.Length - 1].IndexOf('_') + 1, oldPdfInfo[oldPdfInfo.Length - 1].LastIndexOf('_') - oldPdfInfo[oldPdfInfo.Length - 1].IndexOf('_') - 1);
return year;
}
/// <summary>
/// 获得文件大小
/// </summary>
/// <param name="Size"></param>
/// <returns></returns>
public static string CountSize(long Size)
{
string m_strSize = "";
m_strSize = (Size / 1024).ToString("") + "K";
return m_strSize;
}
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="path">文件夹路劲</param>
public static void CreateForder(string path)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
}
/// <summary>
/// 获得报告格式
/// </summary>
/// <param name="indexOfString"></param>
/// <returns></returns>
private static string GetTypeByIndexOf(string indexOfString)
{
string type = "";
switch (indexOfString)
{
case "_z.pdf":
type = "半年度报告";
break;
case "_1.pdf":
type = "第一季度季报";
break;
case "_3.pdf":
type = "第三季度季报";
break;
case "_n.pdf":
type = "年度报告";
break;
}
return type;
}
}
}
更改pdf名称
最新推荐文章于 2023-01-03 21:44:51 发布