DOS / BAT - How to check if a file exists

The syntax of the DOS EXIST statement is:

IF [NOT] EXIST filename command [ELSE expression]

File Examples:

if exist file.txt del file.txt

if exist "long file.txt" del "long file.txt"

if exist file.txt (
    echo found
    del file.txt

Directory Examples:

if exist directory rmdir /s /q directory

if exist "long directory" rmdir /s /q "long directory"

if exist directory (
    echo found
    rmdir /s /q directory

Ads by Google

Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath