I am running batch file but if exist always returns True , Why is it happening?

67 views Asked by At

I have this code, this always returns True and file get moved. What is the problem with my code ? I am running on Windows batch file.

     @echo off
     title echo
     setlocal enableDelayedExpansion
     for /F "tokens=* delims=" %%a in (vendor.txt) do (
     CD /D H:\RCP\inbound\vendor\drop\%%a\
     for /f "delims=" %%1 in ('dir *.xlsx /b') do (
     set  filename=%%~n1
    set text=!filename!_Response_File.xlsx
    set  filepath=H:\RCP\outbound\vendor\%%a\!text! 
    echo !text!  
    echo !filepath!     
    echo H:\RCP\outbound\vendor\%%a\!text!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
    if exist  H:\RCP\outbound\vendor\%%a\!text! (
    echo file will be moved
)  else (
   echo file does not exist
)
)
)
1

There are 1 answers

7
Adrian Maxwell On

It might simply be a typo (%%a instead of %a%) but why not use more meaningful variable names e.g.:

set folder=yourfolder
set filename=yourfile.txt

if exist H:\RCP\outbound\vendor\%folder%\%filename% (
   echo file will be moved: %folder%\%filename%
) else (
   echo file does not exist: %folder%\%filename%
)