可以把全部.bmp格式的移动到另外一个文件夹
文件格式是这样的,要怎么移动
1-周洲-5216(2015-05-29-08-48-37)0.bmp
1-周洲-5216(2015-05-29-08-48-37)1.bmp
例如这两个是一条数据,一个左一个右,只留这两条
1-王文英-(2015-06-18-16-00-12)0.bmp
1-王文英-(2015-06-18-16-00-12)1.bmp
这个也是
1-周洲-5216(2015-05-29-08-48-22)0.bmp
1-周洲-5216(2015-05-29-08-48-22)1.bmp
1-周洲-5216(2015-05-29-08-48-56)0.bmp
1-周洲-5216(2015-05-29-08-48-56)1.bmp
上面这个是多余的数据
下面这是源码,求高手给个过滤数据
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Media;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace mp3
{
public partial class Form1 : Form
{
string s = “”;
string s1 = “”;
string s2 = “”;
string path = @””;
/// <summary>
/// 检索关键字
/// </summary>
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (label2.Text != “” && label3.Text != “”) button4.Enabled = true;
if (radioButton3.Checked == true) s2 = “*.bmp”;
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fo = new FolderBrowserDialog();
if (s2 == “” || s2 == “*”) MessageBox.Show(“请先选择文件类型”, “错误”, MessageBoxButtons.OK);
else
{
if (fo.ShowDialog() != DialogResult.Cancel)
s = fo.SelectedPath;
label2.Text = s;
}
文件格式是这样的,要怎么移动
1-周洲-5216(2015-05-29-08-48-37)0.bmp
1-周洲-5216(2015-05-29-08-48-37)1.bmp
例如这两个是一条数据,一个左一个右,只留这两条
1-王文英-(2015-06-18-16-00-12)0.bmp
1-王文英-(2015-06-18-16-00-12)1.bmp
这个也是
1-周洲-5216(2015-05-29-08-48-22)0.bmp
1-周洲-5216(2015-05-29-08-48-22)1.bmp
1-周洲-5216(2015-05-29-08-48-56)0.bmp
1-周洲-5216(2015-05-29-08-48-56)1.bmp
上面这个是多余的数据
下面这是源码,求高手给个过滤数据
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Media;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace mp3
{
public partial class Form1 : Form
{
string s = “”;
string s1 = “”;
string s2 = “”;
string path = @””;
/// <summary>
/// 检索关键字
/// </summary>
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (label2.Text != “” && label3.Text != “”) button4.Enabled = true;
if (radioButton3.Checked == true) s2 = “*.bmp”;
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fo = new FolderBrowserDialog();
if (s2 == “” || s2 == “*”) MessageBox.Show(“请先选择文件类型”, “错误”, MessageBoxButtons.OK);
else
{
if (fo.ShowDialog() != DialogResult.Cancel)
s = fo.SelectedPath;
label2.Text = s;
}
}
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog fo1 = new FolderBrowserDialog();
if (fo1.ShowDialog() != DialogResult.Cancel)
s1 = fo1.SelectedPath;
label3.Text = s1;
}
private void button4_Click(object sender, EventArgs e)
{
foreach (string item in (Directory.GetFiles(s, s2, SearchOption.TopDirectoryOnly)))
{
FileInfo fi = new FileInfo(item);
string tmp = s1 + @”” + fi.Name;
fi.MoveTo(tmp);
}
label2.Text = “”;
label3.Text = “”;
}
}
}
解决方案:30分
string txt = @"1-周洲-5216(2015-05-29-08-48-37)0.bmp 1-周洲-5216(2015-05-29-08-48-37)1.bmp 1-王文英-(2015-06-18-16-00-12)0.bmp 1-王文英-(2015-06-18-16-00-12)1.bmp 1-周洲-5216(2015-05-29-08-48-22)0.bmp 1-周洲-5216(2015-05-29-08-48-22)1.bmp 1-周洲-5216(2015-05-29-08-48-56)0.bmp 1-周洲-5216(2015-05-29-08-48-56)1.bmp"; var files = Regex.Split(txt, @"\s+"); var query = files.GroupBy(x => Regex.Match(x, @"^[^-]*-[^-]*").Value).SelectMany(g => g.OrderBy(y => y).Take(2)); foreach (var q in query) { Console.WriteLine(q); }
不知道你这个过滤规则怎样的,随便写了个,只取最前面的两个路径
解决方案:10分
var files = new DirectoryInfo("原始文件夹") .GetFiles().Where(x => x.Extension.ToLower() == ".bmp").OrderByDescending(x => x.Name); List<string> listFileName = new List<string>(); foreach(var file in files) { string names = file.Name.Split(""-""); string name = ""; if(name.Length >= 2) { name = names[0] + names[1]; } string leftOrRight = file.Name.Substring(file.Name.IndexOf(".") - 1, 1); if(listFileName.IndexOf(name + leftOrRight) >= 0) { file.MoveTo("目标文件夹"); } else { listFileName.Add(name + leftOrRight); } }