在目录中搜索特定文件名 [英] Search for specific file name in directory

查看:25
本文介绍了在目录中搜索特定文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在系统上的所有卷上搜索特定文件名,系统卷信息或 Windows 目录除外,并打印出每个文件的每个完整路径.

I'm trying to search for specific file name on all volumes on the system, except System Volume Information or Windows Directories and print out every full path for each file.

我尝试使用 Directory.GetFiles,但它只在 Drive C: 或 Drive D: 中搜索(取决于指定的驱动器)

I've tried to use Directory.GetFiles, but it only searche inside Drive C: or Drive D: (depends which drive is specified)

我怎样才能实现我的目标?

How can I achieve my goal?

例如我想运行程序并搜索名称为.DS_Store"的文件(以前使用mac,现在我的文件系统充满了这些文件......)

For example I want to run program and search for file with name ".DS_Store" (used to use mac, and now my file system is full of these files....)

我真的很感激任何帮助.提前谢谢您

I would really appreciate any help. Thank you in advance

更新.程序应以管理员权限运行

推荐答案

作为密码锁分析的一部分,我写了一个定位目标扩展的原型,这里是如何使用它

As part of a cryptolocker analysis, I wrote a prototype that locates target extensions, here is how to use it

var cmd = new LocateTargetFilesCommand();
cmd.Execute()

它将在所有驱动器中搜索两个扩展名:.xlsx.doc.最大的问题是处理权限问题.此外,子文件夹可能具有更广泛的权限,因此如果您无法访问父文件夹,您仍然可以访问其根目录.

It will search all the drives for two extensions: .xlsx and .doc. The biggest problem is to handle permissions issue. Also a child folder may have wider permissions, so if you cannot access a parent folder, you can still potentially access its roots.

最后,cmd.FoundTargets 将包含找到的具有所需扩展名的文件的绝对路径

At the end, cmd.FoundTargets will contain absolute paths to the found files with desired extensions

public class LocateTargetFilesCommand 
{
    private string[] _targetExtensions = new[]
        {
            "xlsx", "doc"
        };

    public LocateTargetFilesCommand()
    {
        FoundTargets = new ConcurrentQueue<string>();
        DirsToSearch = new List<string>();
    }

    public ConcurrentQueue<string> FoundTargets { get; private set; }
    public List<string> DirsToSearch { get; private set; }

    public void Execute()
    {
        DriveInfo[] driveInfos = DriveInfo.GetDrives();

        Console.WriteLine("Start searching");
        foreach (var driveInfo in driveInfos)
        {

            if (!driveInfo.IsReady)
                continue;

            Console.WriteLine("Locating directories for drive: " + driveInfo.RootDirectory);
            GetAllDirs(driveInfo.RootDirectory.ToString());
            Console.WriteLine("Found {0} folders", DirsToSearch.Count);

            foreach (var ext in _targetExtensions)
            {
                Console.WriteLine("Searching for .*" + ext);

                int currentNotificationProgress = 0;
                for (int i = 0; i < DirsToSearch.Count; i++)
                {
                    int progressPercentage = (i * 100) / DirsToSearch.Count;
                    if (progressPercentage != 0)
                    {
                        if (progressPercentage % 10 == 0 && currentNotificationProgress != progressPercentage)
                        {
                            Console.WriteLine("Completed {0}%", progressPercentage);
                            currentNotificationProgress = progressPercentage;
                        }
                    }

                    var dir = DirsToSearch[i];
                    try
                    {
                        string[] files = Directory.GetFiles(dir, "*." + ext, SearchOption.TopDirectoryOnly);
                        foreach (var file in files)
                        {
                            FoundTargets.Enqueue(file);
                        }
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                        Console.WriteLine("Skipping directory: {0}", dir);
                        DirsToSearch.Remove(dir);
                    }
                }

                Console.WriteLine("So far located {0} targets", FoundTargets.Count);
            }

            DirsToSearch.Clear();
        }

        Console.WriteLine("Stop searching");
    }

    public void GetAllDirs(string root)
    {
        try
        {
            string[] dirs = Directory.GetDirectories(root);
            DirsToSearch.AddRange(dirs);
            foreach (var dir in dirs)
            {
                GetAllDirs(dir);
            }
        }
        catch (UnauthorizedAccessException)
        {
            //swallow
        }
    }
}

这篇关于在目录中搜索特定文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