简体   繁体   English

搜索包含特定字符串和删除字符串的目录和子目录中的所有文件

[英]Searching all files in directories and sub-directories that contain specific string and remove string

A number of PHP files have been compromised on my web server.我的 Web 服务器上的许多 PHP 文件已被盗用。 I need to find the ones that have the issue and remove the problem code.我需要找到有问题的那些并删除问题代码。 It's taking too long to do manually.手动操作太费时间了。

The plan was to provide the top level directory and have the console app search all files in that directory (including ones in sub-directories) and remove the line in each file.计划是提供顶级目录并让控制台应用程序搜索该目录中的所有文件(包括子目录中的文件)并删除每个文件中的行。

My code is below, it works, but it doesn't seem to look through all directories.我的代码在下面,它可以工作,但它似乎没有查看所有目录。 Files are correctly updated, but files inside of subdirectories are not.文件已正确更新,但子目录中的文件未正确更新。 I am not sure where I am going wrong.我不确定我哪里出错了。 Any assistance would be appreciated.任何援助将不胜感激。

public static string check_path = string.Empty;
public static string log_folder = string.Empty;
public static string extension = string.Empty;

static void Main(string[] args)
{
    if (args == null || args.Length < 1)
    {
        Console.WriteLine("Invalid Syntax");
        return;
    }
    else
    {
        shakaka(args[0]);
    }
}


public static void shakaka(string check_fol)
{
    if (Directory.Exists(check_fol))
    {
        string[] folders = Directory.GetDirectories(check_fol);
        foreach (string folder in folders)
        {
            string[] files = Directory.GetFiles(folder);
            foreach (string file in files)
            {
                extension = Path.GetExtension(file);
                if (extension == ".php")
                {
                    var oldLines = System.IO.File.ReadAllLines(file);
                    var newLines = oldLines.Where(line => !line.Contains("/*457563643*/"));
                    System.IO.File.WriteAllLines(file, newLines);
                    Console.WriteLine("Updated: " + file);
                }
                else
                {
                    Console.WriteLine("Not a PHP File: " + file);
                }
            }
        }

    }
    else
    {
        Console.WriteLine("Directory Doesn't Exist: " + check_fol);
    }
}

} }

The thing you have to do is, make proper use of the overloaded method Directory.GetFiles() which accepts three params.您必须做的是,正确使用接受三个参数的重载方法Directory.GetFiles() ie., string path, string searchPattern, System.IO.SearchOption searchOption即, 字符串路径、字符串 searchPattern、System.IO.SearchOption searchOption

The modified code will be like this:修改后的代码将是这样的:

string[] files = Directory.GetFiles(folder,"*.php", System.IO.SearchOption.AllDirectories);

Here这里

The first parameter(path) specifies the relative or absolute path to the directory to search.第一个参数(路径)指定要搜索的目录的相对或绝对路径。 This string is not case-sensitive.此字符串不区分大小写。

Second parameter(searchPattern) The search string to match against the names of files in path.第二个参数(searchPattern) 匹配路径中文件名的搜索字符串。 This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.此参数可以包含有效文字路径和通配符(* 和 ?)字符的组合,但它不支持正则表达式。 *in your case you need only .php files so it should be *.php *在你的情况下,你只需要.php文件,所以它应该是*.php

And the last param (SearchOption) One of the enumeration values that specifies whether the search operation should include all subdirectories or only the current directory.而最后一个参数 (SearchOption) 是一个枚举值,指定搜索操作应该包括所有子目录还是只包括当前目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM