Link to home
Start Free TrialLog in
Avatar of agbnielsen
agbnielsen

asked on

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
SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of agbnielsen
agbnielsen

ASKER

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

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

FFS
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??
Looking now...

~bp
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
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.
commands = commas
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
>> 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.
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.
Did not mean to accept my own comment. Comment #37328995 is the accepted solution
Thanks, glad that helped.

~bp