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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tds1973lbp
Posts: 7
Joined: 02 Feb 2017 01:19

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

#1 Post by tds1973lbp » 02 Feb 2017 02:04

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

aGerman
Expert
Posts: 3178
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 02 Feb 2017 03:06

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

tds1973lbp
Posts: 7
Joined: 02 Feb 2017 01:19

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

#3 Post by tds1973lbp » 02 Feb 2017 03:17

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.

aGerman
Expert
Posts: 3178
Joined: 22 Jan 2010 18:01
Location: Germany

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

#4 Post by aGerman » 02 Feb 2017 03:20

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

tds1973lbp
Posts: 7
Joined: 02 Feb 2017 01:19

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

#5 Post by tds1973lbp » 02 Feb 2017 03:28

Hello Steffen,

when I execute

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

nothing happens.

aGerman
Expert
Posts: 3178
Joined: 22 Jan 2010 18:01
Location: Germany

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

#6 Post by aGerman » 02 Feb 2017 03:35

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

tds1973lbp
Posts: 7
Joined: 02 Feb 2017 01:19

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

#7 Post by tds1973lbp » 02 Feb 2017 04:15

Hello Steffen,

it works! Many thanks! :D

Have a nice day!

TD

aGerman
Expert
Posts: 3178
Joined: 22 Jan 2010 18:01
Location: Germany

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

#8 Post by aGerman » 02 Feb 2017 04:18

Thats good news :)

Post Reply