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%
Batch file awlays exit with o
Moderator: DosItHelp
Re: Batch file awlays exit with o
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%
Re: Batch file awlays exit with o
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?
Re: Batch file awlays exit with o
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
Re: Batch file awlays exit with o
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.
Re: Batch file awlays exit with o
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%
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%
Re: Batch file awlays exit with o
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
Re: Batch file awlays exit with o
'
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. . .