找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 24712|回复: 0

[综合工具] 搜索文件和目录,然后复制文件

[复制链接]

该用户从未签到

发表于 2021-6-2 18:33:55 | 显示全部楼层 |阅读模式

您需要 登录 才可以下载或查看,没有账号?立即注册

×
界面很丑,没有使用 visual studio 写着实费劲!
t0hBHniPN50Bn65i.jpg 1.png [i,(50.34 KB, 下载次数: 0)[/i,
下载附件
2021-6-1 09:26 上传





加载文件功能: 把所有需要搜索的文件或者目录名 放到一个文本里面(一个放一行)。选中这个文件即可
前三个按键依次选中,开始就可以了!

百度云盘
http://pan.baidu.com/s/1cDJVnDUoPAPZqSg19AMnYg
提取码:hrj6

以下是源代码:
[C#, [i,纯文本查看[/i, [i,复制代码[/i,
// csc FindFileForm.csusing System;using System.IO;using System.ComponentModel;using System.Drawing;using System.Windows.Forms;using System.Collections.Generic;using System.Threading;// 程序界面public partial class HomeForm : Form{    [STAThread,    static void Main()    {        Application.EnableVisualStyles();        Application.Run(new HomeForm());    }}public partial class HomeForm{    private string SelectedPath;    private string SourceFileName;    private Label tipslbl;    private TextBox SourceCententtxt;    private string CopyToDirectory;        private Label ShowSourcePathlbl;    private Label ShowTargetPathlbl;    private Label ShowCopyToDirPathlbl;        public HomeForm()    {        // 文件按键        Button loadFileBtn = new Button();        loadFileBtn.Size = new Size(150] 30);        loadFileBtn.Location = new Point(30] 20);        loadFileBtn.Text = "加载源文件";        this.Controls.Add(loadFileBtn);        loadFileBtn.Click += new EventHandler(loadFileBtnClick);                // 目录按键        Button selectFolderBtn = new Button();        selectFolderBtn.Size = new Size(150] 30);        selectFolderBtn.Location = new Point(30] 80);        selectFolderBtn.Text = "选择搜索目录";        this.Controls.Add(selectFolderBtn);        selectFolderBtn.Click += new EventHandler(selectFolderBtnClick);                // 复制文件存放目录按键        Button CopyFileDirBtn = new Button();        CopyFileDirBtn.Size = new Size(150] 30);        CopyFileDirBtn.Location = new Point(30] 140);        CopyFileDirBtn.Text = "选择复制文件存放目录";        this.Controls.Add(CopyFileDirBtn);        CopyFileDirBtn.Click += new EventHandler(CopyFileDirBtnClick);                // 开始任务按键        Button StartTaskBtn = new Button();        StartTaskBtn.Size = new Size(150] 30);        StartTaskBtn.Location = new Point(30] 200);        StartTaskBtn.Text = "开始";        this.Controls.Add(StartTaskBtn);        StartTaskBtn.Click += new EventHandler(StartTaskBtnClick);                ShowSourcePathlbl = new Label();        ShowSourcePathlbl.Font = new Font(ShowSourcePathlbl.Font.Name, 11);        ShowSourcePathlbl.Size = new Size(500] 30);        ShowSourcePathlbl.Location = new Point(200] 20);        ShowSourcePathlbl.TextAlign = ContentAlignment.MiddleLeft;        ShowSourcePathlbl.Text = "请选择源文件";                ShowTargetPathlbl = new Label();        ShowTargetPathlbl.Font = new Font(ShowSourcePathlbl.Font.Name, 11);        ShowTargetPathlbl.Size = new Size(500] 30);        ShowTargetPathlbl.Location = new Point(200] 80);        ShowTargetPathlbl.TextAlign = ContentAlignment.MiddleLeft;        ShowTargetPathlbl.Text = "请选择搜索目录";                ShowCopyToDirPathlbl = new Label();        ShowCopyToDirPathlbl.Font = new Font(ShowSourcePathlbl.Font.Name, 11);        ShowCopyToDirPathlbl.Size = new Size(500] 30);        ShowCopyToDirPathlbl.Location = new Point(200] 140);        ShowCopyToDirPathlbl.TextAlign = ContentAlignment.MiddleLeft;        ShowCopyToDirPathlbl.Text = "请选择复制文件存放目录";        this.Controls.Add(ShowCopyToDirPathlbl);        this.Controls.Add(ShowTargetPathlbl);        this.Controls.Add(ShowSourcePathlbl);                // 显示信息        tipslbl = new Label();        tipslbl.Size = new Size(1000] 30);        tipslbl.Location = new Point(30] 260);        this.Controls.Add(tipslbl);                // 源文件内容展示        SourceCententtxt = new TextBox()        {            WordWrap = true,            Multiline = true,            ScrollBars = ScrollBars.Both,        };        SourceCententtxt.Size = new Size(680] 260);        SourceCententtxt.Location = new Point(30] 300);        this.Controls.Add(SourceCententtxt);                this.Size = new Size(800] 600);    }        private void loadFileBtnClick(object sender, EventArgs e)    {        SourceFileName = UserSelectedFile(Directory.GetCurrentDirectory());        tipslbl.Text = SourceFileName;        ShowSourcePathlbl.Text = SourceFileName;        SourceCententtxt.Lines = File.ReadAllLines(SourceFileName, System.Text.Encoding.Default);    }        private void selectFolderBtnClick(object sender, EventArgs e)    {        SelectedPath = UserSelectedPath(Directory.GetCurrentDirectory());        tipslbl.Text = SelectedPath;        ShowTargetPathlbl.Text = SelectedPath;    }        private void CopyFileDirBtnClick(object sender, EventArgs e)    {        CopyToDirectory = UserSelectedPath(Directory.GetCurrentDirectory());        tipslbl.Text = CopyToDirectory;        ShowCopyToDirPathlbl.Text = CopyToDirectory;    }        private void StartTaskBtnClick(object sender, EventArgs e)    {        if (SourceFileName == null ||            SelectedPath == null ||            CopyToDirectory == null)        {            return;        }                FindDirectoryAndFileName fdf = new FindDirectoryAndFileName(SourceFileName, SelectedPath, CopyToDirectory);        Thread th = new Thread(ThreadTask);        th.Start(fdf);    }    // 线程任务    private void ThreadTask(object FDF)    {        FindDirectoryAndFileName fdf;        fdf = FDF as FindDirectoryAndFileName;        tipslbl.Text = "任务进行中!";        fdf.GetResult();        tipslbl.Text = "任务完成!";    }    // 选择目标目录    public string UserSelectedPath(string defaultPath)    {        string res;        FolderBrowserDialog fb = new FolderBrowserDialog();        fb.RootFolder = Environment.SpecialFolder.Desktop;        fb.Description = "请选择待扫描目标目录";        if (defaultPath != null)        {            fb.SelectedPath = defaultPath;                    }        while (true)        {            if (fb.ShowDialog() == DialogResult.OK)            {                res = fb.SelectedPath;                break;            }        }        return res;    }        // 选择文件    public string UserSelectedFile(string defaultPath)    {        string FileFullName;        var openFileDialog = new OpenFileDialog()        {            Filter = "txt (*.txt)|*.txt|All (*.*)|*.*"]        };        if (defaultPath != null)        {            openFileDialog.InitialDirectory = defaultPath;        }        while (true)        {            if (openFileDialog.ShowDialog() == DialogResult.OK)            {                FileFullName = openFileDialog.FileName;                break;            }           }        return FileFullName;    }}// 文件查找操作public class FindDirectoryAndFileName{    private string SourceFile { get; set; }    private string TargetPath { get; set; }    private string NewDirectory { get; set; }        private string[, SourceFileContent { get; set; }        private List<string> TargetAllDir;    private List<string> TargetAllDirName { get; set; }    private List<string> TargetAllFile;    private List<string> TargetAllFileName { get; set; }        public FindDirectoryAndFileName(string sourceFile, string targetPath, string newDirectory)    {        SourceFile = sourceFile;        TargetPath = targetPath;        NewDirectory = newDirectory;                SourceFileContent = File.ReadAllLines(SourceFile, System.Text.Encoding.Default);                TargetAllDirName = GetFileName(Directory.EnumerateDirectories(TargetPath, "*"] SearchOption.AllDirectories)] out TargetAllDir);                TargetAllFileName = GetFileName(Directory.EnumerateFiles(TargetPath, "*"] SearchOption.AllDirectories)] out TargetAllFile);    }        public void GetResult()    {        List<int> res;        bool found = false;        List<string> NotFound = new List<string>();        foreach (string source in SourceFileContent)        {            found = false;            // 复制目录(树)            res = FindDirAndFileName(source, TargetAllDirName);            if (res.Count > 0)            {                found = true;                foreach (int index in res)                {                                        // 复制目录结构                    string newTargetDir = PathReplace(TargetAllDir[index,, NewDirectory);                    if (!Directory.Exists(newTargetDir))                    {                        Directory.CreateDirectory(newTargetDir);                    }                                        foreach (string subDir in Directory.EnumerateDirectories(TargetAllDir[index,, "*"] SearchOption.AllDirectories))                    {                        string newSubDir = PathReplace(subDir, NewDirectory);                        if (!Directory.Exists(newSubDir))                        {                            Directory.CreateDirectory(newSubDir);                        }                    }                                        // 复制文件                    foreach (string subFile in Directory.EnumerateFiles(TargetAllDir[index,, "*"] SearchOption.AllDirectories))                    {                        File.Copy(subFile, PathReplace(subFile, NewDirectory)] true);                    }                }            }                        // Console.WriteLine(1);            // 复制文件            res = FindDirAndFileName(source, TargetAllFileName);            if (res.Count > 0)            {                found = true;                foreach (int index in res)                {                    string newFileFullName = PathReplace(TargetAllFile[index,, NewDirectory);                    string newFileFullDir = Path.GetDirectoryName(newFileFullName);                    if (!Directory.Exists(newFileFullDir))                    {                        Directory.CreateDirectory(newFileFullDir);                    }                    // Console.WriteLine(TargetAllFile[index,);                    // Console.WriteLine(newFileFullName);                    File.Copy(TargetAllFile[index,, newFileFullName, true);                }            }                        if (!found)            {                // Console.WriteLine("{0} 未找到!"] source);                NotFound.Add(source);            }        }        string NotFoundFullName = Path.Combine(Path.GetDirectoryName(SourceFile)] "未找到.txt");        File.WriteAllLines(NotFoundFullName, NotFound, System.Text.Encoding.Default);    }        // 扫描目标目录文件目录结构    static List<string> GetFileName(IEnumerable<string> Data, out List<string> ToTList)    {        List<string> FileName = new List<string>();        ToTList = new List<string>();        foreach (var dir in Data)        {            FileName.Add(Path.GetFileName(dir));            ToTList.Add(dir);        }        return FileName;    }        // 查找文件或者目录    static List<int> FindDirAndFileName(string source, List<string> Target)    {        List<int> result = new List<int>();        int index = Target.IndexOf(source);        if (index == -1) return result;        result.Add(index);        while (true)        {           index = Target.IndexOf(source, index + 1);           if (index != -1)           {               result.Add(index);           }           else           {               break;           }        }        return result;    }        // 路径替换    static string PathReplace(string source, string newDirectory)    {        string newPath;        // 判断是否在一个盘符下面        if (Path.GetPathRoot(source).ToUpper() == Path.GetPathRoot(newDirectory).ToUpper())        {            int index = 0;            while (index < newDirectory.Length)            {                if (char.ToUpper(source[index,) == char.ToUpper(newDirectory[index,))                {                    index++;                }                else                {                    break;                }            }            while (true)            {                if (source[--index, == '\\')                {                    newPath = Path.Combine(newDirectory, source.Substring(index + 1));                    break;                }            }        }        else        {            newPath = Path.Combine(newDirectory, source.Substring(3));        }        return newPath;    }}
[/td][/tr][/table]
回复

使用道具 举报

网站地图|页面地图|文字地图|Archiver|手机版|小黑屋|找资源 |网站地图

GMT+8, 2024-5-6 09:30

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表