how do you determine if a cmd shell executed successful in

if you run a command prompt to execute say psexec, how can you determine if the error code was 0 or not?
i tried
error1="cmd.exe /c ....."
if error1 <>0 then
 msgbox("there was an error")

Open in new window

it doesn't work though so again how do i determine if a cmd shell executed successfully?
Who is Participating?
David GipeCommented:
David GipeCommented:
During testing ... if you either (1) put a "pause" at the end of the CMD commands or (2) put something like "echo CMDScriptRan > c:\ItRan.txt" at the end of the CMD commands.

This should tell you if it is executing the CMD script(s). I would probably do (2) then delete the file and remove the echo line from the CMD lines, once it seems to be working correctly.
bbimisAuthor Commented:
well i was wanting to capture the error code if possible. if error is 0 then it was successful if not 0 then it failed
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

sample batch file that <--- means brief description of action being taken

Echo Off
cd C:\MYBATS  <--- directory where i store my batch file"
net use z: <---"create a map to one of my server shares"
echo Copying started at %date% %time%>>RunTimes.txt <--- notice the date / time stamp being written to a text file
copy z:\*.* /y "copy all my files from mapped drive to the local drive" <--- begin copying files
echo Copying finished at %date% %time%>>RunTimes.txt <--- another date / time stamp
echo Completed Successfully at %date% %time%>>RunTimes.txt <--- final date / time stamp
echo "">>RunTimes.txt <---just writes a blank line to file to separate entries
net use z: /del <--- delete the server share

You end up with a RUNTIMES.TXT file and the following information:

Copying started at Sat 05/05/2018 21:00:01.55
Copying finished at Sat 05/05/2018 21:01:44.30
Completed Successfully at Sat 05/05/2018 21:01:44.30
Copying started at Sun 05/06/2018 21:00:01.87
Copying finished at Sun 05/06/2018 21:01:24.45
Completed Successfully at Sun 05/06/2018 21:01:24.45
Copying started at Mon 05/07/2018 21:00:01.50
Copying finished at Mon 05/07/2018 21:01:36.87
Completed Successfully at Mon 05/07/2018 21:01:36.87
Copying started at Tue 05/08/2018 21:00:01.32
Copying finished at Tue 05/08/2018 21:01:21.92
Completed Successfully at Tue 05/08/2018 21:01:21.92
Copying started at Wed 05/09/2018 21:00:01.66
Copying finished at Wed 05/09/2018 21:01:22.18
Completed Successfully at Wed 05/09/2018 21:01:22.18

and so on and so on....
David GipeCommented:
I did some testing then research. Learn something new every day!

This may assist what you are wanting to confirm (see ); specifically his comments at the end "So you can perform other types of tests against the error level, for example, to perform an equality test:
IF %ERRORLEVEL% EQU 1 echo Different!" ... plus others comments on the post itself.

They should give you some ideas how to determine the errorlevel, depending on the complexity of what you are running.
bbimisAuthor Commented:
so how do you do it in
bbimisAuthor Commented:
This gave me what i needed. Thanks for the help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.