Findstr | if () else() --what's wrong?

December 22, 2010 at 10:14:52
Specs: Windows XP
I have this as the last part of a script and I consistently get an errorlevel of 0 even if %mydate% doesn't appear in the file that I'm searching. I'm not sure what I'm doing wrong or what I'm not doing here. I've tried using "if %errorlevel%==1 if not %errorlevel%==0" but that doesn't do anything. I'm not the best at this but am learning, so any help would be appreciated as I will be needing to write more scripts that look for strings within files and then create system/application events based on output. Also, I am running this on the command line, but because I am using a managed service front end app, I need to keep the ">>" in the code, but I've tried it in the command line syntax of only 1 ">" and get the same results.


For /f "tokens=2-4 delims=/ " %a in ('date /t') do (set mydate=%c%a%b) & echo %mydate% >> null | findstr /c:"%mydate%" c:\imt-temp\filename.txt | if %errorlevel%==1(eventcreate /SO "Datair Backup" /T ERROR /ID 4 /L APPLICATION /D "Backup Failure" >> null) else (eventcreate /SO "Datair Backup" /T Information /ID 5 /L APPLICATION /D "Backup Successful" >> null)


See More: Findstr | if () else() --whats wrong?

Report •

#1
December 22, 2010 at 10:41:25
That's quite a "line".

Right off the bat, if it's a script, %a needs to be %%a and NULL is a file by the name of nul; not the bit bucket.

Dunno why you pipe from there to finstr.

Work on those issues and maybe simplify it a little to lower the fog index.


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
December 22, 2010 at 12:50:47
M2--

For /f "tokens=2-4 delims=/ " %a in ('date /t') do (set mydate=%c%a%b) & echo %mydate% > nul & findstr /c:"%mydate%" c:\imt-temp\filename.txt | if %errorlevel%==1 (eventcreate [parameters] > nul) else (eventcreate [parameters] > nul)

Maybe this looks a little more tidy. I'll just keep it in the command line format so everything is the same, sorry about that.

Like I said, I'm new to this and learning as I go. Regarding the NULL...my mistake, it should be "nul". I completely lost sight of that one.
The reason I have sardined that into one line is because the MSP software, by default, doesn't carry over set variables from one command line execution if I break up that "line" into two separate command line "executables", for lack of a better word. I wish i didn't have to do that, but I don't feel like creating .bat files on the client boxes as it creates clutter and the need to have to copy down a ton of files since I manage a few hundred machines.

Why I piped, I don't know enough when no to. Advice is always appreciated in any form...well almost any.


Report •

#3
December 22, 2010 at 13:34:26
[1] does "c:\imt-temp\filename.tx" HAVE what findstr is looking for?

[2] %errorlevel% may not resolve as hoped inside a FOR without using:
setLocal enableDELAYedeXpansion
and !errorlevel! which commits you to a BAT. Almost.


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
December 22, 2010 at 14:22:00
Yes, "c:\imt-temp\Filename.txt" contains a filename with a date in it in the same format at %mydate%. ie--"DATAIR_DATAIR_BACKUP_20101222_2300.BAK" would be a good example of what would be in "Filename.txt".

I had tried the setlocal enabledelayedexpansion on one of my tests and got nothing different, however, I didn't use !errorlevel! How would that be placed in the code? Also, you say I may "almost" be committed to a .BAT.......

I have another script that's doing almost the same thing as this one and the two scripts use this same line I am having the issue with...but on the other script, instead of looking for %mydate%, the findstr is looking for a set value: ie-- findstr /c:"0 error" filename.txt

Thanks for helping me understand....


Report •

#5
December 22, 2010 at 14:50:28
The more I look at this the less sense it makes. What is the point of this?

For /f "tokens=2-4 delims=/ " %a in ('date /t') do (set mydate=%c%a%b) & echo %mydate% > nul


=====================================
Life is too important to be taken seriously.

M2


Report •

#6
December 22, 2010 at 15:55:52
The %mydate% is not going to expand to the value you have just set, because it will be expanded when the whole line is read in.

You could put the whole thing into a .bat file, split it up into separate lines, and use setlocal enabledelayedexpansion and !mydate! instead of %mydate%. If you don't want to use a .bat file, then you can't use setlocal enabledelayedexpansion. You can instead use a trick that does delayed expansion without first enabling it, which works from the command line: Put a CALL command in front of the command that has %mydate%. Another thing, the %errorlevel% is also going to suffer from being expanded too early. You have two ways of solving this:

For /f "tokens=2-4 delims=/ " %a in ('date /t') do (set mydate=%c%a%b) & call findstr /c:"%mydate%" c:\imt-temp\filename.txt & if not errorlevel 1 (eventcreate [parameters] > nul) else (eventcreate [parameters] > nul)

OR

For /f "tokens=2-4 delims=/ " %a in ('date /t') do (set mydate=%c%a%b) & call findstr /c:"%mydate%" c:\imt-temp\filename.txt && (eventcreate [parameters] > nul) || (eventcreate [parameters] > nul)


Explanation:

if errorlevel 1 checks whether the errorlevel is 1 or higher, so
if not errorlevel 1 checks whether the errorlevel is 0.

also the second option:

command && success-path || failure-path

the && operator executes the command to its right if the command to its left has succeeded (i.e. returned an errorlevel of 0)

the || operator executes the command to its right if the command to its left has failed (i.e. returned an errorlevel of 1 or greater)


Report •

#7
December 22, 2010 at 16:37:47
Hi klint,

Thanks for that last bit. I've been trying to get my head around ||.


=====================================
Life is too important to be taken seriously.

M2


Report •

#8
December 23, 2010 at 06:13:43
Klint -
Excellent explanation and use of examples. You got to the heart of what I needed to know.

The main issues I have are that I am 10 years out of writing any type of scripts or C/C++ stuff. Now, after all this time I come back and I am "rusty" to say the least.
Some explanations out there regarding &&, ||, CALL, etc...don't always put them into a context which deals with expanding variables and reusing outputs.

Some things like !mydate! & !errorlevel! I don't know about, how they are used and where they go in commands. If you could possibly help me understand that a bit better, my batch and command line writing would greatly benefit from it.

Thanks again!


Report •

#9
December 23, 2010 at 06:22:05
Required reading:
SET /?
Suggested reading:
IF /?

How To Ask Questions The Smart Way


Report •

#10
December 23, 2010 at 06:34:17
Klint,

Your second example with && and || works like a charm! Thanks!, REALLY.


Report •

#11
December 23, 2010 at 06:35:34
Razor--

I'll read up on those topics. Thanks for the advice! You guys made my morning, day, and upcoming weekend. Ahhhhhh.


Report •

#12
December 23, 2010 at 07:26:41
Razor had good advice about reading the on-line help from those two commands; I would also add FOR /? for some more advanced stuff after you've read the ones suggested by Razor. Have a good Christmas, enjoy the next fews days and don't spend too much time in front of a computer ;-)

Report •

#13
December 23, 2010 at 07:47:11
Klint

I won't be in front of a computer for too much longer this week....1/2 day tomorrow and then I'll be under one of my Volvo's, pulling the driveshaft to do the u-joints....ahh, joyous!


Report •

Ask Question