Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Difference?


  • Please log in to reply

#1
dongdong88

dongdong88

    New Member

  • Member
  • Pip
  • 4 posts
Quick background:

log1.txt contains the text "BUILD SUCCESSFUL"
log.txt does not contain the above text

Am curious by the returned results, anybody knows why it happens?

First I entered the following into Command Prompt:

find "BUILD SUCCESSFUL" log1.txt
echo %errorlevel%

The returned results: 0

Next, I tried this:

find "BUILD SUCCESSFUL" log.txt
echo %errorlevel%

The returned results: 1

I then tried this:

find "BUILD SUCCESSFUL" log1.txt & echo %errorlevel%

The returned results: 1 :) (expecting a 0 instead of 1 here)

Repeated this command again and the returned results is 0.

I then tried the same with log.txt:

find "BUILD SUCCESSFUL" log.txt & echo %errorlevel%

The returned results: 0 (Shouldn't it be 1?)

Repeat the same command again and the returned results is 1.
  • 0

Advertisements


#2
Opa6x57

Opa6x57

    New Member

  • Member
  • Pip
  • 9 posts
I think the ECHO command may be resetting the system errorlevel variable.

Also - this discussion may have some merit in this topic:

http://blogs.msdn.co...26/8965755.aspx

Edited by Opa6x57, 19 March 2010 - 04:04 PM.

  • 0

#3
dongdong88

dongdong88

    New Member

  • Topic Starter
  • Member
  • Pip
  • 4 posts

I think the ECHO command may be resetting the system errorlevel variable.


I don't quite get this. If it is resetting the system errorlevel variable, shouldn't it be a constant? i.e. 0 or 1 always.
  • 0

#4
Opa6x57

Opa6x57

    New Member

  • Member
  • Pip
  • 9 posts
One would think so, I agree. I guess it would depend on the way 'echo' generates return codes. I've not been able to find any reference on 'echo' which defines the return codes it generates. I don't know if it even does.

Have you tried your tests with double & - like this?
find "BUILD SUCCESSFUL" log.txt && echo %errorlevel%

Do you get the same results if you use the errorlevel without the % around it?
find "BUILD SUCCESSFUL" log.txt && echo ERRORLEVEL

  • 0

#5
dongdong88

dongdong88

    New Member

  • Topic Starter
  • Member
  • Pip
  • 4 posts
I've tried your suggestion, now the returned results looks wrong.

The errorlevel is always 1, which isn't true.
  • 0

#6
Opa6x57

Opa6x57

    New Member

  • Member
  • Pip
  • 9 posts
I'm not sure where to go from here.

Me, personally - as a matter of programming habit - would not put the two commands on the same line.
Line-A sets errorlevel (or %errorlevel%)
Line-B reads/reacts to that value.

I would not ever combine the two lines into a single line - I would anticipate inconsistent results. Your experience confirms my suspicions for Windows. I've seen similar in UNIX scripting.

So, I would go back to the first style ...

find "BUILD SUCCESSFUL" log1.txt
echo %errorlevel%

find "BUILD SUCCESSFUL" log.txt
echo %errorlevel%

...which worked.
  • 0

#7
dongdong88

dongdong88

    New Member

  • Topic Starter
  • Member
  • Pip
  • 4 posts

as a matter of programming habit - would not put the two commands on the same line.


This, I agree. I was trying to figure out a colleague's batch file, which sometimes work, sometimes would fail for no reason, even though the batch file ran fine.

It was when taking apart the commands that I saw this curious incident.

If I code it this way:

find "BUILD SUCCESSFUL" log1.txt
echo %errorlevel%

No issues. If I used her codes, inconsistent results. At its worst, incorrect results.

Thanks Opa6x57 for the help, it has certainly been a good learning experience.
  • 0

#8
Opa6x57

Opa6x57

    New Member

  • Member
  • Pip
  • 9 posts
I asked one of my co-workers about this. He is the guy I seek out for this type of help. Here is his answer when I asked him why the 'combined' form of the command didn't work:

Because the command is not finished being processed for the %errorlevel% to register the results before reporting them… when you use & it’s a single command so it will always be successfully the first time run.
Run it the first time and get the false 0, run it again when it should be successfully and you then get the 1 from the previous failure.
Bottom line, you can not evaluate %errorlevel% as part of the same command string as the command you want to evaluate. Remember windows considers a Cr (or CrLf) as the delimiter for a command to be processed.


He also explained why my suggestion of the double& didn't work ...

And && would not work, not valid. You would get nothing for the errorlevel because it would register no response for the command between the &&… so you would have a command of this:
1. find
2.
3. results of #2


  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP