Search code examples
batch-filecmd

Check if input is a file or folder


I have a command like this:

set /p txtfile=Write destination to the .txt file:

now i need to know if the file really exists so:

if exist %txtfile% echo file found.

it works, but if the user writes me C:\ it will display "file found" to. I want to know is there a way to know a file extension using batch, any ideas?


Solution

  • You can identify it as a file using TYPE. However, it might not be the best way if it is a large file.

    SET "DIR_ITEM=C:\Windows"
    
    TYPE "%DIR_ITEM%" 1>NUL 2>&1
    IF ERRORLEVEL 1 (
        ECHO "%DIR_ITEM%" is not a file
    ) ELSE (
        ECHO "%DIR_ITEM% is a file
    )