Page 1 of 1

CMD Command Line: One command plus direct output of the ERRORLEVEL into a TXT?

Posted: 02 Feb 2017 02:04
by tds1973lbp
CMD Command Line:
One CMD command, one command row, no BAT file. Is there a chance to get the ERRORLEVEL directly written in a TXT file?

I tried but I failed.

I don't want to create and save a BAT file each time.

I would prefer the simpler way: CMD single command plus export of the ERRORLEVEL in a TXT file, all in one command line.

TD :D

Re: CMD Command Line: One command plus direct output of the ERRORLEVEL into a TXT?

Posted: 02 Feb 2017 03:06
by aGerman
You could run a separate instance of cmd with delayed expansion enabled

Code: Select all

cmd /von "dir :*&echo !errorlevel!"


It might be easier to use conditional command chaining though.

Code: Select all

dir :* && (echo errorlevel is 0) || (echo errorlevel other than 0)


Steffen

Re: CMD Command Line: One command plus direct output of the ERRORLEVEL into a TXT?

Posted: 02 Feb 2017 03:17
by tds1973lbp
Hi Steffen,

I need something like this:

( commandToExecute ) & echo %errorlevel%>"file.txt"

I want the error level integer value (0 or 2 or 5 or something else) get written in the txt file. Only that value.

Re: CMD Command Line: One command plus direct output of the ERRORLEVEL into a TXT?

Posted: 02 Feb 2017 03:20
by aGerman
What about my first suggestion? (dir :* is only an example that generates errorlevel 1)

Code: Select all

cmd /von /c "dir :*&>"file.txt" echo !errorlevel!"


Steffen

Re: CMD Command Line: One command plus direct output of the ERRORLEVEL into a TXT?

Posted: 02 Feb 2017 03:28
by tds1973lbp
Hello Steffen,

when I execute

cmd /von ""C:\Program Files\testapp\testapp.exe" &>"file.txt" echo !errorlevel!"

nothing happens.

Re: CMD Command Line: One command plus direct output of the ERRORLEVEL into a TXT?

Posted: 02 Feb 2017 03:35
by aGerman
The /c switch was missing (I already corrected the line above).
If this doesn't help use CALL to run the program. (Sometimes pathes in quotes won't execute.)

Code: Select all

cmd /von /c "call "C:\Program Files\testapp\testapp.exe"&>"file.txt" echo !errorlevel!"


Steffen

Re: CMD Command Line: One command plus direct output of the ERRORLEVEL into a TXT?

Posted: 02 Feb 2017 04:15
by tds1973lbp
Hello Steffen,

it works! Many thanks! :D

Have a nice day!

TD

Re: CMD Command Line: One command plus direct output of the ERRORLEVEL into a TXT?

Posted: 02 Feb 2017 04:18
by aGerman
Thats good news :)