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?
31 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