how to capture standard erros like "The system cannot find the drive specified." or "The system cannot find the path specified." in a batch script

StackOverflow https://stackoverflow.com/questions/13393697

  •  29-11-2021
  •  | 
  •  

Вопрос

I am trying to develop a windows batch program where if error like The system cannot find the drive specified. or The system cannot find the path specified. comes, then "fld_chk.out" file can be checked and looping can happen.

But cd A:\rr\Br>fld_chk.out is not capturing these errors.

how to do capture there standard errors?

My code is like this:-

cd A:\rr\Br>fld_chk.out
cd B:\yy\dd>>fld_chk.out
find /c "The system cannot find" *.out>fld_count_check_1.out
find /c "0" fld_count_check_1.out>fld_count_check_2.out
FOR /F "TOKENS=1* DELIMS=:" %%B IN (fld_count_check_2.out) DO SET b=%%C
set _count=%b%
IF %_count% EQU 2 goto Success
IF not %_count% EQU 2 goto notSuccess
:Success
echo folder found
:notSuccess
echo folder not found

Thanks in advance Sree

Это было полезно?

Решение

The way to do that is by checking %ERRORLEVEL% value after cd command was executed:

cd A:\rr\Br 2> NUL
if %errorlevel% equ 0 (
   echo folder found
) else (
   echo folder not found
)

If the value is 0, CD was correctly executed and current directory be changed; otherwise (errorlevel==1) the drive or directory does not exist.

The 2> NUL part is to avoid that the error message appear in the screen.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top