Search code examples
batch-filecmd

Finding the file having keyword test.html in the folder using batch


I am trying to find a file ends with the test.html in the directory using batch script currently what I have tried so far and test.html does not exist in the folder abc

setlocal enabledelayedexpansion
set "hastestHtml=false"
for /f %%f in ('dir /s /b "C:\\Users\\abc\\test.html"') do (
    set "hastestHtml=true"
    )
if ( !hastestHtml ) (
    echo "No test.html file found."
) else (
   echo "A test.html file was found."
   exit /b 1
    )

Expected output: No test.html file found.

Current Output:

File Not Found
!hastestHtml was unexpected at this time.

Solution

  • There is something much easier than that, use the if parameter

    if exist "C:\\Users\\abc\\test.html" (echo A test.html file was found.) else (echo No test.html file found.)