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

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.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest