界面
使用图片
变量
/// <summary>
/// 记录文件夹路径
/// </summary>
string _filePath = string.Empty;
/// <summary>
/// 文件夹下的 文件数量
/// </summary>
string _fileCount = string.Empty;
/// <summary>
/// 文件夹下的一级文件夹数量
/// </summary>
string _folderCount = string.Empty;
/// <summary>
/// 文件夹列表
/// </summary>
DirectoryInfo[] _folderList = null;
/// <summary>
/// 文件夹下的文件列表
/// </summary>
FileInfo[] _fileList = null;
/// <summary>
/// 目标文件夹路
/// </summary>
string _targetFolderPath = string.Empty;
/// <summary>
/// 目标文件夹文件列表
/// </summary>
DirectoryInfo[] _targetFolderList = null;
代码
private void button6_Click(object sender, EventArgs e)
{
successCopytxt.Text = "";
chuangjianwjjlab.Text = "";
chuangjianwjjtxt.Text = "";
if (string.IsNullOrEmpty(_filePath) || string.IsNullOrEmpty(_targetFolderPath))
{
MessageBox.Show("请按步骤选择文件夹", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (!xmNhtrab.Checked && !fgfhrab.Checked)
{
MessageBox.Show("请选择原文件名类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (fgfhrab.Checked && string.IsNullOrEmpty(fengefutxt.Text))
{
MessageBox.Show("请填写分隔符号【@或#或$或!或+或-】等", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
fengefutxt.Focus();
return;
}
if (fgfhrab.Checked && string.IsNullOrEmpty(duiyingbiaojitxt.Text))
{
MessageBox.Show("请填写【对应标记位置】。根据分隔符分开后的位置标记,填1或2或3或大于0整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
duiyingbiaojitxt.Focus();
return;
}
//记录数据
//找到对应的文件夹的数量
int successCopyCount = 0;
//未找到并创建的数量
int createNewFolderCount = 0;
string chuangjianwjj = string.Empty;
//开始复制
for (int i = 0; i < _fileList.Count(); i++)
{
string fileName = _fileList[i].Name;
//去除文件扩展名
string quchukuozhanming = fileName.Substring(0, fileName.LastIndexOf("."));
//目标文件夹名称
string targetFolderName = string.Empty;
//如果选择了分列
if (fgfhrab.Checked)
{
string[] _ywjmfl = quchukuozhanming.Split(Convert.ToChar(fengefutxt.Text));
//如果填写的标记位置大于文件的分列数量,报错
if (Convert.ToInt32(duiyingbiaojitxt.Text.Trim()) - 1 > _ywjmfl.Count())
{
MessageBox.Show("填写的标记数错误,标记数最大值为文件分列的列数,最小为1", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
duiyingbiaojitxt.Focus();
return;
}
//获得目标文件夹名称
targetFolderName = _ywjmfl[(Convert.ToInt32(duiyingbiaojitxt.Text.Trim())) - 1];
}
//原文件名
else
{
targetFolderName = quchukuozhanming;
}
//获得目标文件夹路径
string targetFolderPath = _targetFolderPath + "\\" + targetFolderName;
//判断目标文件夹是否 包含文件对应的文件夹
if (Directory.Exists(targetFolderPath))
{
CopyFile(fileName, targetFolderName);
successCopyCount++;
}
else
{
Directory.CreateDirectory(targetFolderPath);
CopyFile(fileName, targetFolderName);
createNewFolderCount++;
chuangjianwjj = chuangjianwjj + fileName + ",\r\n";
}
successCopytxt.Text = "成功插入的文件数量【" + successCopyCount.ToString() + "】,无对应文件夹的文件数量【" + createNewFolderCount.ToString() + "】";
if (createNewFolderCount > 0)
{
chuangjianwjjlab.Text = "↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓异常文件记录↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓";
chuangjianwjjtxt.Text = chuangjianwjj.Substring(0, chuangjianwjj.LastIndexOf(","));
}
}
}
CopyFile
private void CopyFile(string fileName, string targetFolderName)
{
string wenjianxinmingcheng = wenjianxinmingchengtxt.Text.Trim();
string ywjdz = _filePath + "\\" + fileName;
if (string.IsNullOrEmpty(wenjianxinmingcheng))
{
string xwjdz = _targetFolderPath + "\\" + targetFolderName + "\\" + fileName;
File.Copy(ywjdz, xwjdz, true);
}
else
{
string xwjdz = _targetFolderPath + "\\" + targetFolderName + "\\" + wenjianxinmingcheng + fileName.Substring(fileName.LastIndexOf("."));
File.Copy(ywjdz, xwjdz, true);
}
}
感谢观看
来都来了,点个赞再走吧
点赞的都是嫖,不点赞的都是白嫖
微信搜索【猿的一日】IT老司机等你上车