user3542866
user3542866

Reputation: 191

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. 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);
    }
}

}

Upvotes: 2

Views: 648

Answers (1)

sujith karivelil
sujith karivelil

Reputation: 29026

The thing you have to do is, make proper use of the overloaded method Directory.GetFiles() which accepts three params. ie., string path, string 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. 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

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.

Upvotes: 1

Related Questions