FINDSTR output to variable help

Hi All,

I have the following code in the attempt to output the result of two FINDSTR commands to a variable and print it to the screen. Something is going wrong though and I don't know what.

FOR /F "delims=" %%a in ('FINDSTR /I /c:"%_SUBJECT%" %_PATH%\%_LOG_BLACKLIST%" ^| FINDSTR /I /c:"%_FROM%"') do set _BLACKLIST=%%a
ECHO %_BLACKLIST%

Open in new window


 
SET _PATH="\\server\c$\Program Files ^(x86^)\GFI\MailEssentials\Logs

SET _LOG_BLACKLIST=blacklist.log"

Open in new window


The command works fine if I do not enclose it in a FOR loop. Any ideas?

Cheers
agbnielsenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gerwin Jansen, EE MVETopic Advisor Commented:
Can you try this:

FOR /F "delims=" %%a in ('FINDSTR /I /c:"%_SUBJECT%" %_PATH%\%_LOG_BLACKLIST%" ^| FINDSTR /I /c:"%_FROM%"') do (set _BLACKLIST=%%a & ECHO %_BLACKLIST%)

Open in new window

0
Steve KnightIT ConsultancyCommented:
It looks after a quick look like it might be the path being define in a variable, escaping the ( ) characters that is the issue?  You shouldn't need those in place.  Try removing the ^ before the two ( and ).

Steve
0
agbnielsenAuthor Commented:
Okay, interesting.

First off, I discovered an error in my code. The first SET _PATH has an opening double quotation mark, and the SET _LOG_BLACKLIST has a closing double quotation mark. However my code was:



%_PATH%\%_LOG_BLACKLIST%"

Open in new window

0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

agbnielsenAuthor Commented:
Disregard that last post...it was incomplete and have a couple more issues....
%_PATH%\%_LOG_BLACKLIST%"

Open in new window

ECHO 1.
FINDSTR /I /c:"%_SUBJECT%" %_PATH1%\%_LOG_BLACKLIST% | FINDSTR /I /c:"%_FROM%"
ECHO.
ECHO 2.
FOR /F "delims=" %%a in ('FINDSTR /I /c:"%_SUBJECT%" %_PATH1%\%_LOG_BLACKLIST% ^| FINDSTR /I /c:"%_FROM%"') do set _BLACKLIST=%%a
ECHO %_BLACKLIST%
ECHO 3.
FOR /F "delims=" %%a in ('FINDSTR /I /c:"%_SUBJECT%" %_PATH1%\%_LOG_BLACKLIST% ^| FINDSTR /I /c:"%_FROM%"') do (set _BLACKLIST1=%%a & ECHO %_BLACKLIST1%)

Open in new window

0
agbnielsenAuthor Commented:
FFS
0
agbnielsenAuthor Commented:
Okay sorry about that. As per my first reply there was an error in my syntax with double quotation marks. I have since pulled this out. Also, I needed to take out the escape characters from (x86) as per dragon-it's suggestion. Now there is something interesting, I have the following code:

 
ECHO 1.
FINDSTR /I /c:"%_SUBJECT%" %_PATH1%\%_LOG_BLACKLIST% | FINDSTR /I /c:"%_FROM%"
ECHO.
ECHO 2.
FOR /F "delims=" %%a in ('FINDSTR /I /c:"%_SUBJECT%" %_PATH1%\%_LOG_BLACKLIST% ^| FINDSTR /I /c:"%_FROM%"') do set _BLACKLIST=%%a
ECHO %_BLACKLIST%
ECHO 3.
FOR /F "delims=" %%a in ('FINDSTR /I /c:"%_SUBJECT%" %_PATH1%\%_LOG_BLACKLIST% ^| FINDSTR /I /c:"%_FROM%"') do (set _BLACKLIST1=%%a & ECHO %_BLACKLIST1%)

Open in new window


I was searching for "noreply" and "Last chance for upcoming"
Here is the result: (note I have replaced our domain with domain.com, and the users with user1/2/3).

