0

I'm trying to use an IF statement with %ERRORLEVEL% to ECHO into a text document that it fails. However, I call another batch file at the top to reference credentials so that I can log onto the remote computer. The batch file thinks %ERRORLEVEL% is a variable in that batch file I called at the top and as a result enters a blank variable in place of %ERRORLEVEL%. Is there a better way to go about this?

call "D:\TEST\CREDS.bat"

net use \\2100\D$ %NETPSWRD% /USER:%WINUSER% /PERSISTENT:NO

IF %ERRORLEVEL% NEQ 0 ECHO 2100>>D:\LOGS\Failed.txt
  • 1
    Either edit creds.bat to perform an exit /b 0 at the end, or clear error level in the current script. See https://stackoverflow.com/questions/1113727/what-is-the-easiest-way-to-reset-errorlevel-to-zero. – jwdonahue Feb 14 '20 at 17:30
  • 1
    Does this answer your question? [What is the easiest way to reset ERRORLEVEL to zero?](https://stackoverflow.com/questions/1113727/what-is-the-easiest-way-to-reset-errorlevel-to-zero) – jwdonahue Feb 14 '20 at 17:31
  • 1
    You need spaces around redirection characters. This is how it is being parsed `IF %ERRORLEVEL% NEQ 0 ECHO 210 0>>D:\LOGS\Failed.txt` Number 0 - 9 before a redirection character is a file handle. –  Feb 14 '20 at 19:05
  • 1
    The last line should be `IF ERRORLEVEL 1 >>"D:\LOGS\Failed.txt" ECHO 2100` to write into file `D:\LOGS\Failed.txt` the string `2100` if `net` exits with a value greater or equal 1. `%SystemRoot%\System32\net.exe` never exits with a negative value as all [Windows Commands](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands). Run in a [command prompt](https://www.howtogeek.com/235101/) `if /?` and read the output help explaining the recommended method to evaluate the exit code of an executable or command using the syntax `IF [NOT] ERRORLEVEL number`. – Mofi Feb 15 '20 at 11:50

0 Answers0