吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6373|回复: 51
收起左侧

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

  [复制链接]
Cool_Breeze 发表于 2021-5-30 11:03
本帖最后由 Cool_Breeze 于 2021-6-11 09:33 编辑

界面很丑,没有使用 visual studio 写着实费劲!
1.png

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

编译程序时使用的控制台模式,所以有一个控制窗口。
给大家提供一个编译成窗口模式的方法(没有控制台窗口)。
需要进去控制台(cmd),然后更改目录至存放源代码目录(pushd d:\123\源代码目录)
1: 窗口模式
[Bash shell] 纯文本查看 复制代码
csc /t:winexe 代码.cs

2:控制台模式
[Bash shell] 纯文本查看 复制代码
csc /t:exe  代码.cs


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

以下是源代码:
[C#] 纯文本查看 复制代码
// csc FindFileForm.cs

using 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;
    }}

免费评分

参与人数 9吾爱币 +16 热心值 +9 收起 理由
myjian234 + 1 + 1 我很赞同!
lookskys + 1 + 1 谢谢@Thanks!
Loksr1 + 1 + 1 我很赞同!
王金彪 + 1 + 1 热心回复!
抱薪风雪雾 + 1 + 1 谢谢@Thanks!
笙若 + 1 + 1 谢谢@Thanks!
快车0326 + 1 + 1 感谢分享!
老墙 + 2 + 1 谢谢@Thanks!
风之暇想 + 7 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| Cool_Breeze 发表于 2021-5-30 13:13
本帖最后由 Cool_Breeze 于 2021-6-1 10:44 编辑

21/6/1
放弃Task模块,因为XP系统不支持,使用Threading模块(我想XP应该是支持的,未测试!)。
界面改动了一些!
已知问题暂未被修复!
21/5/30
文件名区分大小写, 未找到的文件或者目录名放在源文件目录下的 未找到.txt
后续优化!已知bug,访问不了目录会报错(这个错误并没有被捕获)! 尽量不要在根目录和需要权限的目录下面搜索!
默认会复制目录结构。
 楼主| Cool_Breeze 发表于 2021-6-1 11:04
baicaosheng 发表于 2021-5-30 11:53
Jinhui 发表于 2021-5-30 11:24
沙发一下
龍謹 发表于 2021-5-30 11:25
问个题外话,不用VS那用的啥?
 楼主| Cool_Breeze 发表于 2021-5-30 11:27
龍謹 发表于 2021-5-30 11:25
问个题外话,不用VS那用的啥?

一款文本编辑器, 使用命令行 编译!
bing90740 发表于 2021-5-30 11:31
太厉害了,谢谢分享666.。。。。。
讲话 发表于 2021-5-30 11:35
支持原创~!
 楼主| Cool_Breeze 发表于 2021-5-30 13:04
baicaosheng 发表于 2021-5-30 11:53
源文件那里有bug。

已知无法访问的目录会报错!
头像被屏蔽
youximang 发表于 2021-5-30 13:10
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 提醒:禁止复制他人回复等『恶意灌水』行为,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-26 15:21

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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