Today I had to find a way to search for specific values in set of text files. I had around 200 files with each has 6000+ records as pipe ( | ) separated File.
This is how I managed to find the file(s) which was the text.
- Check each file for the existence of the word or phrase which I’m looking for.
- If word is existing, dig deeper in to the file to find to find the record which is having search word in particular column (in my case it was NTID [windows login name of the user].
- Same ID might available in different fields manage ID but I wanted to check the User Login ID
I used PowerShell Contains() method and iterate the file data as | separated csv file.
#Source File Path
$filePath = "C:\script\CSV";
$timeStamp = Get-Date;
$ExecutionID=$timeStamp.ToString("yyyyMMddHHmm");
#All files names which contains the word will be writen in to this file
$NameSearchLogFilePath= "C:\script\logs\NameVerification-$($ExecutionID).log";
#All Recoreds which are maching with LoginID field will be written in to this file
$NameSearchLogFilePathData= "C:\script\logs\NameVerification-$($ExecutionID)-Data.log";
Add-Content -Path $NameSearchLogFilePath -value "Execution ID|FileName|Created|";
function Get-FilesWithName()
{
param($searchText)
#get all files in folder
$files = Get-ChildItem $filePath;
Write-Host "Number of Files : $($files.Count)";
foreach ($file in $files)
{
$dataFile = "$filePath\$($file.Name)"
$filedata = [IO.File]::ReadAllText($dataFile);
#Check the file to check search text existing in the file or not
$fileExist= $filedata.Contains($searchText);
IF($fileExist)
{
#search text is existing in the file and now find the record
$fileCreatedTime = $file.CreationTime;
Add-Content -Path $NameSearchLogFilePath -value "$($ExecutionID)|$($file.Name)|$($file.CreationTime)";
$employeeData = Import-Csv $dataFile -Delimiter "|";
foreach($record in $employeeData)
{
#LoginID is the field which I wanted to check the value
IF($record.LoginID -eq $searchText)
{
Write-Host "Text Matched.. in $($file.Name) ";
Add-Content -Path $NameSearchLogFilePathData -value "$($file.Name) | $($fileCreatedTime) | $($record)";
}
}
}
$filedata="";
}
}
$searchValue ="searchText";
Get-FilesWithName -searchText $searchValue ;
Hope it might help someone.
If you find better options to achieve same results. let me know in the comments
Cheers