How can I detect errors in diskpart when using a pipe?

I am trying to eliminate the temporary script file (i.e. diskpart /s scriptFile) used to automate diskpart by replacing it with commands issued via a pipe, however I can't seem to figure out how to detect when diskpart throws an error. If you have a drive N: change the select vol n with a non-existent drive so diskpart will generate an error:

( ( echo select disk 0 echo list partition echo select vol n ) | diskpart set foundErr=1 if errorlevel 0 if not errorlevel 1 set "foundErr=" if defined foundErr goto :errorMsg echo Success! goto :EOF
) :errorMsg echo diskpart failed! goto :EOF
pause 

I based the code above on this answer, but it's not working.

Is this even possible, or does the use of a pipe block or interfere with diskpart error detection?

3

1 Answer

I haven't tested it yet. but this should work:

( ( echo select disk 0 echo list partition echo select vol n ) | diskpart if "%errorlevel%"=="0" (
if not "%errorlevel%"=="1"
(
set "foundErr="
)
) Else set foundErr=1 if defined foundErr goto errorMsg echo Success! goto :EOF
) :errorMsg echo diskpart failed! goto :EOF
pause 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like