I need a way to list all files that does NOT contain a known text.
The project contains over 1000 files and I only want the ones that does not contain s
From a command prompt:
@for /r %f in (FILE_SPECIFIER) do @find "YOUR_STRING" "%f" > nul || echo %f
For example:
C:\web-trunk\Views>@for /r %f in (*.cshtml) do @find "Layout" "%f" > nul || echo %f
C:\data\web-trunk\Views\Account\RegisterPartial.cshtml
C:\data\web-trunk\Views\Admin\SiteInspector.cshtml
C:\data\web-trunk\Views\CandidateProfile\View.cshtml
C:\data\web-trunk\Views\Common\ContactFooterForInfoEmails.cshtml
C:\data\web-trunk\Views\Common\ContactFooterForInfoPages.cshtml
C:\data\web-trunk\Views\Common\ContactFooterForSalesEmails.cshtml
C:\data\web-trunk\Views\Common\ContactFooterForSalesPages.cshtml
Use find or findstr from a command window ('DOS box').
UPDATE: as Peter McVoy pointed out, this solution does not work. It will return false positives (files that contain a line without the text to search for will also be printed). A VS macro or a custom powershell script is probably the way to go. I might revisit this answer later.
For reference, this was my answer:
You can do this on the command line:
findstr /S /V /M text_to_search_for *.cs
This will print all filenames that do not contain the text
You can use the Find in Files feature of notepad++.
Steps:
Here is the loop to get the files list is (searches in .cs
files) (can be optimized):
private void GetFileNamesNotContainingWord(string filePath, string searchDirectoryPath, string notContainingFilePath)
{
string[] lines = File.ReadAllLines(filePath);
List<string> filesList = new List<string>();
foreach (string line in lines)
{
if (!line.StartsWith("\t"))
{
filesList.Add(line.Substring(0, line.LastIndexOf('(')).Trim());
}
}
List<string> notContainedFiles = new List<string>();
foreach (FileInfo file in new DirectoryInfo(searchDirectoryPath).GetFiles("*.cs", SearchOption.AllDirectories))
{
if (!filesList.Contains(file.FullName))
{
notContainedFiles.Add(file.FullName);
}
}
File.WriteAllLines(notContainingFilePath, notContainedFiles.ToArray());
}