1.
"07/12/11 23:22:18","Email Blocklist","noreply@engage.redhat.com","user1@domain","Last chance for upcoming webcast: Extend Beyond Virtualization to the Cloud: Getting Started","Move to Junk Folder|Tag Subject","Sender blocklisted","<125a2ee4fc254b3ca75caf88420cf0b5@1795>"
"07/12/11 23:25:48","Email Blocklist","noreply@engage.redhat.com","user2@domain","Last chance for upcoming webcast: Extend Beyond Virtualization to the Cloud: Getting Started","Move to Junk Folder|Tag Subject","Sender blocklisted","<46eab591193944da92ddef87aebd4b5a@1795>"
"07/19/11 13:20:28","Email Blocklist","noreply@engage.redhat.com","user2@domain","Last chance for upcoming webcast: Migrate from Solaris with Confidence","Move to Junk Folder|Tag Subject","Sender blocklisted","<89c458da432c4c098b154684545c94f6@1795>"
"07/19/11 13:39:32","Email Blocklist","noreply@engage.redhat.com","user1@domain","Last chance for upcoming webcast: Migrate from Solaris with Confidence","Move to Junk Folder|Tag Subject","Sender blocklisted","<11dc0e90b26c476a8067a174948c3746@1795>"
"07/25/11 23:28:07","Email Blocklist","noreply@engage.redhat.com","user2@domain","Last chance for upcoming webcast: 07/26 - Red Hat Enterprise Virtualization for Desktops: How Red Hat Delivers on the Promise of Desktop Virtualization","Move to Junk Folder|Tag Subject","Sender blocklisted","<bc3cdf38f77547f5ac797c51a5cfe849@1795>"
"08/17/11 23:22:41","Email Blocklist","noreply@engage.redhat.com","user1@domain","Last chance for upcoming webcast: Red Hat Enterprise Virtualization for Servers: How Red Hat Delivers on the Promise of Enterprise Virtualization","Move to Junk Folder|Tag Subject","Sender blocklisted","<e9a89257ea5849d7a7c18435af05cc04@1795>"
"08/17/11 23:51:58","Email Blocklist","noreply@engage.redhat.com","user2@domain","Last chance for upcoming webcast: Red Hat Enterprise Virtualization for Servers: How Red Hat Delivers on the Promise of Enterprise Virtualization","Move to Junk Folder|Tag Subject","Sender blocklisted","<5ff2077f65194783bba2d3ca8c24d0cd@1795>"
"09/03/11 01:24:04","Email Blocklist","noreply@engage.redhat.com","user3@domain","Last chance for upcoming webcast: It's Not as Hard as You Think - Stories about Migrating to JBoss","Move to Junk Folder|Tag Subject","Sender blocklisted","<834cdc5f9b8f4ae3ae7feaf028ba0f5d@1795>"
"09/03/11 01:29:40","Email Blocklist","noreply@engage.redhat.com","user1@domain","Last chance for upcoming webcast: It's Not as Hard as YouThink - Stories about Migrating to JBoss","Move to Junk Folder|Tag Subject","Sender blocklisted","<7185b4fd93dc4fa094427e842ea2387c@1795>"
"09/03/11 01:48:00","Email Blocklist","noreply@engage.redhat.com","user2@domain","Last chance for upcoming webcast: It's Not as Hard as You Think - Stories about Migrating to JBoss","Move to Junk Folder|Tag Subject","Sender blocklisted","<09aca6c12f914c8db95b6473c5a16c03@1795>"

2.
"09/03/11 01:48:00","Email Blocklist","noreply@engage.redhat.com","user2@domain","Last chance for upcoming webcast: It's Not as Hard as You Think - Stories about Migrating to JBoss","Move to Junk Folder|Tag Subject","Sender blocklisted","<09aca6c12f914c8db95b6473c5a16c03@1795>"
3.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
Press any key to continue . . .

Open in new window


So, as you can see, the first FINDSTR seems to be working correctly. The second one in the FOR loop returns only one result, and the last one doesn't work at all.

Any suggestions??
0
Bill PrewIT / Software Engineering ConsultantCommented:
Looking now...

~bp
0
Bill PrewIT / Software Engineering ConsultantCommented:
I set up a little test, and this should work now.  A few notes.

- Try to avoid including the quotes in the SET variable content, better to add the quotes as needed when you reference the variables

- In case 2., naturally you will only display the last match found.

- in case 3, in order to access a variable that you updated inside the FOR loop you need to use ! with delayed expansion.

@echo off
setlocal EnableDelayedExpansion

SET _PATH1=C:\EE\EE27506875\Logs (x86)
SET _LOG_BLACKLIST=blacklist.log

set _FROM=noreply
set _SUBJECT=Last chance for upcoming

ECHO 1.
FINDSTR /I /c:"%_SUBJECT%" "%_PATH1%\%_LOG_BLACKLIST%" | FINDSTR /I /c:"%_FROM%"
ECHO.
ECHO 2.
FOR /F "delims=" %%a in ('FINDSTR /I /c:"%_SUBJECT%" "%_PATH1%\%_LOG_BLACKLIST%" ^| FINDSTR /I /c:"%_FROM%"') do set _BLACKLIST=%%a
ECHO %_BLACKLIST%
ECHO 3.
FOR /F "delims=" %%a in ('FINDSTR /I /c:"%_SUBJECT%" "%_PATH1%\%_LOG_BLACKLIST%" ^| FINDSTR /I /c:"%_FROM%"') do (set _BLACKLIST1=%%a & ECHO !_BLACKLIST1!)

