Search string and export

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

April 20th, 2015 6:49am

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
    }
  }
}

Possibly something like this might work
Free Windows Admin Tool Kit Click here and download it now
April 20th, 2015 8:43am

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

April 22nd, 2015 6:46am

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 *

Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 8:39am

Thats it!

Thanks a lot!

April 22nd, 2015 9:17am

You're welcome
Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 9:19am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics