Batch file awlays exit with o

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ryc
Posts: 3
Joined: 26 Jun 2012 12:49

Batch file awlays exit with o

#1 Post by ryc » 26 Jun 2012 12:52

I have below script where I am comparing 2 files. If the files are same, script should exit with 0, else script should exit with 1. In below code, it always exit with 0. How can I achieve above with the below script?

@echo off
:main
fc start.txt a.txt

:End

exit %ERRORLEVEL%

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Batch file awlays exit with o

#2 Post by Fawers » 26 Jun 2012 13:38

Try testing it within a child shell; what does it show?

Code: Select all

@echo off
if not defined call (
  set call=1
  call %0
) else (
  echo %errorlevel%
  pause
  exit
)
:main
fc start.txt a.txt

:End

exit /b %ERRORLEVEL%

ryc
Posts: 3
Joined: 26 Jun 2012 12:49

Re: Batch file awlays exit with o

#3 Post by ryc » 26 Jun 2012 16:48

It gives 0 even if files are same or not. It should return 0 for same and 1 for different files. How to do this?

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Batch file awlays exit with o

#4 Post by Liviu » 26 Jun 2012 17:11

ryc wrote:If the files are same, script should exit with 0, else script should exit with 1.

The code you quoted works correctly here. Doublecheck that your real code does not run other commands before 'exit' which could change the ERRORLEVEL. Also that you don't accidentally have an actual environment variable named ERRORLEVEL with a set value of "0".

Liviu

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Batch file awlays exit with o

#5 Post by Fawers » 26 Jun 2012 17:13

Could you copy-paste the contents of your text files? This wasn't supposed to happen, so I don't know what exactly you can/should do. Despite that, I'd like to test it myself.

ryc
Posts: 3
Joined: 26 Jun 2012 12:49

Re: Batch file awlays exit with o

#6 Post by ryc » 26 Jun 2012 20:24

Basically, I have start.txt file which is null. The other file a.txt will be either null or has some content (I have just put aaaaaa in a.txt).

I want to check if a.txt is null or not. If a.txt is null, I want return code as 0 else 1.

For this, I created dummy empty file start.txt and comparing a.txt with it.

I don't have environemnt variable as ERRORLEVEL. If I execute below code, it returns correctly 'error' if no match and 'No error' if match.
But exit code does not work properly. Any suggestion please?

@echo off
:main
fc start.txt a.txt

:End

if %ERRORLEVEL% == 1 echo "error"
if %ERRORLEVEL% == 0 echo "No error"


exit /b %ERRORLEVEL%

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Batch file awlays exit with o

#7 Post by Liviu » 26 Jun 2012 21:04

ryc wrote:If I execute below code, it returns correctly 'error' if no match and 'No error' if match.
But exit code does not work properly.

Is that the entire batch file, or are there other commands executed after fc and before exit? 'echo' alone won't change or clear the errorlevel, but other commands might.

How do you call the batch file, and how do you check the exit code?

Liviu

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch file awlays exit with o

#8 Post by Ed Dyreen » 26 Jun 2012 21:24

'

Code: Select all

@echo off &setlocal enableDelayedExpansion

echo.
echo.Never override the global variable errorlevel
set /a errorlevel = 10
set /p=<nul
echo.%errorlevel%

set "errorlevel="
set /p=<nul
echo.%errorlevel%
pause

> "file1" (

   echo.file content
)

> "file2" (

   echo.file content is different
)

( fc /b "file1" "file1" &&echo.not different ||echo.different ) &echo.!errorlevel!
  fc /b "file1" "file1" &if errorlevel 1 ( echo.different ) else echo.not different
pause

( fc /b "file1" "file2" &&echo.not different ||echo.different ) &echo.!errorlevel!
  fc /b "file1" "file2" &if errorlevel 1 ( echo.different ) else echo.not different
pause

del /f /q "file1"
del /f /q "file2"
exit

Code: Select all

Never override the global variable errorlevel
10
1
Druk op een toets om door te gaan. . .
Bezig met het vergelijken van bestanden file1 en FILE1
FC: geen verschillen gevonden

not different
0
Bezig met het vergelijken van bestanden file1 en FILE1
FC: geen verschillen gevonden

not different
Druk op een toets om door te gaan. . .
Bezig met het vergelijken van bestanden file1 en FILE2
0000000C: 0D 20
0000000D: 0A 69
FC: FILE2 langer dan file1


different
1
Bezig met het vergelijken van bestanden file1 en FILE2
0000000C: 0D 20
0000000D: 0A 69
FC: FILE2 langer dan file1


different
Druk op een toets om door te gaan. . .

Post Reply