Syntax - Am I missing some detail?

September 5, 2009 at 05:12:20
Specs: Windows XP Pro SP3
Hi,

I have a doubt in a batch file I am making.
The findstr command ALWAYS returns errorlevel=0 within a IF statement. I had to transfer findstr to another label and call this label within the IF statement.

Is there a more economic solution than to create a new label for every findstr command that occurs within IF. (code would result more clean)

Thanks.


See More: Syntax - Am I missing some detail?

Report •


#1
September 5, 2009 at 05:17:58
Are you using IF %ERRORLEVEL%, or IF ERRORLEVEL? Because IF statements are under the same environment variable restrictions as FOR loops.

Report •

#2
September 5, 2009 at 07:06:01
It's like that, Raizon:

setlocal enabledelayedexpansion
@echo off
set a=1
if %a%==1 (
findstr /x "NonExistentString" test.txt
echo if not %errorlevel%==0 set x=%a%
)
pause

------
The echo command above is only to display the real command in front it (if not 0=0 ...)

Do I have to use %% somewhere?


Report •

#3
September 5, 2009 at 07:10:14
Option 1:
if %a%==1 (
findstr /x "NonExistentString" test.txt
if errorlevel 1 set x=%a%
)

Option 2:

if %a%==1 (
findstr /x "NonExistentString" test.txt || set x=%a%
)


Report •

Related Solutions

#4
September 5, 2009 at 11:14:12
Thank you very much, Razor. You are really great!

Report •

#5
September 5, 2009 at 17:10:07
I just finished something that may help explain the restriction Razor2.3 was referring to, it's a bit long but it does cover a few things...

http://www.computing.net/howtos/sho...

Suggestions welcome.


Report •

#6
September 5, 2009 at 17:42:29
I've been thinking someone should write a quick article on the steps the Command Prompt goes though for each line in a script. That way, we could point to that article when these types of questions come up.

Report •

#7
September 5, 2009 at 17:55:57
I've been thinking someone should write a quick article on the steps the Command Prompt goes though for each line in a script. That way, we could point to that article when these types of questions come up.

Exactly my motivation.


Report •

#8
September 6, 2009 at 13:04:36
Judago, congratulations for your article. It is helpful and clear.
Knowing some concepts, practices, resources like that you have treated are essential and certainly save everyone a lot of digging.

I thing batch scripting is lacking of more detailed (and clear!) material for study. Nothing massive but only a good collection of catchs and tips designed for "advanced beginners" and low intermediate level users.

For instance, a beginner would never figure out this code (bellow) unless he had seen it before:
type input.txt | find /v "string1" | find /v "string2" >> output.txt

This code is trivial and everyone will see it everywhere, then it doesn't requires any help.

But there are codings that are not so trivial, like your "call echo %%test%%" or "java -version 2> #".

This second is a snippet of code by M2 in the topic "Pull Multiple Lines into Text File". It was to suit a very peculiar problem and was unthinkable for me.

Not trivial: "A code block is treated as a line ...."

Bellow, not trivial (endlocal without reset some variables / return has to be in the same code block or line).
(
endlocal
set variable=%variable%
set v2...
...
)

When I get more skilled in bats I intend to make something like a handy compendium of scripts and shortcut concepts for beginners. I will do it for my son, I am certain he will enjoy it, as many others will do.
(Get cool, Judago! I will not pirate your How-To.)

Regards!


Report •


Ask Question