Hello,
I want to search for two strings "+error" and "+denied" at .txt files in one Folder.
If one line exist in such file, it should Export this line into an new txt file.
How can I do
Technology Tips and News
Hello,
I want to search for two strings "+error" and "+denied" at .txt files in one Folder.
If one line exist in such file, it should Export this line into an new txt file.
How can I do
Get-ChildItem C:\MyFolder\*.txt | Foreach { $file = $_.FullName Get-Content $file | Foreach { if (($_ -contains "+error") -or ($_ -contains "+denied")) { Out-File C:\MyNewFile.txt -InputObject $_ -Append } } }
Hello,
thanks a lot.
The value from files is:
trhrteh
rth
tr
htrhtrh
rther
th+error
12344+error
12345+error
+error
+denied5354353
+denied
and the ouptut at MyNewFile is:
+error
+denied
But I want also have the values with something bevore and, the Output should look like:
th+error
12344+error
12345+error
+error
+denied5354353
+denied
Can try updating the if statement to be
if (($_ -like "*+error*") -or ($_ -like "*+denied*")) { Out-File C:\MyNewFile.txt -InputObject $_ -Append }That way, if the current line has +error or +denied with characters before or after, it should pick it up by using the wild card *
Thats it!
Thanks a lot!