Open in new window

~bp
0
agbnielsenAuthor Commented:
Okay... well I want to pipe it to a variable so you're saying the only viable option is use option #3?

Let me ask this before I find myself up the creek without a paddle. The whole reason I want to pipe this to a variable and not simply print it to the screen is I then want to take that output and put it through another for loop to make it presentable.

You can see from the output there are commands and double quotation marks I can use to separate things up. Can I take the variable (_BLACKLIST1) which now has many lines, and use another FOR loop on it to get only the parts I need?

Note: Not asking you to do this as I want to try myself, but I want to make sure it's possible.

Thanks for the help.
0
agbnielsenAuthor Commented:
commands = commas
0
Bill PrewIT / Software Engineering ConsultantCommented:
I would suggest to just do the processing you want with each line inside the iteration block of the main loop.  Take a look at case 4 below, just trying to get you thinking, and let me know if this make sense and what other questions you have.

@echo off
setlocal EnableDelayedExpansion

SET _PATH1=C:\EE\EE27506875\Logs (x86)
SET _LOG_BLACKLIST=blacklist.log

set _FROM=noreply
set _SUBJECT=Last chance for upcoming

ECHO 1.
FINDSTR /I /c:"%_SUBJECT%" "%_PATH1%\%_LOG_BLACKLIST%" | FINDSTR /I /c:"%_FROM%"
ECHO.
ECHO 2.
FOR /F "delims=" %%a in ('FINDSTR /I /c:"%_SUBJECT%" "%_PATH1%\%_LOG_BLACKLIST%" ^| FINDSTR /I /c:"%_FROM%"') do set _BLACKLIST=%%a
ECHO %_BLACKLIST%
ECHO 3.
FOR /F "delims=" %%a in ('FINDSTR /I /c:"%_SUBJECT%" "%_PATH1%\%_LOG_BLACKLIST%" ^| FINDSTR /I /c:"%_FROM%"') do (set _BLACKLIST1=%%a & ECHO !_BLACKLIST1!)
ECHO 4.
for /F "delims=" %%Z in ('FINDSTR /I /c:"%_SUBJECT%" "%_PATH1%\%_LOG_BLACKLIST%" ^| FINDSTR /I /c:"%_FROM%"') do (
  for /F "tokens=1-8 delims=," %%A in ("%%~Z") do (
    echo A=[%%~A]
    echo B=[%%~B]
    echo C=[%%~C]
    echo D=[%%~D]
    echo E=[%%~E]
    echo F=[%%~F]
    echo G=[%%~G]
    echo H=[%%~H]
  )
)

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve KnightIT ConsultancyCommented:
well that was a busy little exchange.... Been wrapping santas offerings since initial post and 1:30am so leaving you in Bill's capable hands!

i had noticed the quote in variable and after and prefer to add them at the point of use too as Bill suggested.  The reason I asked in last Q whether it was the whole line or part is it is easy enough to split it or process the output without needing another for in most cases.  will look back tomorrow to see which way things went.

Steve
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
>> Can I take the variable (_BLACKLIST1) which now has many lines,
Don't get fooled, the var only has a single line, which is the one the FOR is processing just that moment.
In the lines after the FOR loop your var contains only the last result for case 4, like in case 2.

PowerShell is a better "text processor", as it allows to consider text as an array of lines, with filtering and selecting capabilities. Further it can import CSV data into dynamic objects. I suspect you have more than some simple operations in mind, and then PS would be great.
If you do not want to switch languages, http:#a37329076 is the correct start.
0
agbnielsenAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for agbnielsen's comment http:/Q_27506875.html#37328918
Assisted answer: 150 points for gerwinjansen's comment http:/Q_27506875.html#37328653
Assisted answer: 50 points for dragon-it's comment http:/Q_27506875.html#37328713
Assisted answer: 300 points for billprew's comment http:/Q_27506875.html#37328995

for the following reason:

Thanks everyone for your combined input, I hope I have been fair with the allocation of points.<br /><br />The FOR inside the FOR as the end works great and I will work on this. It seems it is getting more and more complicated as I get more and more ideas... I'd like to add the ability to add a date in.<br /><br />I will keep on working on this and reply to this thread if I need any more help. Powershell is a bit outside of my capabilities at the moment.<br /><br />Thanks again.
0
agbnielsenAuthor Commented:
Did not mean to accept my own comment. Comment #37328995 is the accepted solution
0
Bill PrewIT / Software Engineering ConsultantCommented:
Thanks, glad that helped.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.