Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 564 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get an errorlevel in a for /f loop from the command result?

#1
I'm running an SQL request in batch-file and I need to get the errorlevel as well as the output it gives. I've tried using :

for /f "tokens=*" %%i in ('%SQLCommand%') do ( ...

and in the 'do', if the errorlevel is 1, then I can do whatever I plan to do, but I can't seem to find a way to get the errorlevel from the result of the command. I'm also not sure if the errorlevel can change while in the loop, but that I was planning on testing it as so I would get a way to know if the command change the errorlevel.

Reply

#2
The problem here is that the command line assigned to environment variable `SQLCommand` is executed by __FOR__ in a separate command process in background with `cmd.exe /C` and its output to handle __STDOUT__ is captured by __FOR__ and processed line by line. For that reason the exit code of the SQL command line cannot be evaluated in command process executing the batch file.

The most likely best solution is running the SQL command line in current command process with redirecting the output of the SQL command line into a temporary file, evaluating the exit code and processing the output according to exit code.

The batch file below demonstrates this solution. It can be started without an argument, with a valid file/folder name as first argument, or with an invalid file/folder name for watching error handling.

<!-- language: lang-none -->

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "OutputFile=%TEMP%\%~n0.tmp"

if "%~1" == "" ( set "DirArg=%TEMP%" ) else set "DirArg=%~1"
set "SQLCommand=dir /A /B "%DirArg%""

%SQLCommand% >"%OutputFile%" 2>nul
echo/

if errorlevel 1 (
echo An error occurred on execution of command:
echo/
echo %SQLCommand%
echo/
echo Exit code is: %ERRORLEVEL%
goto EndBatch
)

echo Execution of command was successful. The output is:
echo/

for /F "usebackq delims=" %%I in ("%OutputFile%") do echo %%I

:EndBatch
del "%OutputFile%"
endlocal
echo/
pause

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

+ `del /?`
+ `echo /?`
+ `endlocal /?`
+ `for /?`
+ `goto /?`
+ `if /?`
+ `pause /?`
+ `set /?`
+ `setlocal /?`

See also the Stack Overflow questions:

+

[To see links please register here]

+

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through